PRIVATE: reorganized lexer and grammar code
This commit is contained in:
parent
b95e920fbc
commit
fcddf8aae1
6 changed files with 101 additions and 61 deletions
|
@ -1,25 +1,12 @@
|
||||||
#include <stdio.h>
|
#include "common/str.h"
|
||||||
#include <stdlib.h>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
#include "grammar.tab.h"
|
#include "grammar.tab.h"
|
||||||
|
|
||||||
#define NSTACK 256
|
|
||||||
#define NPROG 2000
|
|
||||||
|
|
||||||
namespace Private {
|
namespace Private {
|
||||||
|
|
||||||
typedef struct Setting {
|
Setting *psetting;
|
||||||
|
SettingMap settings;
|
||||||
Datum stack[NSTACK]; /* the stack */
|
|
||||||
Datum *stackp; /* next free spot on stack */
|
|
||||||
|
|
||||||
Inst prog[NPROG]; /* the machine */
|
|
||||||
Inst *progp; /* next free spot for code generation */
|
|
||||||
Inst *pc; /* program counter during execution */
|
|
||||||
|
|
||||||
} Setting;
|
|
||||||
|
|
||||||
Datum *stack = NULL; /* the stack */
|
Datum *stack = NULL; /* the stack */
|
||||||
Datum *stackp = NULL; /* next free spot on stack */
|
Datum *stackp = NULL; /* next free spot on stack */
|
||||||
|
@ -28,33 +15,35 @@ Inst *prog = NULL; /* the machine */
|
||||||
Inst *progp = NULL; /* next free spot for code generation */
|
Inst *progp = NULL; /* next free spot for code generation */
|
||||||
Inst *pc = NULL; /* program counter during execution */
|
Inst *pc = NULL; /* program counter during execution */
|
||||||
|
|
||||||
void initcode(char *name) /* initialize for code generation */
|
void initsetting() /* initialize for code generation */
|
||||||
{
|
{
|
||||||
printf("setting %s\n", name);
|
psetting = (Setting*) malloc(sizeof(Setting));
|
||||||
Setting *s = (Setting*) malloc(sizeof(Setting));
|
memset((void *) psetting, 0, sizeof(Setting));
|
||||||
memset((void *) s, 0, sizeof(Setting));
|
|
||||||
|
|
||||||
prog = (Inst *) &s->prog;
|
prog = (Inst *) &psetting->prog;
|
||||||
stack = (Datum *) &s->stack;
|
stack = (Datum *) &psetting->stack;
|
||||||
|
|
||||||
stackp = stack;
|
stackp = stack;
|
||||||
progp = prog;
|
progp = prog;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void savesetting(char *name)
|
||||||
|
{
|
||||||
|
//printf("saving setting %s\n", name);
|
||||||
|
Common::String s(name);
|
||||||
|
settings.setVal(s, psetting);
|
||||||
|
}
|
||||||
|
|
||||||
int push(Datum d) /* push d onto stack */
|
int push(Datum d) /* push d onto stack */
|
||||||
{
|
{
|
||||||
if (stackp >= &stack[NSTACK])
|
assert (!(stackp >= &stack[NSTACK]));
|
||||||
abort();
|
|
||||||
// execerror("stack overflow", (char *) 0);
|
|
||||||
*stackp++ = d;
|
*stackp++ = d;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Datum pop() /* pop and return top elem from stack */
|
Datum pop() /* pop and return top elem from stack */
|
||||||
{
|
{
|
||||||
if (stackp <= stack)
|
assert (!(stackp <= stack));
|
||||||
abort();
|
|
||||||
// execerror("stack underflow", (char *) 0);
|
|
||||||
return *--stackp;
|
return *--stackp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +51,7 @@ int constpush() /* push constant onto stack */
|
||||||
{
|
{
|
||||||
Datum d;
|
Datum d;
|
||||||
d.val = ((Symbol *)*pc++)->u.val;
|
d.val = ((Symbol *)*pc++)->u.val;
|
||||||
printf("pushing %d\n", d.val);
|
//printf("pushing %d\n", d.val);
|
||||||
push(d);
|
push(d);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +60,7 @@ int strpush() /* push constant onto stack */
|
||||||
{
|
{
|
||||||
Datum d;
|
Datum d;
|
||||||
d.str = ((Symbol *)*pc++)->u.str;
|
d.str = ((Symbol *)*pc++)->u.str;
|
||||||
printf("pushing %s\n", d.str);
|
//printf("pushing %s\n", d.str);
|
||||||
push(d);
|
push(d);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -81,7 +70,7 @@ int varpush() /* push variable onto stack */
|
||||||
{
|
{
|
||||||
Datum d;
|
Datum d;
|
||||||
d.sym = (Symbol *)(*pc++);
|
d.sym = (Symbol *)(*pc++);
|
||||||
printf("var pushing %s", d.sym->name);
|
//printf("var pushing %s", d.sym->name);
|
||||||
push(d);
|
push(d);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -103,7 +92,7 @@ int add() /* add top two elems on stack */
|
||||||
Datum d1, d2;
|
Datum d1, d2;
|
||||||
d2 = pop();
|
d2 = pop();
|
||||||
d1 = pop();
|
d1 = pop();
|
||||||
printf("adding %d %d\n",d1.val, d2.val);
|
//printf("adding %d %d\n",d1.val, d2.val);
|
||||||
d1.val += d2.val;
|
d1.val += d2.val;
|
||||||
push(d1);
|
push(d1);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -196,15 +185,14 @@ int print() /* pop top value from stack, print it */
|
||||||
{
|
{
|
||||||
Datum d;
|
Datum d;
|
||||||
d = pop();
|
d = pop();
|
||||||
printf("\t%d\n", d.val);
|
//printf("\t%d\n", d.val);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Inst *code(Inst f) /* install one instruction or operand */
|
Inst *code(Inst f) /* install one instruction or operand */
|
||||||
{
|
{
|
||||||
Inst *oprogp = progp;
|
Inst *oprogp = progp;
|
||||||
if (progp >= &prog[NPROG])
|
assert (!(progp >= &prog[NPROG]));
|
||||||
abort();
|
|
||||||
*progp++ = f;
|
*progp++ = f;
|
||||||
return oprogp;
|
return oprogp;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,12 @@
|
||||||
|
#include "common/str.h"
|
||||||
|
#include "common/hash-str.h"
|
||||||
|
|
||||||
|
#define NSTACK 256
|
||||||
|
#define NPROG 2000
|
||||||
|
|
||||||
|
#ifndef PRIVATE_GRAMMAR_H
|
||||||
|
#define PRIVATE_GRAMMAR_H
|
||||||
|
|
||||||
typedef struct Symbol { /* symbol table entry */
|
typedef struct Symbol { /* symbol table entry */
|
||||||
char *name;
|
char *name;
|
||||||
short type; /* NAME, NUM or STRING */
|
short type; /* NAME, NUM or STRING */
|
||||||
|
@ -14,15 +23,33 @@ typedef union Datum { /* interpreter stack type */
|
||||||
Symbol *sym;
|
Symbol *sym;
|
||||||
} Datum;
|
} Datum;
|
||||||
|
|
||||||
|
|
||||||
namespace Private {
|
namespace Private {
|
||||||
|
|
||||||
Symbol *install(char *, int, int, char *), *lookup(char *);
|
|
||||||
extern Datum pop();
|
|
||||||
|
|
||||||
typedef int (*Inst)(); /* machine instruction */
|
typedef int (*Inst)(); /* machine instruction */
|
||||||
#define STOP (Inst) 0
|
#define STOP (Inst) 0
|
||||||
|
|
||||||
|
typedef struct Setting {
|
||||||
|
|
||||||
|
Datum stack[NSTACK]; /* the stack */
|
||||||
|
Datum *stackp; /* next free spot on stack */
|
||||||
|
|
||||||
|
Inst prog[NPROG]; /* the machine */
|
||||||
|
Inst *progp; /* next free spot for code generation */
|
||||||
|
Inst *pc; /* program counter during execution */
|
||||||
|
|
||||||
|
} Setting;
|
||||||
|
|
||||||
|
extern Setting *psetting;
|
||||||
|
|
||||||
|
typedef Common::HashMap<Common::String, Setting*> SettingMap;
|
||||||
|
|
||||||
|
extern SettingMap settings;
|
||||||
|
|
||||||
|
Symbol *install(char *, int, int, char *), *lookup(char *);
|
||||||
|
extern Symbol *symlist;
|
||||||
|
|
||||||
|
extern Datum pop();
|
||||||
|
|
||||||
extern Inst *code(Inst);
|
extern Inst *code(Inst);
|
||||||
extern Inst *prog;
|
extern Inst *prog;
|
||||||
extern int eval();
|
extern int eval();
|
||||||
|
@ -40,7 +67,11 @@ extern int lt();
|
||||||
extern int gt();
|
extern int gt();
|
||||||
|
|
||||||
|
|
||||||
extern void initcode(char *);
|
extern void initsetting();
|
||||||
|
extern void savesetting(char *);
|
||||||
|
|
||||||
extern void execute(Inst *);
|
extern void execute(Inst *);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -5,6 +5,8 @@
|
||||||
%option outfile="engines/private/lex.yy.cpp"
|
%option outfile="engines/private/lex.yy.cpp"
|
||||||
|
|
||||||
%{
|
%{
|
||||||
|
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "grammar.h"
|
#include "grammar.h"
|
||||||
|
@ -39,7 +41,7 @@ namespace Private {
|
||||||
|
|
||||||
int parse(char *code) {
|
int parse(char *code) {
|
||||||
|
|
||||||
initcode("init");
|
initsetting();
|
||||||
//_lines[0] = _lines[1] = _lines[2] = code;
|
//_lines[0] = _lines[1] = _lines[2] = code;
|
||||||
|
|
||||||
YY_BUFFER_STATE bp;
|
YY_BUFFER_STATE bp;
|
||||||
|
|
|
@ -3,9 +3,11 @@
|
||||||
%output "engines/private/grammar.tab.cpp"
|
%output "engines/private/grammar.tab.cpp"
|
||||||
|
|
||||||
%{
|
%{
|
||||||
|
|
||||||
|
#include "grammar.h"
|
||||||
|
|
||||||
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
#define FORBIDDEN_SYMBOL_ALLOW_ALL
|
||||||
|
|
||||||
#include "private/grammar.h"
|
|
||||||
#define code1(c1) Private::code(c1);
|
#define code1(c1) Private::code(c1);
|
||||||
#define code2(c1,c2) Private::code(c1); Private::code(c2)
|
#define code2(c1,c2) Private::code(c1); Private::code(c2)
|
||||||
#define code3(c1,c2,c3) Private::code(c1); Private::code(c2); Private::code(c3)
|
#define code3(c1,c2,c3) Private::code(c1); Private::code(c2); Private::code(c3)
|
||||||
|
@ -18,13 +20,12 @@ int yydebug=1;
|
||||||
|
|
||||||
using namespace Private;
|
using namespace Private;
|
||||||
|
|
||||||
extern FILE *yyin;
|
|
||||||
extern int yylex();
|
extern int yylex();
|
||||||
extern int yyparse();
|
extern int yyparse();
|
||||||
|
|
||||||
void yyerror(const char *str)
|
void yyerror(const char *str)
|
||||||
{
|
{
|
||||||
fprintf(stderr,"error: %s\n",str);
|
//fprintf(stderr,"error: %s\n",str);
|
||||||
}
|
}
|
||||||
|
|
||||||
int yywrap()
|
int yywrap()
|
||||||
|
@ -38,11 +39,13 @@ int yywrap()
|
||||||
struct Symbol *sym; /* symbol table pointer */
|
struct Symbol *sym; /* symbol table pointer */
|
||||||
char *s;
|
char *s;
|
||||||
int *i;
|
int *i;
|
||||||
|
int narg;
|
||||||
}
|
}
|
||||||
|
|
||||||
%token<s> NAME
|
%token<s> NAME
|
||||||
%token<sym> STRING NUM
|
%token<sym> STRING NUM
|
||||||
%token LTE GTE NEQ EQ IFTOK ELSETOK GOTOTOK DEBUGTOK DEFINETOK SETTINGTOK RANDOMTOK
|
%token LTE GTE NEQ EQ IFTOK ELSETOK GOTOTOK DEBUGTOK DEFINETOK SETTINGTOK RANDOMTOK
|
||||||
|
%type<narg> params
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
@ -50,9 +53,9 @@ lines: line lines
|
||||||
| line
|
| line
|
||||||
;
|
;
|
||||||
|
|
||||||
line: DEBUGTOK '{' debug '}' { printf("debug\n"); }
|
line: DEBUGTOK '{' debug '}' { /*printf("debug\n");*/ }
|
||||||
| DEFINETOK NAME '{' define '}' { printf("define %s\n", $NAME); }
|
| DEFINETOK NAME '{' define '}' { /*printf("define %s\n", $NAME);*/ }
|
||||||
| SETTINGTOK NAME '{' statements '}' { initcode($NAME); }
|
| SETTINGTOK NAME '{' statements '}' { savesetting($NAME); initsetting(); }
|
||||||
;
|
;
|
||||||
|
|
||||||
debug: /* nothing */
|
debug: /* nothing */
|
||||||
|
@ -74,21 +77,21 @@ statement: GOTOTOK expr ';' statements
|
||||||
;
|
;
|
||||||
|
|
||||||
define: /* nothing */
|
define: /* nothing */
|
||||||
| NAME ',' fcall ',' define { }
|
| NAME ',' fcall ',' define { Private::install($NAME, NAME, 0, NULL); }
|
||||||
| NAME ',' fcall { }
|
| NAME ',' fcall { Private::install($NAME, NAME, 0, NULL); }
|
||||||
| NAME ',' define { Private::install($NAME, NAME, 0, NULL); }
|
| NAME ',' define { Private::install($NAME, NAME, 0, NULL); }
|
||||||
| NAME { Private::install($NAME, NAME, 0, NULL); }
|
| NAME { Private::install($NAME, NAME, 0, NULL); }
|
||||||
;
|
;
|
||||||
|
|
||||||
fcall: GOTOTOK '(' NAME ')'
|
fcall: GOTOTOK '(' params ')'
|
||||||
| NAME '(' params ')'
|
| NAME '(' params ')' { /*printf("%s( .. %d)\n", $NAME, $params);*/ }
|
||||||
;
|
;
|
||||||
|
|
||||||
params: /* nothing */
|
params: /* nothing */ { $$ = 0; }
|
||||||
| params ',' fcall
|
| fcall ',' params { $$ = $3 + 1; }
|
||||||
| params ',' expr
|
| expr ',' params { $$ = $3 + 1; }
|
||||||
| expr
|
| expr { $$ = 1; }
|
||||||
| fcall
|
| fcall { $$ = 1; }
|
||||||
;
|
;
|
||||||
|
|
||||||
value: NUM { code2(Private::constpush, (Private::Inst)$NUM); }
|
value: NUM { code2(Private::constpush, (Private::Inst)$NUM); }
|
||||||
|
|
|
@ -64,6 +64,22 @@ Common::Error PrivateEngine::run() {
|
||||||
file->read(buf, 191000);
|
file->read(buf, 191000);
|
||||||
parse((char *) buf);
|
parse((char *) buf);
|
||||||
|
|
||||||
|
for (Symbol *s = symlist; s != NULL; s = s->next) {
|
||||||
|
if (s->type == 260) {
|
||||||
|
//debug("int");
|
||||||
|
//debug("%d", s->u.val);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s->type == 259) {
|
||||||
|
//debug("str");
|
||||||
|
//debug("%s", s->u.str);
|
||||||
|
}
|
||||||
|
if (s->type == 258) {
|
||||||
|
//debug(s->name);
|
||||||
|
//debug("%d", s->u.val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize graphics using following:
|
// Initialize graphics using following:
|
||||||
_screenW = 640;
|
_screenW = 640;
|
||||||
_screenH = 480;
|
_screenH = 480;
|
||||||
|
@ -103,7 +119,7 @@ Common::Error PrivateEngine::run() {
|
||||||
// Simple main event loop
|
// Simple main event loop
|
||||||
Common::Event evt;
|
Common::Event evt;
|
||||||
_videoDecoder = new Video::SmackerDecoder();
|
_videoDecoder = new Video::SmackerDecoder();
|
||||||
playVideo("intro/intro.smk");
|
//playVideo("intro/intro.smk");
|
||||||
while (!shouldQuit()) {
|
while (!shouldQuit()) {
|
||||||
g_system->getEventManager()->pollEvent(evt);
|
g_system->getEventManager()->pollEvent(evt);
|
||||||
g_system->delayMillis(10);
|
g_system->delayMillis(10);
|
||||||
|
|
|
@ -6,15 +6,15 @@
|
||||||
|
|
||||||
namespace Private {
|
namespace Private {
|
||||||
|
|
||||||
static Symbol *symlist = 0; /* symbol table: linked list */
|
Symbol *symlist; /* symbol table: linked list */
|
||||||
|
|
||||||
char *emalloc(unsigned n) /* check return from malloc */
|
char *emalloc(unsigned n) /* check return from malloc */
|
||||||
{
|
{
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
p = (char*) malloc(n);
|
p = (char*) malloc(n);
|
||||||
if (p == 0)
|
assert(p != NULL);
|
||||||
abort(); //execerror("out of memory", (char *) 0);
|
// abort(); //execerror("out of memory", (char *) 0);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ Symbol *install(char *n, int t, int d, char *s) /* install s in symbol table */
|
||||||
else if (t == STRING)
|
else if (t == STRING)
|
||||||
sp->u.str = s;
|
sp->u.str = s;
|
||||||
else
|
else
|
||||||
abort();
|
assert(0); //abort();
|
||||||
|
|
||||||
sp->next = symlist; /* put at front of list */
|
sp->next = symlist; /* put at front of list */
|
||||||
symlist = sp;
|
symlist = sp;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue