3 ████████╗██╗ ██╗██╗ ██╗███╗ ██╗
4 ╚══██╔══╝██║ ██║██║ ██║████╗ ██║
5 ██║ ███████║██║ ██║██╔██╗ ██║
6 ██║ ██╔══██║██║ ██║██║╚██╗██║
7 ██║ ██║ ██║╚██████╔╝██║ ╚████║
8 ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝
10 Copyright © 2023 Simon Forman
12 This file is part of Thun
14 Thun is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
19 Thun is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with Thun. If not see <http://www.gnu.org/licenses/>.
30 (import (chicken string))
33 ;(load "defs.scm") ; csc -prologue defs.scm joy.scm
35 (chicken-script (load "defs.scm"))
38 (define (joy stack expression dict)
39 (if (null? expression)
41 (if (string? (car expression))
43 (joy-eval (car expression) stack (cdr expression) dict)
45 (joy (cons (car expression) stack) (cdr expression) dict))))
47 (define (joy-eval symbol stack expression dict)
48 (define (is-it? name) (string=? symbol name))
50 ((is-it? "+") (values (joy-add stack) expression dict))
51 ((is-it? "-") (values (joy-sub stack) expression dict))
52 ((is-it? "mul") (values (joy-mul stack) expression dict))
53 ((is-it? "dup") (values (joy-dup stack) expression dict))
54 ((hash-table-exists? dict symbol)
55 (values stack (append (hash-table-ref dict symbol) expression) dict))
56 (else (error "Unknown word."))))
58 (define (joy-add stack) (cons (+ (cadr stack) (car stack)) (cddr stack)))
59 (define (joy-sub stack) (cons (- (cadr stack) (car stack)) (cddr stack)))
60 (define (joy-mul stack) (cons (* (cadr stack) (car stack)) (cddr stack)))
61 (define (joy-dup stack) (cons (car stack) stack))
64 (define (string-replace str from to)
65 (string-intersperse (string-split str from #t) to))
67 (define (tokenize str)
69 (string-replace (string-replace str "]" " ] ") "[" " [ ")))
71 (define (tokenator token)
72 (cond ((string->number token) (string->number token))
73 ((string=? token "true") #t)
74 ((string=? token "false") #f)
77 (define (expect-right-bracket tokens acc)
79 (error "Missing closing bracket.")
80 (expect-right-bracket-lookahead (car tokens) (cdr tokens) acc)))
82 (define (expect-right-bracket-lookahead token tokens acc)
83 (cond ((string=? token "]") (values acc tokens))
85 (receive (sub_list rest) (expect-right-bracket tokens '())
86 (receive (el rrest) (expect-right-bracket rest acc)
87 (values (cons sub_list el) rrest))))
89 (receive (el rest) (expect-right-bracket tokens acc)
90 (values (cons (tokenator token) el) rest)))))
92 (define (one-token-lookahead token tokens)
93 (cond ((string=? token "]") (error "Extra closing bracket."))
94 ((string=? token "[") (expect-right-bracket tokens '()))
95 (else (values (tokenator token) tokens))))
97 (define (parse0 tokens acc)
100 (receive (term rest_of_tokens)
101 (one-token-lookahead (car tokens) (cdr tokens))
102 (cons term (parse0 rest_of_tokens acc)))))
104 (define (parse tokens) (parse0 tokens '()))
106 (define (text->expression text) (parse (tokenize text)))
109 (define (joy-term->string term)
110 (cond ((boolean? term) (if term "true" "false"))
111 ((number? term) (->string term))
112 ((list? term) (conc "[" (joy-expression->string term) "]"))
115 (define (joy-expression->string expr)
116 (string-intersperse (map joy-term->string expr) " "))
119 (receive (stack _dict)
120 (joy '() (text->expression text) (initialize))
121 (joy-expression->string stack)))
125 (load-defs (make-hash-table string=? string-hash)))
127 (define (load-defs dict)
128 (map (lambda (def) (add-def def dict)) (defs)) ;defs is defined in defs.scm
131 (define (add-def def dict)
132 (let ((def_list (text->expression def)))
133 (hash-table-set! dict (car def_list) (cdr def_list))))
136 (display (doit "12 23 [[ ]] 23 4 - dup - [true] false 23 sqr"))