Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rework codegen to put all definitions in #main
  • Loading branch information
jeaye committed Dec 28, 2015
1 parent dc42bf8 commit fcdf4de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
5 changes: 1 addition & 4 deletions .color_coded
@@ -1,5 +1,2 @@
-std=c++1y
-Itest/include
-Iinclude
-Ilib/jtl/include
-Ilib/jest/include
-Ibackend/c++/include
3 changes: 2 additions & 1 deletion bin/jank
Expand Up @@ -10,8 +10,9 @@ lein run $@ > $tmp/jank-generated.hpp 2>&1
ret=$?
if [ $ret -eq 0 ];
then
set -e
g++ -std=c++14 -I$here/../backend/c++/include -I$tmp $here/../backend/c++/src/main.cpp
ret=$?
set +e
else
cat $tmp/jank-generated.hpp
fi
Expand Down
35 changes: 13 additions & 22 deletions src/jank/codegen/c++.clj
Expand Up @@ -33,13 +33,10 @@
"}"))

(defmethod codegen-impl :binding-definition [current]
; Special case for function definitions
(if (= (first (nth current 2)) :lambda-definition)
(codegen-impl (update-in current [0] (fn [x] :function-definition)))
(str "auto "
(codegen-impl (second current))
"="
(codegen-impl (nth current 2)))))
(str "auto "
(codegen-impl (second current))
"="
(codegen-impl (nth current 2))))

(defmethod codegen-impl :function-call [current]
(str (codegen-impl (second current)) ; Name
Expand Down Expand Up @@ -110,18 +107,12 @@
(codegen-assert false (str "no codegen for '" current "'")))

(defn codegen [ast]
(let [[definitions expressions] (util/partition-definitions (:cells ast))]
; Generate all top-level definitions
(doseq [current definitions]
(util/print-statement (util/end-statement (codegen-impl current))))

; Build the main function
(util/print-statement
(codegen-impl
[:binding-definition
[:identifier "#main"]
(apply (partial vector
:lambda-definition
[:argument-list]
[:return-list])
expressions)]))))
(util/print-statement
(codegen-impl
[:function-definition
[:identifier "#main"]
(apply (partial vector
:lambda-definition
[:argument-list]
[:return-list])
(:cells ast))])))

0 comments on commit fcdf4de

Please sign in to comment.