diff options
author | David Walter Seikel | 2012-01-08 09:01:44 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-01-08 09:01:44 +1000 |
commit | 0306182b9bcafa7f850dd91aa196ae7d562d2254 (patch) | |
tree | c815ac4506e27ed285a67d5ca1f936f40b90b9b6 /LuaSL/src | |
parent | Parenthesis and statement parsing. (diff) | |
download | SledjHamr-0306182b9bcafa7f850dd91aa196ae7d562d2254.zip SledjHamr-0306182b9bcafa7f850dd91aa196ae7d562d2254.tar.gz SledjHamr-0306182b9bcafa7f850dd91aa196ae7d562d2254.tar.bz2 SledjHamr-0306182b9bcafa7f850dd91aa196ae7d562d2254.tar.xz |
Use character classes, some clean up. Tried to add space storing, but it did not work.
Diffstat (limited to 'LuaSL/src')
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.c | 17 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_LSL_tree.h | 28 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_lexer.l | 19 | ||||
-rw-r--r-- | LuaSL/src/LuaSL_yaccer.y | 13 |
4 files changed, 46 insertions, 31 deletions
diff --git a/LuaSL/src/LuaSL_LSL_tree.c b/LuaSL/src/LuaSL_LSL_tree.c index c3f2a49..cc9007c 100644 --- a/LuaSL/src/LuaSL_LSL_tree.c +++ b/LuaSL/src/LuaSL_LSL_tree.c | |||
@@ -69,7 +69,7 @@ LSL_Token LSL_Tokens[] = | |||
69 | 69 | ||
70 | // Then the rest of the syntax tokens. | 70 | // Then the rest of the syntax tokens. |
71 | 71 | ||
72 | // {LSL_SPACE, " ", LSL_NONE, NULL, NULL, NULL}, | 72 | {LSL_SPACE, " ", LSL_NONE, NULL, NULL, NULL}, |
73 | // {LSL_COMMENT_LINE, "//", LSL_NONE, NULL, NULL, NULL}, | 73 | // {LSL_COMMENT_LINE, "//", LSL_NONE, NULL, NULL, NULL}, |
74 | // {LSL_COMMENT, "/*", LSL_NONE, NULL, NULL, NULL}, | 74 | // {LSL_COMMENT, "/*", LSL_NONE, NULL, NULL, NULL}, |
75 | // {LSL_TYPE, "type", LSL_NONE, NULL, NULL, NULL}, | 75 | // {LSL_TYPE, "type", LSL_NONE, NULL, NULL, NULL}, |
@@ -142,7 +142,7 @@ LSL_AST *addInteger(int value) | |||
142 | return ast; | 142 | return ast; |
143 | } | 143 | } |
144 | 144 | ||
145 | LSL_AST *addOperation(LSL_Operation type, LSL_AST *left, LSL_AST *right) | 145 | LSL_AST *addOperation(LSL_Type type, LSL_AST *left, LSL_AST *right) |
146 | { | 146 | { |
147 | LSL_AST *ast = newAST(type, left, right); | 147 | LSL_AST *ast = newAST(type, left, right); |
148 | 148 | ||
@@ -182,11 +182,22 @@ LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr) | |||
182 | return stat; | 182 | return stat; |
183 | } | 183 | } |
184 | 184 | ||
185 | LSL_AST *addSpace(char *text, LSL_AST *root) | ||
186 | { | ||
187 | LSL_AST *ast = newAST(LSL_SPACE, root, NULL); | ||
188 | |||
189 | if (ast) | ||
190 | ast->content.spaceValue = text; | ||
191 | |||
192 | return ast; | ||
193 | } | ||
194 | |||
185 | LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root) | 195 | LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root) |
186 | { | 196 | { |
187 | LSL_AST *ast = newAST(LSL_STATEMENT, root, NULL); | 197 | LSL_AST *ast = newAST(LSL_STATEMENT, root, NULL); |
188 | 198 | ||
189 | ast->content.statementValue = statement; | 199 | if (ast) |
200 | ast->content.statementValue = statement; | ||
190 | 201 | ||
191 | return ast; | 202 | return ast; |
192 | } | 203 | } |
diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 597db54..0b2f125 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h | |||
@@ -89,16 +89,12 @@ typedef enum // In order of precedence, high to low. | |||
89 | LSL_BIT_XOR, | 89 | LSL_BIT_XOR, |
90 | LSL_BIT_OR, | 90 | LSL_BIT_OR, |
91 | LSL_BOOL_OR, | 91 | LSL_BOOL_OR, |
92 | LSL_BOOL_AND | 92 | LSL_BOOL_AND, |
93 | } LSL_Operation; | ||
94 | #else | ||
95 | typedef int LSL_Operation; | ||
96 | #endif | ||
97 | 93 | ||
98 | #ifdef LUASL_USE_ENUM | 94 | // The rest are not operater types. |
99 | typedef enum | 95 | |
100 | { | 96 | LSL_SPACE, |
101 | LSL_COMMENT = (LSL_BOOL_AND + 1), | 97 | LSL_COMMENT, |
102 | LSL_TYPE, | 98 | LSL_TYPE, |
103 | LSL_NAME, | 99 | LSL_NAME, |
104 | LSL_IDENTIFIER, | 100 | LSL_IDENTIFIER, |
@@ -125,7 +121,8 @@ typedef enum | |||
125 | LSL_PARAMETER, | 121 | LSL_PARAMETER, |
126 | LSL_FUNCTION, | 122 | LSL_FUNCTION, |
127 | LSL_STATE, | 123 | LSL_STATE, |
128 | LSL_SCRIPT | 124 | LSL_SCRIPT, |
125 | LSL_UNKNOWN | ||
129 | } LSL_Type; | 126 | } LSL_Type; |
130 | #else | 127 | #else |
131 | typedef int LSL_Type; | 128 | typedef int LSL_Type; |
@@ -166,6 +163,7 @@ typedef struct | |||
166 | 163 | ||
167 | typedef union LSL_Leaf | 164 | typedef union LSL_Leaf |
168 | { | 165 | { |
166 | char *spaceValue; | ||
169 | char *commentValue; | 167 | char *commentValue; |
170 | LSL_Type typeValue; | 168 | LSL_Type typeValue; |
171 | char *nameValue; | 169 | char *nameValue; |
@@ -178,7 +176,7 @@ typedef union LSL_Leaf | |||
178 | float rotationValue[4]; | 176 | float rotationValue[4]; |
179 | union LSL_Leaf *listValue; | 177 | union LSL_Leaf *listValue; |
180 | char *labelValue; | 178 | char *labelValue; |
181 | LSL_Operation operationValue; | 179 | LSL_Type operationValue; |
182 | struct LSL_AST *expressionValue; | 180 | struct LSL_AST *expressionValue; |
183 | LSL_Statement *doValue; | 181 | LSL_Statement *doValue; |
184 | LSL_Statement *forValue; | 182 | LSL_Statement *forValue; |
@@ -192,6 +190,7 @@ typedef union LSL_Leaf | |||
192 | LSL_Function *functionValue; | 190 | LSL_Function *functionValue; |
193 | LSL_State *stateValue; | 191 | LSL_State *stateValue; |
194 | LSL_Script *scriptValue; | 192 | LSL_Script *scriptValue; |
193 | char *unknownValue; | ||
195 | } LSL_Leaf; | 194 | } LSL_Leaf; |
196 | 195 | ||
197 | typedef struct | 196 | typedef struct |
@@ -257,10 +256,11 @@ typedef struct | |||
257 | 256 | ||
258 | LSL_AST *addExpression(LSL_AST *exp); | 257 | LSL_AST *addExpression(LSL_AST *exp); |
259 | LSL_AST *addInteger(int value); | 258 | LSL_AST *addInteger(int value); |
260 | LSL_AST *addOperation(LSL_Operation type, LSL_AST *left, LSL_AST *right); | 259 | LSL_AST *addOperation(LSL_Type type, LSL_AST *left, LSL_AST *right); |
261 | LSL_AST *addParenthesis(LSL_AST *expr); | 260 | LSL_AST *addParenthesis(LSL_AST *expr); |
262 | LSL_Statement *createStatement(LSL_Type type, LSL_AST *expr); | 261 | LSL_Statement *createStatement(LSL_Type type, LSL_AST *root); |
263 | LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *left); | 262 | LSL_AST *addStatement(LSL_Statement *statement, LSL_AST *root); |
263 | LSL_AST *addSpace(char *text, LSL_AST *root); | ||
264 | 264 | ||
265 | int yyerror(const char *msg); | 265 | int yyerror(const char *msg); |
266 | int yyparse(void *param); | 266 | int yyparse(void *param); |
diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index 91e4264..2166743 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l | |||
@@ -19,22 +19,17 @@ | |||
19 | %option backup debug perf-report perf-report verbose warn | 19 | %option backup debug perf-report perf-report verbose warn |
20 | %option align full | 20 | %option align full |
21 | 21 | ||
22 | SPACE [ \r\n\t]* | 22 | HEX [[:xdigit:]] |
23 | 23 | NAME [[:alpha:]](_|[[:alpha:]]|[[:digit:]])* | |
24 | LETTER [A-Za-z] | 24 | INTEGER [[:digit:]]+ |
25 | DECIMAL [0-9] | ||
26 | HEX [0-9A-Fa-f] | ||
27 | |||
28 | NAME {LETTER}(_|{LETTER}|{DECIMAL})* | ||
29 | INTEGER {DECIMAL}+ | ||
30 | EXPONANT [eE][+-]?{INTEGER} | 25 | EXPONANT [eE][+-]?{INTEGER} |
31 | FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? | 26 | FLOAT {INTEGER}("."{INTEGER})?{EXPONANT}? |
32 | 27 | ||
33 | %% | 28 | %% |
34 | 29 | ||
35 | /* Basic tokens */ | 30 | /* Basic tokens */ |
36 | {SPACE} %{ ECHO; /* Skip blanks. */ %} | 31 | [[:space:]]+ %{ /* ECHO; yylval->spaceValue = strdup(yytext); return LSL_SPACE; */ %} |
37 | {NAME} %{ ECHO; /* yylval->nameValue=strdup(yytext); return LSL_NAME; */ %} | 32 | {NAME} %{ ECHO; /* yylval->nameValue = strdup(yytext); return LSL_NAME; */ %} |
38 | {INTEGER} %{ ECHO; yylval->integerValue = atoi(yytext); return LSL_INTEGER; %} | 33 | {INTEGER} %{ ECHO; yylval->integerValue = atoi(yytext); return LSL_INTEGER; %} |
39 | {FLOAT} %{ ECHO; /* yylval->floatValue = atof(yytext); return LSL_FLOAT; */ %} | 34 | {FLOAT} %{ ECHO; /* yylval->floatValue = atof(yytext); return LSL_FLOAT; */ %} |
40 | 35 | ||
@@ -74,12 +69,12 @@ int yywrap(yyscan_t yyscanner) | |||
74 | { | 69 | { |
75 | #ifdef FLEX_SCANNER | 70 | #ifdef FLEX_SCANNER |
76 | #ifndef LL_WINDOWS | 71 | #ifndef LL_WINDOWS |
77 | // get gcc to stop complaining about lack of use of yyunput | 72 | // Get gcc to stop complaining about lack of use of yyunput and input. |
78 | (void) yyunput; | 73 | (void) yyunput; |
79 | (void) input; | 74 | (void) input; |
80 | #endif | 75 | #endif |
81 | #endif | 76 | #endif |
82 | // TODO - If we are getting files from stdin, or multiple -f arguments, we should loop through them asd return 0. Return 1 when there are no more files. | 77 | // TODO - If we are getting files from stdin, or multiple -f arguments, we should loop through them and return 0. Return 1 when there are no more files. |
83 | return(1); | 78 | return(1); |
84 | } | 79 | } |
85 | 80 | ||
diff --git a/LuaSL/src/LuaSL_yaccer.y b/LuaSL/src/LuaSL_yaccer.y index 75cfd78..57d3075 100644 --- a/LuaSL/src/LuaSL_yaccer.y +++ b/LuaSL/src/LuaSL_yaccer.y | |||
@@ -9,9 +9,12 @@ | |||
9 | 9 | ||
10 | %define api.pure | 10 | %define api.pure |
11 | 11 | ||
12 | %token <integerValue> LSL_INTEGER | 12 | %token <spaceValue> LSL_SPACE |
13 | 13 | ||
14 | %nonassoc LSL_STATEMENT | 14 | %nonassoc LSL_STATEMENT |
15 | |||
16 | %token <integerValue> LSL_INTEGER | ||
17 | |||
15 | %left LSL_BOOL_AND | 18 | %left LSL_BOOL_AND |
16 | %left LSL_BOOL_OR | 19 | %left LSL_BOOL_OR |
17 | %left LSL_BIT_AND LSL_BIT_XOR LSL_BIT_OR | 20 | %left LSL_BIT_AND LSL_BIT_XOR LSL_BIT_OR |
@@ -20,19 +23,25 @@ | |||
20 | %left LSL_LEFT_SHIFT LSL_RIGHT_SHIFT | 23 | %left LSL_LEFT_SHIFT LSL_RIGHT_SHIFT |
21 | %left LSL_SUBTRACT LSL_ADD | 24 | %left LSL_SUBTRACT LSL_ADD |
22 | %left LSL_DIVIDE LSL_MODULO LSL_MULTIPLY | 25 | %left LSL_DIVIDE LSL_MODULO LSL_MULTIPLY |
26 | |||
23 | %right LSL_BIT_NOT LSL_BOOL_NOT LSL_NEGATION | 27 | %right LSL_BIT_NOT LSL_BOOL_NOT LSL_NEGATION |
24 | 28 | ||
25 | %token LSL_PARENTHESIS_OPEN LSL_PARENTHESIS_CLOSE LSL_EXPRESSION | 29 | %token LSL_PARENTHESIS_OPEN LSL_PARENTHESIS_CLOSE LSL_EXPRESSION |
26 | 30 | ||
27 | %type <expressionValue> expr | 31 | %type <expressionValue> expr |
28 | |||
29 | %type <statementValue> statement | 32 | %type <statementValue> statement |
33 | %type <spaceValue> ignorable | ||
30 | 34 | ||
31 | %% | 35 | %% |
32 | 36 | ||
33 | input : | 37 | input : |
34 | expr { ((LuaSL_yyparseParam*)data)->ast = addOperation(LSL_EXPRESSION, $1, $1); } | 38 | expr { ((LuaSL_yyparseParam*)data)->ast = addOperation(LSL_EXPRESSION, $1, $1); } |
35 | | statement { ((LuaSL_yyparseParam*)data)->ast = addStatement($1, ((LuaSL_yyparseParam*)data)->ast); } | 39 | | statement { ((LuaSL_yyparseParam*)data)->ast = addStatement($1, ((LuaSL_yyparseParam*)data)->ast); } |
40 | | ignorable { ((LuaSL_yyparseParam*)data)->ast = addSpace($1, ((LuaSL_yyparseParam*)data)->ast); } | ||
41 | ; | ||
42 | |||
43 | ignorable : | ||
44 | LSL_SPACE { $$ = strdup($1); } | ||
36 | ; | 45 | ; |
37 | 46 | ||
38 | statement : | 47 | statement : |