diff options
Diffstat (limited to 'src/parser.y')
-rw-r--r-- | src/parser.y | 56 |
1 files changed, 44 insertions, 12 deletions
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() |