summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-12-11 21:57:44 -0700
committerMike Buland <eichlan@xagasoft.com>2011-12-11 21:57:44 -0700
commit7c90fe68b557d32b65955ffd18e2e79585a7282b (patch)
tree1713fa7b087c83cd68669a46389bd3b35ae8b840
parent78118e3b515ea5268205b4f56d914900a9535c88 (diff)
downloadstage-7c90fe68b557d32b65955ffd18e2e79585a7282b.tar.gz
stage-7c90fe68b557d32b65955ffd18e2e79585a7282b.tar.bz2
stage-7c90fe68b557d32b65955ffd18e2e79585a7282b.tar.xz
stage-7c90fe68b557d32b65955ffd18e2e79585a7282b.zip
Getting closer.
Real turing-complete code is coming soon
-rw-r--r--default.bld2
-rw-r--r--demo.stage11
-rw-r--r--src/parser.l37
-rw-r--r--src/parser.y56
4 files changed, 83 insertions, 23 deletions
diff --git a/default.bld b/default.bld
index 296453f..b3d2794 100644
--- a/default.bld
+++ b/default.bld
@@ -7,6 +7,8 @@ target "stage"
7 7
8 FLEXFLAGS="-osrc/parser.yy.c"; 8 FLEXFLAGS="-osrc/parser.yy.c";
9 BISONFLAGS="-d"; 9 BISONFLAGS="-d";
10
11 LDFLAGS += "-lbu++";
10} 12}
11 13
12/* 14/*
diff --git a/demo.stage b/demo.stage
index b1ff4e8..3d10efe 100644
--- a/demo.stage
+++ b/demo.stage
@@ -1,11 +1,8 @@
1//game
2//{
3 /*
4 title = "Demo game";
5 1
6 goto <<start>>; 2game
7 */ 3{
8//} 4 title = "Demo game";
5}
9 6
10function hello() 7function hello()
11{ 8{
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; }
25command { return tokCommand; } 26command { return tokCommand; }
26goto { return tokGoto; } 27goto { return tokGoto; }
27 28
29true { yylval.bValue = true; return tokBool; }
30false { yylval.bValue = false; return tokBool; }
31null { 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
5int yylex(); 6int yylex();
6void yyerror( const char *error ) { printf("%s\n", error ); } 7void 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%%
42input: 46input: gameDecl bodyDecl
43 | input situation
44 | input function
45 ; 47 ;
46 48
47situation: tokSituation tokSituationName '{' cmpltExprList '}' 49gameDecl: tokGame '{' gameExprList '}'
50 ;
51
52gameExprList:
53 | gameExprList cmpltGameExpr
54 ;
55
56cmpltGameExpr: gameExpr ';'
57 ;
58
59gameExpr: tokIdent '=' expr
60 ;
61
62bodyDecl:
63 | bodyDecl situation
64 | bodyDecl function
65 ;
66
67situation: tokSituation tokSituationName { printf("Read situtaion: %s\n", (*$2).getStr() ); } '{' cmpltExprList '}'
48 ; 68 ;
49 69
50function: tokFunction tokIdent '(' ')' '{' '}' 70function: tokFunction tokIdent '(' ')' '{' '}'
@@ -55,6 +75,7 @@ cmpltExprList:
55 ; 75 ;
56 76
57cmpltExpr: expr ';' 77cmpltExpr: expr ';'
78 | tokGoto '(' expr ')' ';'
58 ; 79 ;
59 80
60expr: tokInt 81expr: 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
99listValues: expr
100 | listValues ',' expr
101 ;
102
103dictValues: expr ':' expr
104 | dictValues ',' expr ':' expr
105 ;
74%% 106%%
75 107
76int main() 108int main()