source: applications/trunk/LFC/ToASR/ToASR.rf @ 3893

Last change on this file since 3893 was 3893, checked in by orlov, 12 years ago
  • Draft version of compiler to ASR.
File size: 3.1 KB
Line 
1$module ToASR;
2
3$use Access Arithm Convert Box List StdIO;
4$use Parser GrammarParser;
5
6$func Comp e.ast = e.asr;
7Comp {
8  (FUNC t.name (e.func_type) (e.var_decls) e.sentences) e.rest =
9    e.func_type : e.types t,
10    <Store &Idx 0>,
11    <Map &GenerArg (e.types)> :: e.args,
12    (LOCAL FUNC <RefName t.name> (e.args) ((EVAR)) (BRANCH (LEFT e.args) (CUTALL)
13      <Map &CallEarley (e.args)>
14      (ALT <Map &CompSentence (e.args) (e.var_decls) (e.sentences)>)
15    ))
16    <Comp e.rest>;
17  t1 e.rest = t1 <Comp e.rest>;
18  /*empty*/ = /*empty*/;
19};
20
21$box Idx;
22
23$func GenerArg t.type = t.var;
24GenerArg (TYPE (e.name)) =
25  <Get &Idx> : s.n,
26  <Store &Idx <Add s.n 1>>,
27  (EVAR (s.n e.name));
28
29$func CallEarley t.var = e.call;
30CallEarley (EVAR (s.n e.name)) =
31  (RESULT (CALL (Earley Earley) (TYPE (e.name)) (EVAR (s.n e.name))))
32  (FORMAT (EVAR (s.n)));
33
34$func CompSentence (e.args) (e.var_decls) t.sentence = e.branch;
35CompSentence (e.args) (e.var_decls) (SENTENCE t.name (e.patterns) e.exp) =
36  <Store &Idx 0>,
37  (BRANCH <Map &CompPattern (e.args) (e.var_decls) (e.patterns)> (CUTALL) (RESULT <Map &CompTerm (e.exp)>));
38
39$func CompPattern (e.args) (e.var_decls) t.pattern = e.pattern;
40CompPattern (e.args) (e.var_decls) (e.pattern) =
41  <Get &Idx> : s.n,
42  <Store &Idx <Add s.n 1>>,
43  <L s.n e.args> : (EVAR (s.n e.type_name)),
44  (RESULT (EVAR (s.n)))
45  (LEFT <FreshEVAR> ((TYPE (e.type_name) (EVAR (0 s.n))) <Map &VarType (e.var_decls) (e.pattern)> DOT 0) <FreshEVAR>)
46  (RESULT (CALL (Earley RemoveParens) (EVAR (0 s.n))))
47  (LEFT <Map &GetVar (e.pattern)>);
48
49$func VarType (e.var_decls) t.pat = t.pat;
50VarType (e.var_decls) t.pat, {
51  t.pat : (VAR t.name) =
52    e.var_decls : e (e t.pat e t.type) e = t.type;
53  t.pat;
54};
55
56$func GetVar t.pat = e.var;
57GetVar {
58  (VAR t.name) = (PAREN (EVAR t.name));
59  t = ;
60};
61
62$func CompTerm t.term = e.asr;
63CompTerm {
64  (UNDEFINED) = (ERROR (BRANCH (RESULT UNDEFINED)));
65  s.char = s.char;
66  (VAR t.name) = (EVAR t.name);
67  (CALL t.name e.args) = (CALL <RefName t.name> <Map &CompArg (e.args)>);
68  (IF (e.cond) (e.exp_true) (e.exp_false)) = (ALT
69    (BRANCH (RESULT <Map &CompTerm (e.cond)>) (LEFT 'True') (CUTALL) <Map &CompTerm (e.exp_true)>)
70    (BRANCH <Map &CompTerm (e.exp_false)>)
71  );
72  t1 = t1;
73};
74
75$func CompArg t.arg = e.asr;
76CompArg (e.exp) = (PAREN <Map &CompTerm (e.exp)>);
77
78$box FreeIdx;
79$func FreshEVAR = t.var;
80FreshEVAR = {
81  <Get &FreeIdx> : s.n =
82    <Store &FreeIdx <Add s.n 1>>,
83    (EVAR (s.n));
84  <Store &FreeIdx 101>,
85    (EVAR (100));
86};
87
88$func GenerMain t.fname e.grammar = t.func;
89GenerMain t.fname e.grammar =
90  (LOCAL FUNC <RefName ('Main')> () ((EVAR)) (BRANCH (LEFT) (CUTALL)
91    (RESULT (CALL (Earley Init) e.grammar))
92    (RESULT (CALL <RefName t.fname>))
93  ));
94
95$box ModuleName;
96
97$func RefName t.name = t.ref_name;
98RefName (e.chars) = (<Get &ModuleName> <ToWord e.chars>);
99
100$func Main = e;
101Main =
102  <ParseFile "samples.lfc"> :: e.ast,
103  <ParseGrammarFile "samples.g"> :: e.grammar,
104  <Store &ModuleName "samples">,
105  <WriteLn (MODULE (<Get &ModuleName>) <Comp e.ast> <GenerMain ('isBit') e.grammar>)>;
Note: See TracBrowser for help on using the repository browser.