-
Notifications
You must be signed in to change notification settings - Fork 1
/
expression-ops.scm
118 lines (105 loc) · 3.43 KB
/
expression-ops.scm
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
110
111
112
113
114
115
116
117
118
; Brett Johnson
; Adam Beck
; Daniel Grigsby
#lang racket
(provide (all-defined-out))
; Checks if an expression is a a valid expression
(define G-expr?
(lambda (arglist)
(cond
((math-expr? (get-op-from-expr arglist)) #t)
((boolean-expr? (get-op-from-expr arglist)) #t)
((compare-expr? (get-op-from-expr arglist)) #t)
(else #f))))
(define get-op-from-expr car)
; Checks to see if a math expression is encountered
(define math-expr?
(lambda (op)
(cond
((eq? op '+) #t)
((eq? op '-) #t)
((eq? op '*) #t)
((eq? op '/) #t)
((eq? op '%) #t)
(else #f))))
; Checks to see if a boolean expression is encountered
(define boolean-expr?
(lambda (op)
(cond
((eq? op '&&) #t)
((eq? op '||) #t)
((eq? op '!) #t)
(else #f))))
; Checks to see if a comparing expression is encountered
(define compare-expr?
(lambda (op)
(cond
((eq? op '==) #t)
((eq? op '!=) #t)
((eq? op '<) #t)
((eq? op '>) #t)
((eq? op '<=) #t)
((eq? op '>=) #t)
(else #f))))
; Determines whether a boolean in java boolean notation was encountered
(define java-boolean?
(lambda (value)
(cond
((eq? value 'true) #t)
((eq? value 'false) #t)
(else #f))))
; Converted a java boolean name to a scheme boolean name (e.g. true in java is #t in scheme)
(define java-bool-to-scheme-bool
(lambda (value)
(cond
((eq? value 'true) #t)
((eq? value 'false) #f)
(else (error "not a java boolean")))))
; this function takes a boolean operator (ie: !) and returns the actual function
; this translation is for 1 argument boolean expressions
(define boolean-operator-to-function-uni
(lambda (op)
(cond
((eq? op '!) (lambda (arg1) (not arg1)))
(else (error "unsupported unary boolean expression")))))
; this function takes a math operator ie: -> along with a boolean is-int that specifies whether the values are integers
; this translation is for 1 argument math expressions
(define math-operator-to-function-uni
(lambda (op is-int)
(cond
((eq? op '-) -)
(else (error "invalid unary math operator")))))
; this function takes a math operator ie: +, ->, * ... along with a boolean is-int that specifies whether the values are integers
; this translation is for 2 argument math expressions
(define math-operator-to-function-multi
(lambda (op is-int)
(cond
((eq? op '+) +)
((eq? op '*) *)
((eq? op '-) -)
((and (eq? op '/) is-int) quotient)
((eq? op '/) /)
((and (eq? op '%) is-int) modulo)
((eq? op '%) (error "modulo % only works on integers"))
(else (error "invalid math operator")))))
; this function takes a boolean operator (ie: &&, ||, ...) and returns the actual function
; this translation is for 2 argument boolean expressions
(define boolean-operator-to-function-multi
(lambda (op)
(cond
((eq? op '&&) (lambda (arg1 arg2)
(and arg1 arg2)))
((eq? op '||) (lambda (arg1 arg2)
(or arg1 arg2)))
(else (error "unsupported boolean expression")))))
; Determines what type of operator we have
(define compare-operator-to-function-multi
(lambda (op)
(cond
((eq? op '==) equal?)
((eq? op '!=) (lambda (arg1 arg2) (not (equal? arg1 arg2))))
((eq? op '<) <)
((eq? op '>) >)
((eq? op '<=) <=)
((eq? op '>=) >=)
(else (error "invalid comparison operator")))))