From 4d5d24f1da11a1279da0b5ea9351c4372a74bc43 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 11 Dec 2011 10:01:52 -0700 Subject: Basic parser coming together. --- .gitignore | 3 +++ default.bld | 24 ++++++++++++++++++ demo.stage | 24 ++++++++++++++++++ src/parser.l | 43 ++++++++++++++++++++++++++++++++ src/parser.y | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stage.txt | 29 ++++++++++++++++++++++ 6 files changed, 204 insertions(+) create mode 100644 .gitignore create mode 100644 default.bld create mode 100644 demo.stage create mode 100644 src/parser.l create mode 100644 src/parser.y create mode 100644 stage.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..275e405 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +stage +.build_cache +.*.swp diff --git a/default.bld b/default.bld new file mode 100644 index 0000000..296453f --- /dev/null +++ b/default.bld @@ -0,0 +1,24 @@ + +CC="g++"; +target "stage" +{ + rule "exe"; + input files("src/*.y", "src/*.l", "src/*.cpp"); + + FLEXFLAGS="-osrc/parser.yy.c"; + BISONFLAGS="-d"; +} + +/* +rule "bison" +{ + input "*.y"; + output [INPUT.regex("\\.y$", ".tab.c"), INPUT.regex("\\.y$", ".tab.h")]; + profile "build" + { + DIR=INPUT.dirName(); + FILE=INPUT.fileName(); + execute("cd ${DIR}; bison -d ${FILE}"); + } +} +*/ diff --git a/demo.stage b/demo.stage new file mode 100644 index 0000000..b1ff4e8 --- /dev/null +++ b/demo.stage @@ -0,0 +1,24 @@ +//game +//{ + /* + title = "Demo game"; + + goto <>; + */ +//} + +function hello() +{ +} + +situation <> +{ +// set situation end; +// push situation somethingElse; + +} + +situation <> +{ + goto( <> ); +} diff --git a/src/parser.l b/src/parser.l new file mode 100644 index 0000000..d5ff735 --- /dev/null +++ b/src/parser.l @@ -0,0 +1,43 @@ +%{ +#include +#include "parser.tab.h" +%} + +%option noyywrap + +%x sitname +%x comment +%x tdqstr tsqstr +%% + +[-{}<>=+/*,();] { return yytext[0]; } + +game { return tokGame; } +function { return tokFunction; } +situation { return tokSituation; } +while { return tokWhile; } +for { return tokFor; } +each { return tokEach; } +in { return tokIn; } +if { return tokIf; } +then { return tokThen; } +else { return tokElse; } +command { return tokCommand; } +goto { return tokGoto; } + +"<<" { BEGIN( sitname ); } +[- a-zA-Z0-9]+ { printf("situation: %s\n", yytext ); return tokSituationName; } +">>" { BEGIN( INITIAL ); } +. REJECT; + +"//"[^\n]* {} + +"/\*" { BEGIN( comment ); } +"\*/" { BEGIN( INITIAL ); } +. {} + +[a-zA-Z_][a-zA-Z0-9_]* { return tokIdent; } + +[ \t\n\r]+ {} + +%% diff --git a/src/parser.y b/src/parser.y new file mode 100644 index 0000000..b3da848 --- /dev/null +++ b/src/parser.y @@ -0,0 +1,81 @@ +%{ +#include +#include + +int yylex(); +void yyerror( const char *error ) { printf("%s\n", error ); } +%} + +%union { + int64_t iValue; + double dValue; + char *sValue; +} + +%token tokGame +%token tokFunction +%token tokSituation +%token tokWhile +%token tokFor +%token tokEach +%token tokIn +%token tokIf +%token tokThen +%token tokElse +%token tokCommand +%token tokSituationName +%token tokIdent +%token tokGoto +%token tokString +%token tokInt +%token tokFloat +%token tokBool +%token tokNull + +%token eos 0 "end of stream" + +%left '(' ')' '[' ']' +%left '*' '/' +%left '-' '+' + +%% +input: + | input situation + | input function + ; + +situation: tokSituation tokSituationName '{' cmpltExprList '}' + ; + +function: tokFunction tokIdent '(' ')' '{' '}' + ; + +cmpltExprList: + | cmpltExprList cmpltExpr + ; + +cmpltExpr: expr ';' + ; + +expr: tokInt + | tokFloat + | tokString + | tokBool + | tokNull + | tokSituationName + | expr '+' expr + | expr '-' expr + | expr '/' expr + | expr '*' expr + | '(' expr ')' + | expr '[' expr ']' + | tokGoto '(' expr ')' + ; +%% + +int main() +{ + yyparse(); + return 0; +} + diff --git a/stage.txt b/stage.txt new file mode 100644 index 0000000..751b1f0 --- /dev/null +++ b/stage.txt @@ -0,0 +1,29 @@ +Simple, Textual, Adventure Game Environemnt +=========================================== + +This is a program that loads games from files that use the Stage language to +describe a complete adventure game. There are lots and lots of possibilities. + +This documentation is primarily aimed at game developers initially. + +Structure +========= + +A game is divided into several pieces. At any point during the play of a game +a player will be "in" a situation. Each situation provides context for the +player. They can be thought of like rooms in a MUD game or locations in an old +text adventure title, but are more flexible, as they allow different possible +transitions between them as well as different contexts for commands. + +Each situation is effectively a block of code that is run when the situation is +made active. I.e. that code runs when the player enters a room, say. Every +time a situation starts it contains an empty context, but may have access to +global parameters. + +Variables may be set and used, and there are some special variables that you +have access to that control the way the system works and interactions with the +player. + +New functions can be defined that can be used from any code. + + -- cgit v1.2.3