Load defs from defs.txt.

They are loaded at runtime though :(  shouldn't be too hard to fix that?
This commit is contained in:
sforman 2023-08-11 08:50:00 -07:00
parent b6c3c364d0
commit d014709a32
1 changed files with 13 additions and 10 deletions

View File

@ -1,6 +1,8 @@
(import (chicken io))
(import (chicken string)) (import (chicken string))
(import srfi-69) (import srfi-69)
(define (joy stack expression dict) (define (joy stack expression dict)
(if (null? expression) (if (null? expression)
(values stack dict) (values stack dict)
@ -78,25 +80,26 @@
(string-intersperse (map joy-term->string expr) " ")) (string-intersperse (map joy-term->string expr) " "))
(define (doit text) (define (doit text)
(receive (stack dict) (receive (stack _dict)
(joy '() (text->expression text) '()) (joy '() (text->expression text) (initialize))
(joy-expression->string stack))) (joy-expression->string stack)))
(define (initialize defs dict) (define (initialize)
(map (lambda (def) (add-def dict def)) (string-split defs "\n"))) (load-defs
; TODO: load defs at compile-time, not run-time.
(with-input-from-file "../defs.txt" read-string)
(make-hash-table string=? string-hash)))
(define (load-defs defs dict)
(map (lambda (def) (add-def def dict)) (string-split defs "\n"))
dict)
(define (add-def def dict) (define (add-def def dict)
(let ((def_list (text->expression def))) (let ((def_list (text->expression def)))
(hash-table-set! dict (car def_list) (cdr def_list)))) (hash-table-set! dict (car def_list) (cdr def_list))))
(display (doit "ab cd [[ ]] 23 4 - dup - [true] false")) (display (doit "ab cd [[ ]] 23 4 - dup - [true] false"))
(newline) (newline)