-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rkt
109 lines (80 loc) · 2.49 KB
/
main.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#lang racket
(define ns (make-base-namespace))
(provide (all-defined-out))
(define := (lambda (var value)
(list var value)
))
(define -- (lambda args
(list 'let args)
))
(define @ (lambda (bindings expr)
(append bindings expr) )
)
(define split_at_delim (lambda (delim args)
(foldr (lambda (head tail)
(if (eqv? head delim)
(cons '() tail)
(cons (cons head (car tail)) (cdr tail) ))
)
(list '()) args)))
(define split_at_delim2 (lambda (delim args)
(foldr (lambda (head tail)
(if (eqv? head delim)
(cons '() tail)
(cons (cons head (car tail)) (cdr tail) ))
)
'() args)))
(define parse_expr (lambda (expr) ;expr + expr
(cond
[(and (list? expr) (> (length expr) 2) (member '+ expr))
(cons '+ ( map term (split_at_delim '+ expr)) )]
[else (term expr)]
)))
(define term (lambda (expr) ;expr * expr
(cond
[(and (list? expr) (> (length expr) 2) (member '* expr))
(cons '* ( map factor (split_at_delim '* expr)) )]
[else (factor expr)]
)))
(define factor (lambda (expr) ;binding list @ expression
(cond
[(and (list? expr) (> (length expr) 2) (member '@ expr))
(list 'let (car (BindList (caar (split_at_delim '@ expr)))) (parse_expr (cdr (member '@ expr))))]
[else (bindL expr)]
)))
(define bindL (lambda (expr) ;checks for binding list
(cond
[(and (list? expr) (= 1 (length expr)))
(parse_expr (car expr))]
[(and (list? expr) (> (length expr)) 1) (parse_expr expr)]
[else expr]
)))
(define assignmentCheck (lambda (expr) ;is a valid binding list
(cond
[(and (list? expr) (= (length expr) 3) (eqv? (cadr expr) ':=)) 0]
[(null? expr) 0]
[else 1]
)))
(define break (lambda (expr) ;check for assignment: "var := var" or "var := number"
(cond
[(number? (caddr expr)) (list (cadar expr) (caddr expr))]
[else (list (cadar expr) (cadr (caddr expr)) ) ]
)))
(define assignmentList (lambda (expr) ;is an assignment? if so check which type
(cond
[(and (list? expr) (= (length expr) 3) (eqv? (cadr expr) ':=))
(break expr)
]
[(null? expr) '()]
[else #f]
)))
(define BindList (lambda (expr) ;remove --
(cond
[(eq? 0 (foldl + 0 (map assignmentCheck (split_at_delim2 '-- expr))))
(list (map assignmentList (split_at_delim2 '-- expr)))
]
[else #f]
)))
(define eval_expr (lambda(expr)
(eval (parse_expr expr) ns)
))