Commits

Tero Koskinen  committed 473c442

Some utility functions to print the statements.

  • Participants
  • Parent commits 8aaf43e

Comments (0)

Files changed (1)

File src/ir/print_utils.clj

+;
+; Copyright (c) 2012 Tero Koskinen <tero.koskinen@iki.fi>
+;
+; Permission to use, copy, modify, and distribute this software for any
+; purpose with or without fee is hereby granted, provided that the above
+; copyright notice and this permission notice appear in all copies.
+;
+; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+; OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;
+
+(ns ir.print-utils
+  (:require [clojure.string] [clojure.set])
+)
+
+(defn print-term [term]
+  (case (:name term)
+    "number" (print (:value term))
+    "symbol" (print (:value term))
+    (print term)
+  )
+  (if (:use_offset term) (do (print ":") (print (:offset term))))
+)
+
+(defn print-binop-expr [op expression]
+  (print-term (:left expression))
+  (print (format " %s " op))
+  (print-term (:right expression))
+)
+
+(defmulti print-expr (fn [expression] (:name expression)))
+
+(defmethod print-expr "term" [expression]
+  (print-term (:term expression))
+)
+
+(defmethod print-expr "plus" [expression]
+  (print-binop-expr "+" expression)
+)
+
+(defmethod print-expr "minus" [expression] (print-binop-expr "-" expression))
+(defmethod print-expr "mult" [expression] (print-binop-expr "*" expression))
+(defmethod print-expr "div" [expression] (print-binop-expr "/" expression))
+(defmethod print-expr "and" [expression] (print-binop-expr "and" expression))
+(defmethod print-expr "or" [expression] (print-binop-expr "or" expression))
+(defmethod print-expr "equals" [expression] (print-binop-expr "=" expression))
+(defmethod print-expr "not-equals" [expression]
+  (print-binop-expr "/=" expression))
+(defmethod print-expr "bigger-than" [expression]
+  (print-binop-expr ">" expression))
+(defmethod print-expr "smaller-than" [expression]
+  (print-binop-expr "<" expression))
+
+(defmethod print-expr "convert" [expression]
+  (do
+    (print "@" (:to_type expression) "")
+    (print-term (:term expression))))
+
+(defmethod print-expr "reference" [expression]
+  (do
+    (print "& ")
+    (print-term (:term expression))
+  )
+)
+
+(defmethod print-expr :default [expression]
+  (println "   " expression))
+
+(defmulti print-stmt (fn [statement] (:name statement))
+)
+
+(defmethod print-stmt "assign" [statement]
+  (do
+    (print "   ")
+    (if (:deref (:target statement))
+      (print "* "))
+    (print-term (:term (:target statement)))
+    (print " := ")
+    (print-expr (:expression statement))
+    (println ";")
+  )
+)
+
+(defmethod print-stmt "return" [statement]
+  (println "   RETURN;")
+)
+
+(defmethod print-stmt "label" [statement]
+  (println (format "%s:" (:label statement))))
+
+(defmethod print-stmt "goto" [statement]
+  (println (format "   GOTO %s;" (:target statement))))
+
+(defmethod print-stmt "nop" [statement]
+  (println "   NOP;")
+)
+
+(defmethod print-stmt "call" [statement]
+  (println (format "   CALL %s ...;" (:fname statement)))
+)
+
+(defmethod print-stmt "if" [statement]
+  (do
+    (print "   IF ")
+    (print-expr (:expression statement))
+    (println (format " GOTO %s;" (:target statement)))))
+
+(defmethod print-stmt :default [statement]
+  (println "STMT:" statement)
+)