Mercurial > repos > icfp2014
annotate code/lmc.tp @ 7:afd55b32549b
Implement support for local variables and top level functions
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 25 Jul 2014 13:47:21 -0700 |
parents | 0ab6eb5f0190 |
children | 494ef2e3a756 |
rev | line source |
---|---|
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
1 { |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
2 inst <- :_name _args { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
3 #{ |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
4 name <- _name |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
5 args <- _args |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
6 translateLabels <- :labelDict { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
7 missing <- #[] |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
8 foreach: args :idx arg { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
9 if: (object does: arg understand?: "isString?") && (arg isString?) { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
10 labelDict ifget: arg :translated { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
11 args set: idx translated |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
12 } else: { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
13 missing append: arg |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
14 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
15 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
16 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
17 missing |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
18 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
19 label <- "" |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
20 string <- { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
21 (if: label != "" { ";" . label . "\n " } else: { " " } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
22 ) . name . " " . (args join: " ") |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
23 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
24 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
25 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
26 _nextLabel <- 0 |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
27 _setLabel <- :inst { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
28 inst |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
29 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
30 prog <- #{ |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
31 instructions <- #[] |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
32 add <- :inst { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
33 instructions append: (_setLabel: inst) |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
34 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
35 makeLabel <- :suffix { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
36 num <- _nextLabel |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
37 _nextLabel <- _nextLabel + 1 |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
38 "" . num . "_" . suffix |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
39 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
40 labels <- dict hash |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
41 setLabel <- :name { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
42 labels set: name pc |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
43 _setLabel <- :inst { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
44 _setLabel <- :i { i } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
45 inst label!: name |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
46 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
47 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
48 pc <- { instructions length } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
49 print <- { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
50 foreach: instructions :idx i { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
51 missing <- i translateLabels: labels |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
52 if: (missing length) > 0 { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
53 error: "Undefined labels " . (missing join: ", ") . " at address " . idx |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
54 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
55 print: (string: i) . "\n" |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
56 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
57 |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
58 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
59 } |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
60 error <- :msg { |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
61 (file stderr) write: "Error - " . msg . "\n" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
62 } |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
63 |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
64 _exprHandlers <- dict hash |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
65 |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
66 compileExpr:syms <- :expr :syms { |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
67 _exprHandlers ifget: (expr nodeType) :handler { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
68 handler: expr syms |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
69 } else: { |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
70 error: "Unhandled node type " . (expr nodeType) |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
71 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
72 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
73 |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
74 _exprHandlers set: (ast intlit) :expr syms { |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
75 prog add: (inst: "LDC" #[(expr val)]) |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
76 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
77 |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
78 _exprHandlers set: (ast sequence) :expr syms { |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
79 count <- 0 |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
80 foreach: (expr els) :idx el { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
81 compileExpr: el syms: syms |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
82 count <- count + 1 |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
83 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
84 if: (expr array?) { |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
85 count <- count - 1 |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
86 } else: { |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
87 prog add: (inst: "LDC" #[0]) |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
88 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
89 while: { count > 0} do: { |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
90 prog add: (inst: "CONS" #[]) |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
91 count <- count - 1 |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
92 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
93 } |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
94 |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
95 _opNames <- dict hash |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
96 _opNames set: "+" "ADD" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
97 _opNames set: "-" "SUB" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
98 _opNames set: "*" "MUL" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
99 _opNames set: "/" "DIV" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
100 _opNames set: "|" "CONS" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
101 _opNames set: "=" "CEQ" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
102 _opNames set: ">" "CGT" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
103 _opNames set: ">=" "CGTE" |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
104 |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
105 _exprHandlers set: (ast binary) :expr syms { |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
106 if: (expr op) = "|" { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
107 compileExpr: (expr left) syms: syms |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
108 compileExpr: (expr right) syms: syms |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
109 } else: { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
110 compileExpr: (expr right) syms: syms |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
111 compileExpr: (expr left) syms: syms |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
112 } |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
113 _opNames ifget: (expr op) :i { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
114 prog add: (inst: i #[]) |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
115 } else: { |
6 | 116 if: (expr op) = "<" { |
117 prog add: (inst: "CGTE" #[]) | |
118 prog add: (inst: "LDC" #[0]) | |
119 prog add: (inst: "CEQ" #[]) | |
120 } else: { | |
121 if: (expr op) = "<=" { | |
122 prog add: (inst: "CGT" #[]) | |
123 prog add: (inst: "LDC" #[0]) | |
124 prog add: (inst: "CEQ" #[]) | |
125 } else: { | |
126 error: "operator " . (expr op) . " is not supported" | |
127 } | |
128 } | |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
129 } |
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
130 } |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
131 |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
132 _funHandlers <- dict hash |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
133 _funHandlers set: "if:else" :args syms { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
134 compileExpr: (args value) syms: syms |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
135 args <- args tail |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
136 tlabel <- prog makeLabel: "true" |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
137 flabel <- prog makeLabel: "false" |
5
80e224fff567
Fix handling of true case in if:else
Michael Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
138 elabel <- prog makeLabel: "end" |
4
eaf0a014d18b
Use TSEL instead of SEL for if:else
Michael Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
139 prog add: (inst: "TSEL" #[ |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
140 tlabel |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
141 flabel |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
142 ]) |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
143 prog setLabel: tlabel |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
144 foreach: ((args value) expressions) :idx expr { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
145 compileExpr: expr syms: syms |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
146 } |
5
80e224fff567
Fix handling of true case in if:else
Michael Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
147 prog add: (inst: "LDC" #[1]) |
80e224fff567
Fix handling of true case in if:else
Michael Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
148 prog add: (inst: "TSEL" #[ |
80e224fff567
Fix handling of true case in if:else
Michael Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
149 elabel |
80e224fff567
Fix handling of true case in if:else
Michael Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
150 elabel |
80e224fff567
Fix handling of true case in if:else
Michael Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
151 ]) |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
152 args <- args tail |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
153 prog setLabel: flabel |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
154 foreach: ((args value) expressions) :idx expr { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
155 compileExpr: expr syms: syms |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
156 } |
5
80e224fff567
Fix handling of true case in if:else
Michael Pavone <pavone@retrodev.com>
parents:
4
diff
changeset
|
157 prog setLabel: elabel |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
158 } |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
159 _funHandlers set: "isInteger?" :args syms { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
160 compileExpr: (args value) syms: syms |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
161 prog add: (inst: "ATOM" #[]) |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
162 } |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
163 _funHandlers set: "value" :args syms { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
164 compileExpr: (args value) syms: syms |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
165 prog add: (inst: "CAR" #[]) |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
166 } |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
167 _funHandlers set: "tail" :args syms { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
168 compileExpr: (args value) syms: syms |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
169 prog add: (inst: "CDR" #[]) |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
170 } |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
171 _funHandlers set: "not" :args syms { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
172 compileExpr: (args value) syms: syms |
6 | 173 prog add: (inst: "LDC" #[0]) |
174 prog add: (inst: "CEQ" #[]) | |
175 } | |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
176 |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
177 _exprHandlers set: (ast call) :expr syms { |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
178 tc <- (expr tocall) |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
179 if: (tc nodeType) = (ast sym) { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
180 _funHandlers ifget: (tc name) :handler { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
181 handler: (expr args) syms |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
182 } else: { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
183 error: "function calls not implemented yet" |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
184 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
185 } else: { |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
186 error: "call expression to value not implemented yet - " . tc |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
187 } |
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
188 } |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
189 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
190 _exprHandlers set: (ast sym) :expr syms { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
191 syms ifDefined: (expr name) :info { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
192 frame <- if: (info isLocal?) { 0 } else: { info depth } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
193 prog add: (inst: "LD" #[ |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
194 frame |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
195 (info def) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
196 ]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
197 } else: { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
198 error: "symbol " . (expr name) . " is not defined" |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
199 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
200 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
201 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
202 _exprHandlers set: (ast assignment) :expr syms { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
203 sym <- expr to |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
204 syms ifDefined: (sym name) :info { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
205 frame <- if: (info isLocal?) { 0 } else: { info depth } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
206 compileExpr: (expr assign) syms: syms |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
207 prog add: (inst: "ST" #[ |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
208 frame |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
209 (info def) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
210 ]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
211 } else: { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
212 error: "symbol " . (sym name) . " is not defined" |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
213 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
214 } |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
215 #{ |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
216 compile <- :code { |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
217 res <- parser top: code |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
218 if: res { |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
219 outer <- res yield |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
220 functions <- dict hash |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
221 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
222 num <- (outer messages) length |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
223 syms <- symbols table |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
224 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
225 prog add: (inst: "DUM" #[num]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
226 slot <- 0 |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
227 foreach: (outer messages) :idx msg { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
228 if: (msg nodeType) = (ast assignment) { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
229 def <- msg assign |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
230 sym <- (msg to) name |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
231 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
232 if: (def nodeType) = (ast lambda) { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
233 prog add: (inst: "LDF" #[sym]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
234 functions set: sym def |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
235 } else: { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
236 compileExpr: def syms: syms |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
237 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
238 syms define: sym slot |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
239 slot <- slot + 1 |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
240 } else: { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
241 error: "Only assignments are allowed at the top level" |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
242 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
243 } |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
244 prog add: (inst: "LDF" #["main"]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
245 prog add: (inst: "TRAP" #[num]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
246 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
247 foreach: functions :fname fun { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
248 prog setLabel: fname |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
249 argsyms <- symbols tableWithParent: syms |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
250 foreach: (fun args) :idx el { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
251 argsyms define: (if: (el startsWith?: ":") { el from: 1 } else: { el }) idx |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
252 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
253 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
254 slot <- 0 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
255 locsyms <- symbols tableWithParent: argsyms |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
256 foreach: (fun expressions) :idx expr { |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
257 if: (expr nodeType) = (ast assignment) { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
258 locsyms ifDefined: ((expr to) name) :sym { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
259 //already defined, nothing to do here |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
260 } else: { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
261 locsyms define: ((expr to) name) slot |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
262 slot <- slot + 1 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
263 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
264 } |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
265 } |
7
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
266 fsyms <- if: slot > 0 { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
267 //allocate frame for locals |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
268 prog add: (inst: "DUM" #[slot]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
269 i <- 0 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
270 while: { i < slot } do: { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
271 prog add: (inst: "LDC" #[0]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
272 i <- i + 1 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
273 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
274 prologue_end <- prog makeLabel: fname . "_real" |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
275 prog add: (inst: "LDF" #[prologue_end]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
276 prog add: (inst: "TRAP" #[slot]) |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
277 prog setLabel: prologue_end |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
278 locsyms |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
279 } else: { argsyms } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
280 |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
281 foreach: (fun expressions) :idx expr { |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
282 compileExpr: expr syms: fsyms |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
283 } |
afd55b32549b
Implement support for local variables and top level functions
Michael Pavone <pavone@retrodev.com>
parents:
6
diff
changeset
|
284 prog add: (inst: "RTN" #[]) |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
285 } |
3
a9a2ad99adfb
Rework lmc a bit to support labels in generated code. Add support for certain special funcall expressions, namely: if:else, isInteger? value and tail which translate to SEL ATOM CAR and CDR respectively
Michael Pavone <pavone@retrodev.com>
parents:
2
diff
changeset
|
286 print: prog |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
287 } else: { |
2
71e8d638da5c
Add operator support to lmc
Michael Pavone <pavone@retrodev.com>
parents:
1
diff
changeset
|
288 error: "Parse failed!" |
1
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
289 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
290 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
291 |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
292 compileFile <- :filename { |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
293 f <- file open: filename |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
294 compile: (f readAll) |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
295 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
296 |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
297 main <- :args { |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
298 if: (args length) > 1 { |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
299 compileFile: (args get: 1) |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
300 } else: { |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
301 print: "Usage lmc FILE\n" |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
302 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
303 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
304 } |
68d1447bfdbe
Support for compiling lists, tuples and integer literals in lmc. Added small test lm program for exercising compiler.
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
305 } |