aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_parser.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-01-05 09:03:42 +1000
committerDavid Walter Seikel2012-01-05 09:03:42 +1000
commit0384c411107672aeb4bd1134d101b5dc62cc41bb (patch)
treeaf576a45a18787d04f727a01f17ec32f9c0f40fc /LuaSL/src/LuaSL_parser.c
parentRemove most of the constants, we can put them in Lua globals later. Make the... (diff)
downloadSledjHamr-0384c411107672aeb4bd1134d101b5dc62cc41bb.zip
SledjHamr-0384c411107672aeb4bd1134d101b5dc62cc41bb.tar.gz
SledjHamr-0384c411107672aeb4bd1134d101b5dc62cc41bb.tar.bz2
SledjHamr-0384c411107672aeb4bd1134d101b5dc62cc41bb.tar.xz
Clean up the parser.
Diffstat (limited to '')
-rw-r--r--LuaSL/src/LuaSL_parser.c92
1 files changed, 12 insertions, 80 deletions
diff --git a/LuaSL/src/LuaSL_parser.c b/LuaSL/src/LuaSL_parser.c
index 9e1d3ca..c6026e2 100644
--- a/LuaSL/src/LuaSL_parser.c
+++ b/LuaSL/src/LuaSL_parser.c
@@ -1,86 +1,18 @@
1#include "LuaSL_parser_param.h" 1#include "LuaSL_LSL_tree.h"
2#include "LuaSL_yaccer.tab.h" 2#include <stdio.h>
3
4
5int yyparse(void *param);
6
7static int initParserParam(SParserParam* param)
8{
9 int ret = 0;
10
11 ret = yylex_init(&param->scanner);
12 param->expression = NULL;
13
14 return ret;
15}
16
17static int destroyParserParam(SParserParam* param)
18{
19 return yylex_destroy(param->scanner);
20}
21
22SExpression *getAST(const char *expr)
23{
24 SParserParam p;
25 YY_BUFFER_STATE state;
26
27 if ( initParserParam(&p) )
28 {
29 // couldn't initialize
30 return NULL;
31 }
32
33 state = yy_scan_string(expr, p.scanner);
34
35 if ( yyparse(&p) )
36 {
37 // error parsing
38 return NULL;
39 }
40
41 yy_delete_buffer(state, p.scanner);
42
43 destroyParserParam(&p);
44
45 return p.expression;
46}
47
48int evaluate(SExpression *e)
49{
50 switch(e->type)
51 {
52 case eVALUE:
53 return e->value;
54 case eMULTIPLY:
55 return evaluate(e->left) * evaluate(e->right);
56 case ePLUS:
57 return evaluate(e->left) + evaluate(e->right);
58 default:
59 // shouldn't be here
60 return 0;
61 }
62}
63 3
64int yyerror(const char *msg)
65{
66 fprintf(stderr,"Error:%s\n",msg); return 0;
67}
68 4
69int main(void) 5int main(void)
70{ 6{
71 SExpression *e = NULL; 7 char test[]=" 4 + 2*10 + 3*( 5 + 1 )";
72 char test[]=" 4 + 2*10 + 3*( 5 + 1 )"; 8 SExpression *e = NULL;
73 int result = 0; 9 int result = 0;
74 10
75 e = getAST(test); 11 e = getAST(test);
76 12 result = evaluate(e);
77 result = evaluate(e); 13 printf("Result of '%s' is %d\n", test, result);
78 14 deleteExpression(e);
79 printf("Result of '%s' is %d\n", test, result);
80
81 deleteExpression(e);
82
83 return 0;
84}
85 15
16 return 0;
17}
86 18