diff options
| author | Shagun Agrawal <agrawalshagun07@gmail.com> | 2024-08-17 01:32:17 +0530 |
|---|---|---|
| committer | Shagun Agrawal <agrawalshagun07@gmail.com> | 2024-08-17 01:32:17 +0530 |
| commit | 9867129bf45bb620ca56715c74243de3bde9de3c (patch) | |
| tree | c346c543ea9ec6d5d9440535e0b3406f5662d913 /src | |
| parent | c795bb12c210e8d54060cf7766c97f101063c99a (diff) | |
Fix changes for ch2
Diffstat (limited to 'src')
| -rw-r--r-- | src/cljcc/emit.clj | 2 | ||||
| -rw-r--r-- | src/cljcc/tacky.clj | 49 |
2 files changed, 6 insertions, 45 deletions
diff --git a/src/cljcc/emit.clj b/src/cljcc/emit.clj index d18edb3..514cd45 100644 --- a/src/cljcc/emit.clj +++ b/src/cljcc/emit.clj @@ -48,7 +48,7 @@ (let [operand (operand-emit (:operand instruction)) assembly-operator (condp = (:unary-operator instruction) :complement "notl" - :negate "negl" + :hyphen "negl" (throw (AssertionError. (str "Invalid unary operator: " instruction))))] [(format " %s %s" assembly-operator operand)])) 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)) - ()) |
