aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/tacky.clj
diff options
context:
space:
mode:
authorShagun Agrawal <agrawalshagun07@gmail.com>2024-08-17 01:32:17 +0530
committerShagun Agrawal <agrawalshagun07@gmail.com>2024-08-17 01:32:17 +0530
commit9867129bf45bb620ca56715c74243de3bde9de3c (patch)
treec346c543ea9ec6d5d9440535e0b3406f5662d913 /src/cljcc/tacky.clj
parentc795bb12c210e8d54060cf7766c97f101063c99a (diff)
Fix changes for ch2
Diffstat (limited to 'src/cljcc/tacky.clj')
-rw-r--r--src/cljcc/tacky.clj49
1 files changed, 5 insertions, 44 deletions
diff --git a/src/cljcc/tacky.clj b/src/cljcc/tacky.clj
index bc0d176..1d0af27 100644
--- a/src/cljcc/tacky.clj
+++ b/src/cljcc/tacky.clj
@@ -83,10 +83,10 @@
{:val (constant-instruction (:value e))})
(defn- unary-expr-handler [e]
- (let [inner (expression-handler (nth e 2))
+ (let [inner (expression-handler (:value e))
dst (variable)
src (:val inner)
- unary-operator (unary-operator (second (second e)))
+ unary-operator (:unary-operator e)
instruction (unary-instruction unary-operator src dst)]
{:val dst
:instructions (flatten [(:instructions inner) instruction])}))
@@ -103,14 +103,14 @@
:instructions (flatten [(:instructions e1) (:instructions e2) instruction])}))
(defn- expression-handler [e]
- (when-let [exp-type (:type e)]
+ (when-let [exp-type (:exp-type e)]
(cond
(= exp-type :constant-exp) (constant-expr-handler e)
(= exp-type :unary-exp) (unary-expr-handler e)
(binary-expr? exp-type) (binary-expr-handler e))))
(defn- exp-instructions [exp]
- (expression-handler (:value exp)))
+ (expression-handler exp))
(defn- ret-instructions [exp]
(let [e (exp-instructions exp)
@@ -135,7 +135,7 @@
(pp/pprint
(tacky-generate
- (p/parse (l/lex "int main(void) {return 1;}"))))
+ (p/parse (l/lex "int main(void) {return -(~1);}"))))
(pp/pprint
(tacky-generate
@@ -151,43 +151,4 @@
(tacky-generate
(p/parse "int main(void) {return 1 + 2 + -3 + -(4 + 5);}")))
- (pp/pprint
- (exp-instructions [:exp [:constant "2"]]))
-
- (pp/pprint
- (exp-instructions [:exp [:constant "2"]]))
-
- (pp/pprint
- (exp-instructions [:exp [:unop-exp [:unop "-"] [:exp [:constant "2"]]]]))
-
- (def ex-exp
- [:exp
- [:unop-exp
- [:unop "-"]
- [:exp
- [:unop-exp
- [:unop "~"]
- [:exp [:unop-exp [:unop "-"] [:exp [:constant "8"]]]]]]]])
-
- (def ex-ret
- [:statement "return"
- [:exp
- [:unop-exp
- [:unop "-"]
- [:exp
- [:unop-exp
- [:unop "~"]
- [:exp [:unop-exp [:unop "-"] [:exp [:constant "8"]]]]]]]]])
-
- (pp/pprint
- (exp-instructions ex-exp))
-
- (def exprg
- "int main(void) {
- return -(~(-8));
- }")
-
- (pp/pprint
- (ret-instructions ex-ret))
-
())