diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-11 21:57:44 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-11 21:57:44 -0700 |
commit | 7c90fe68b557d32b65955ffd18e2e79585a7282b (patch) | |
tree | 1713fa7b087c83cd68669a46389bd3b35ae8b840 /src | |
parent | 78118e3b515ea5268205b4f56d914900a9535c88 (diff) | |
download | stage-7c90fe68b557d32b65955ffd18e2e79585a7282b.tar.gz stage-7c90fe68b557d32b65955ffd18e2e79585a7282b.tar.bz2 stage-7c90fe68b557d32b65955ffd18e2e79585a7282b.tar.xz stage-7c90fe68b557d32b65955ffd18e2e79585a7282b.zip |
Getting closer.
Real turing-complete code is coming soon
Diffstat (limited to 'src')
-rw-r--r-- | src/parser.l | 37 | ||||
-rw-r--r-- | src/parser.y | 56 |
2 files changed, 77 insertions, 16 deletions
diff --git a/src/parser.l b/src/parser.l index d5ff735..dff6507 100644 --- a/src/parser.l +++ b/src/parser.l | |||
@@ -1,5 +1,6 @@ | |||
1 | %{ | 1 | %{ |
2 | #include <stdint.h> | 2 | #include <stdint.h> |
3 | #include <bu/string.h> | ||
3 | #include "parser.tab.h" | 4 | #include "parser.tab.h" |
4 | %} | 5 | %} |
5 | 6 | ||
@@ -7,7 +8,7 @@ | |||
7 | 8 | ||
8 | %x sitname | 9 | %x sitname |
9 | %x comment | 10 | %x comment |
10 | %x tdqstr tsqstr | 11 | %x dqstr tdqstr tsqstr |
11 | %% | 12 | %% |
12 | 13 | ||
13 | [-{}<>=+/*,();] { return yytext[0]; } | 14 | [-{}<>=+/*,();] { return yytext[0]; } |
@@ -25,18 +26,46 @@ else { return tokElse; } | |||
25 | command { return tokCommand; } | 26 | command { return tokCommand; } |
26 | goto { return tokGoto; } | 27 | goto { return tokGoto; } |
27 | 28 | ||
29 | true { yylval.bValue = true; return tokBool; } | ||
30 | false { yylval.bValue = false; return tokBool; } | ||
31 | null { return tokNull; } | ||
32 | |||
28 | "<<" { BEGIN( sitname ); } | 33 | "<<" { BEGIN( sitname ); } |
29 | <sitname>[- a-zA-Z0-9]+ { printf("situation: %s\n", yytext ); return tokSituationName; } | 34 | <sitname>[- a-zA-Z0-9]+ { yylval.sValue = new Bu::String( yytext ); return tokSituationName; } |
30 | <sitname>">>" { BEGIN( INITIAL ); } | 35 | <sitname>">>" { BEGIN( INITIAL ); } |
31 | <sitname>. REJECT; | 36 | <sitname>. REJECT; |
32 | 37 | ||
33 | "//"[^\n]* {} | 38 | "//"[^\n]* {} |
34 | 39 | ||
35 | "/\*" { BEGIN( comment ); } | 40 | "/\*" { BEGIN( comment ); } |
36 | <comment>"\*/" { BEGIN( INITIAL ); } | 41 | <comment>"\*/" { BEGIN( INITIAL ); } |
37 | <comment>. {} | 42 | <comment>. {} |
38 | 43 | ||
39 | [a-zA-Z_][a-zA-Z0-9_]* { return tokIdent; } | 44 | [a-zA-Z_][a-zA-Z0-9_]* { yylval.sValue = new Bu::String( yytext ); return tokIdent; } |
45 | |||
46 | [1-9][0-9]* { | ||
47 | yylval.iValue = strtoll( yytext, NULL, 10 ); | ||
48 | return tokInt; | ||
49 | } | ||
50 | |||
51 | ([1-9][0-9]*)?\.[0-9]* { | ||
52 | yylval.dValue = strtod( yytext, NULL ); | ||
53 | return tokFloat; | ||
54 | } | ||
55 | |||
56 | \"\"\" { BEGIN( tdqstr ); yylval.sValue = new Bu::String(); } | ||
57 | <tdqstr>[^"]+ { (*yylval.sValue) += yytext; } | ||
58 | <tdqstr>\" { (*yylval.sValue) += yytext; } | ||
59 | <tdqstr>\"\"\" { BEGIN( INITIAL ); return tokString; } | ||
60 | |||
61 | \'\'\' { BEGIN( tsqstr ); yylval.sValue = new Bu::String(); } | ||
62 | <tsqstr>[^']+ { (*yylval.sValue) += yytext; } | ||
63 | <tsqstr>\' { (*yylval.sValue) += yytext; } | ||
64 | <tsqstr>\'\'\' { BEGIN( INITIAL ); return tokString; } | ||
65 | |||
66 | \" { BEGIN( dqstr ); yylval.sValue = new Bu::String(); } | ||
67 | <dqstr>[^"]+ { (*yylval.sValue) += yytext; } | ||
68 | <dqstr>\" { BEGIN( INITIAL ); return tokString; } | ||
40 | 69 | ||
41 | [ \t\n\r]+ {} | 70 | [ \t\n\r]+ {} |
42 | 71 | ||
diff --git a/src/parser.y b/src/parser.y index b3da848..9fb2be9 100644 --- a/src/parser.y +++ b/src/parser.y | |||
@@ -1,6 +1,7 @@ | |||
1 | %{ | 1 | %{ |
2 | #include <stdint.h> | 2 | #include <stdint.h> |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <bu/string.h> | ||
4 | 5 | ||
5 | int yylex(); | 6 | int yylex(); |
6 | void yyerror( const char *error ) { printf("%s\n", error ); } | 7 | void yyerror( const char *error ) { printf("%s\n", error ); } |
@@ -9,7 +10,8 @@ void yyerror( const char *error ) { printf("%s\n", error ); } | |||
9 | %union { | 10 | %union { |
10 | int64_t iValue; | 11 | int64_t iValue; |
11 | double dValue; | 12 | double dValue; |
12 | char *sValue; | 13 | Bu::String *sValue; |
14 | bool bValue; | ||
13 | } | 15 | } |
14 | 16 | ||
15 | %token tokGame | 17 | %token tokGame |
@@ -23,15 +25,17 @@ void yyerror( const char *error ) { printf("%s\n", error ); } | |||
23 | %token tokThen | 25 | %token tokThen |
24 | %token tokElse | 26 | %token tokElse |
25 | %token tokCommand | 27 | %token tokCommand |
26 | %token tokSituationName | 28 | %token <sValue> tokSituationName |
27 | %token tokIdent | 29 | %token <sValue> tokIdent |
28 | %token tokGoto | 30 | %token tokGoto |
29 | %token tokString | 31 | %token <sValue> tokString |
30 | %token tokInt | 32 | %token <iValue> tokInt |
31 | %token tokFloat | 33 | %token <dValue> tokFloat |
32 | %token tokBool | 34 | %token <bValue> tokBool |
33 | %token tokNull | 35 | %token tokNull |
34 | 36 | ||
37 | %destructor { delete $$; printf("string deleted.\n"); } tokString tokSituationName | ||
38 | |||
35 | %token eos 0 "end of stream" | 39 | %token eos 0 "end of stream" |
36 | 40 | ||
37 | %left '(' ')' '[' ']' | 41 | %left '(' ')' '[' ']' |
@@ -39,12 +43,28 @@ void yyerror( const char *error ) { printf("%s\n", error ); } | |||
39 | %left '-' '+' | 43 | %left '-' '+' |
40 | 44 | ||
41 | %% | 45 | %% |
42 | input: | 46 | input: gameDecl bodyDecl |
43 | | input situation | ||
44 | | input function | ||
45 | ; | 47 | ; |
46 | 48 | ||
47 | situation: tokSituation tokSituationName '{' cmpltExprList '}' | 49 | gameDecl: tokGame '{' gameExprList '}' |
50 | ; | ||
51 | |||
52 | gameExprList: | ||
53 | | gameExprList cmpltGameExpr | ||
54 | ; | ||
55 | |||
56 | cmpltGameExpr: gameExpr ';' | ||
57 | ; | ||
58 | |||
59 | gameExpr: tokIdent '=' expr | ||
60 | ; | ||
61 | |||
62 | bodyDecl: | ||
63 | | bodyDecl situation | ||
64 | | bodyDecl function | ||
65 | ; | ||
66 | |||
67 | situation: tokSituation tokSituationName { printf("Read situtaion: %s\n", (*$2).getStr() ); } '{' cmpltExprList '}' | ||
48 | ; | 68 | ; |
49 | 69 | ||
50 | function: tokFunction tokIdent '(' ')' '{' '}' | 70 | function: tokFunction tokIdent '(' ')' '{' '}' |
@@ -55,6 +75,7 @@ cmpltExprList: | |||
55 | ; | 75 | ; |
56 | 76 | ||
57 | cmpltExpr: expr ';' | 77 | cmpltExpr: expr ';' |
78 | | tokGoto '(' expr ')' ';' | ||
58 | ; | 79 | ; |
59 | 80 | ||
60 | expr: tokInt | 81 | expr: tokInt |
@@ -69,8 +90,19 @@ expr: tokInt | |||
69 | | expr '*' expr | 90 | | expr '*' expr |
70 | | '(' expr ')' | 91 | | '(' expr ')' |
71 | | expr '[' expr ']' | 92 | | expr '[' expr ']' |
72 | | tokGoto '(' expr ')' | 93 | | '[' ']' |
94 | | '[' listValues ']' | ||
95 | | '{' '}' | ||
96 | | '{' dictValues '}' | ||
73 | ; | 97 | ; |
98 | |||
99 | listValues: expr | ||
100 | | listValues ',' expr | ||
101 | ; | ||
102 | |||
103 | dictValues: expr ':' expr | ||
104 | | dictValues ',' expr ':' expr | ||
105 | ; | ||
74 | %% | 106 | %% |
75 | 107 | ||
76 | int main() | 108 | int main() |