Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Python target and made some more changes to make _t-univ.scm mo…

…re generic.
  • Loading branch information...
commit ece0d6a198e29983052f2f0b939c53874038a4fe 1 parent 358dba4
@gnuvince gnuvince authored
View
12 gsc/_t-univ.scm
@@ -13,6 +13,7 @@
(include "target_js.scm")
(include "target_php.scm")
+(include "target_py.scm")
;;;----------------------------------------------------------------------------
;;
@@ -72,7 +73,8 @@
targ
(case (target-name targ)
((js) js-generator)
- ((php) php-generator)))
+ ((php) php-generator)
+ ((python) py2-generator)))
#f)
@@ -481,10 +483,13 @@
(gen "lbl" num "_" (scheme-id->c-id ns)))
(define (prim-applic ctx prim opnds test?)
+ (define targ-gen (target-generator (ctx-target ctx)))
(case (string->symbol (proc-obj-name prim))
((##not)
- (gen (translate-gvm-opnd ctx (list-ref opnds 0)) " === false"))
+ (targ-gen 'equal
+ (translate-gvm-opnd ctx (list-ref opnds 0))
+ (targ-gen 'false)))
(else
(compiler-internal-error
@@ -494,7 +499,8 @@
(define (runtime-system targ)
(case (target-name targ)
((js) js-runtime)
- ((php) php-runtime)))
+ ((php) php-runtime)
+ ((python) py2-runtime)))
(define (entry-point ctx main-proc)
(let ((targ (ctx-target ctx)))
View
5 gsc/target_js.scm
@@ -28,6 +28,9 @@
else_
"}\n"))
(define (return expr) (gen "return " expr ";\n"))
+ (define (equal a b)
+ (gen a " === " b))
+ (define (false) "false")
(let ((fn (case msg
((entry-point) entry-point)
@@ -49,6 +52,8 @@
((apply) apply_)
((if) if_)
((return) return)
+ ((false) false)
+ ((equal) equal)
(else
(compiler-internal-error "unknown message" msg)))))
(apply fn args)))
View
5 gsc/target_php.scm
@@ -36,6 +36,9 @@
else_
"}\n"))
(define (return expr) (gen "return " expr ";\n"))
+ (define (equal a b)
+ (gen a " === " b))
+ (define (false) "false")
@@ -59,6 +62,8 @@
((apply) apply_)
((if) if_)
((return) return)
+ ((false) false)
+ ((equal) equal)
(else
(compiler-internal-error "unknown message" msg)))))
(apply fn args)))
View
164 gsc/target_py.scm
@@ -0,0 +1,164 @@
+(define py2-generator
+ (let ((indent 0))
+ (define (py-indent) (set! indent (+ indent 1)) "")
+ (define (py-dedent) (set! indent (- indent 1)) "")
+ (define (py-newline) (gen "\n" (make-string (* 4 indent) #\space)))
+
+ (define (entry-point name)
+ (gen "run(" name ")" (py-newline)))
+
+ (define (reg n) (gen "reg[" n "]"))
+ (define (stk n) (gen "stack[sp+" n "]"))
+ (define (glo name) (gen "glo['" name "']"))
+ (define (clo lval index) (gen lval "[" index "]"))
+ (define (lbl name) name)
+ (define (adjust-sp offset)
+ (gen "sp += " offset (py-newline)))
+ (define (void) "None")
+ (define (proc-obj p) p)
+ (define (label-start name)
+ (gen "def " name "():"
+ (py-indent)
+ (py-newline)
+ "global glo" (py-newline)
+ "global reg" (py-newline)
+ "global stack" (py-newline)
+ "global sp" (py-newline)
+ "global nargs" (py-newline)))
+ (define (label-stop)
+ (py-dedent)
+ (py-newline))
+ (define (var-name name) name)
+ (define (comment . xs) (gen "# " xs (py-newline)))
+ (define (die msg)
+ (gen "raise Exception('" msg "')" (py-newline)))
+ (define (copy a b) (gen a " = " b (py-newline)))
+ (define (apply_ a b) (gen a " = " b (py-newline)))
+ (define (return expr) (gen "return " expr (py-newline)))
+ (define (if_ cond_ then else_)
+ (gen "if " cond_ ":"
+ (py-indent)
+ (py-newline)
+ then
+ (py-dedent)
+ (py-newline)
+ "else:"
+ (py-indent)
+ (py-newline)
+ else_
+ (py-dedent)
+ (py-newline)))
+ (define (narg-check n)
+ (if_ (gen (var-name 'nargs) " != " n)
+ (die "incorrect number of arguments")
+ (gen "pass")))
+ (define (equal a b)
+ (gen a " == " b))
+ (define (false) "False")
+
+
+
+ (lambda (msg . args)
+ (let ((fn (case msg
+ ((entry-point) entry-point)
+ ((reg) reg)
+ ((stk) stk)
+ ((glo) glo)
+ ((clo) clo)
+ ((lbl) lbl)
+ ((adjust-sp) adjust-sp)
+ ((void) void)
+ ((proc-obj) proc-obj)
+ ((label-start) label-start)
+ ((label-stop) label-stop)
+ ((var-name) var-name)
+ ((comment) comment)
+ ((narg-check) narg-check)
+ ((die) die)
+ ((copy) copy)
+ ((apply) apply_)
+ ((if) if_)
+ ((return) return)
+ ((false) false)
+ ((equal) equal)
+ (else
+ (compiler-internal-error "unknown message" msg)))))
+ (apply fn args)))))
+
+(define py2-runtime
+#<<EOF
+glo = {}
+reg = [None for i in xrange(5)]
+#stack = []
+stack = {0:None}
+sp = -1
+nargs = 0
+
+def lbl1_fx_3c_(): # fx<
+ global glo
+ global reg
+ global stack
+ global sp
+ global nargs
+ if nargs != 2:
+ raise Exception("wrong number of arguments")
+ reg[1] = reg[1] < reg[2]
+ return reg[0]
+glo["fx<"] = lbl1_fx_3c_
+
+def lbl1_fx_2a_(): # fx*
+ global glo
+ global reg
+ global stack
+ global sp
+ global nargs
+ if nargs != 2:
+ raise Exception("wrong number of arguments")
+ reg[1] = reg[1] * reg[2]
+ return reg[0]
+glo["fx*"] = lbl1_fx_2a_
+
+
+def lbl1_fx_2b_(): # fx+
+ global glo
+ global reg
+ global stack
+ global sp
+ global nargs
+ if nargs != 2:
+ raise Exception("wrong number of arguments")
+ reg[1] = reg[1] + reg[2]
+ return reg[0]
+glo["fx+"] = lbl1_fx_2b_
+
+def lbl1_fx_2d_(): # fx-
+ global glo
+ global reg
+ global stack
+ global sp
+ global nargs
+ if nargs != 2:
+ raise Exception("wrong number of arguments")
+ reg[1] = reg[1] - reg[2]
+ return reg[0]
+glo["fx-"] = lbl1_fx_2d_
+
+def lbl1_print(): # print
+ global glo
+ global reg
+ global stack
+ global sp
+ global nargs
+ if nargs != 1:
+ raise Exception("wrong number of arguments")
+ print reg[1]
+ return reg[0]
+glo["print"] = lbl1_print
+
+
+def run(pc):
+ while pc is not None:
+ pc = pc()
+
+EOF
+)
Please sign in to comment.
Something went wrong with that request. Please try again.