diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cljcc/compiler.clj | 2 | ||||
| -rw-r--r-- | src/cljcc/lexer.clj | 2 | ||||
| -rw-r--r-- | src/cljcc/parser.clj | 14 | ||||
| -rw-r--r-- | src/cljcc/token.clj | 66 |
4 files changed, 57 insertions, 27 deletions
diff --git a/src/cljcc/compiler.clj b/src/cljcc/compiler.clj index 7d2a93b..4862d95 100644 --- a/src/cljcc/compiler.clj +++ b/src/cljcc/compiler.clj @@ -108,8 +108,6 @@ (let [transformer-fn ((:type inst) tacky->assembly-transformers)] (transformer-fn inst))) -(declare tacky-ex) - (defn- find-pseudo-values [instructions] (distinct (remove diff --git a/src/cljcc/lexer.clj b/src/cljcc/lexer.clj index 9922887..8525441 100644 --- a/src/cljcc/lexer.clj +++ b/src/cljcc/lexer.clj @@ -9,8 +9,6 @@ :line 1 :col 1}) -(str \1 nil) - (defn lex ([source] (lex source 0 (lexer-ctx))) diff --git a/src/cljcc/parser.clj b/src/cljcc/parser.clj index e8f1791..f0ee777 100644 --- a/src/cljcc/parser.clj +++ b/src/cljcc/parser.clj @@ -39,18 +39,14 @@ (defn- parse-factor [[{kind :kind :as token} :as tokens]] (cond (= kind :number) [(constant-exp-node (:literal token)) (rest tokens)] - (contains? - #{:complement :hyphen} kind) (let [operator kind - [e rst] (parse-factor (rest tokens))] - [(unary-exp-node operator e) rst]) + (t/unary-op? kind) (let [op kind + [e rst] (parse-factor (rest tokens))] + [(unary-exp-node op e) rst]) (= kind :left-paren) (let [[e rst] (parse-exp (rest tokens)) [_ rst] (expect :right-paren rst)] [e rst]) :else (throw (ex-info "Parser Error. Malformed token." {:token token})))) -(defn- precedence [kind] - (kind t/bin-ops)) - (defn- parse-exp ([tokens] (parse-exp tokens 0)) @@ -58,8 +54,8 @@ (loop [[left rst] (parse-factor tokens) tokens rst] (let [[{kind :kind :as _token} :as tokens] tokens] - (if (and (contains? t/bin-ops kind) (>= (precedence kind) min-prec)) - (let [[right rst] (parse-exp (rest tokens) (+ (precedence kind) 1))] + (if (and (t/binary-op? kind) (>= (t/precedence kind) min-prec)) + (let [[right rst] (parse-exp (rest tokens) (+ (t/precedence kind) 1))] (recur [(binary-exp-node left right kind)] rst)) [left tokens]))))) diff --git a/src/cljcc/token.clj b/src/cljcc/token.clj index 9136631..bea1ebd 100644 --- a/src/cljcc/token.clj +++ b/src/cljcc/token.clj @@ -11,24 +11,25 @@ :right-paren ;; operators - :bitwise-not - :multiply :divide :remainder - :plus :minus - + :logical-not + :logical-and + :logical-or + :equal-to + :not-equal-to + :less-than + :greater-than + :less-than-equal-to + :greater-than-equal-to :bitwise-left-shift :bitwise-right-shift - :ampersand - :bitwise-xor - :bitwise-or - :negate :assignemnt :increment @@ -42,21 +43,49 @@ :kw-int :kw-void}) -(def chrs - #{}) +(def unary-ops + #{:logical-not + :complement + :hyphen}) + +(defn unary-op? [op] + (contains? unary-ops op)) (def bin-ops - "Binary operanrs and their precedence." + "Binary operands and their precedence." {:multiply 100 :divide 100 :remainder 100 + :plus 90 :hyphen 90 + :bitwise-left-shift 80 :bitwise-right-shift 80 - :ampersand 70 - :bitwise-xor 60 - :bitwise-or 50}) + + :less-than 70 + :less-than-equal-to 70 + :greater-than 70 + :greater-than-equal-to 70 + + :equal-to 60 + :not-equal-to 60 + + :ampersand 50 + + :bitwise-xor 40 + + :bitwise-or 30 + + :logical-and 20 + + :logical-or 10}) + +(defn binary-op? [op] + (contains? bin-ops op)) + +(defn precedence [op] + (op bin-ops)) (def chrs-kind-map {\( :left-paren @@ -68,6 +97,15 @@ "++" :increment "<<" :bitwise-left-shift ">>" :bitwise-right-shift + \! :logical-not + "&&" :logical-and + "||" :logical-or + "==" :equal-to + "!=" :not-equal-to + \< :less-than + \> :greater-than + "<=" :less-than-equal-to + ">=" :greater-than-equal-to \^ :bitwise-xor \| :bitwise-or \& :ampersand |
