PRIVATE: reorganized lexer and grammar code

This commit is contained in:
neuromancer 2020-12-31 17:03:11 -03:00 committed by Eugene Sandulenko
parent b95e920fbc
commit fcddf8aae1
6 changed files with 101 additions and 61 deletions

View file

@ -1,25 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include "common/str.h"
#include "grammar.h"
#include "grammar.tab.h"
#define NSTACK 256
#define NPROG 2000
namespace Private {
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;
Setting *psetting;
SettingMap settings;
Datum *stack = NULL; /* the 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 *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);
Setting *s = (Setting*) malloc(sizeof(Setting));
memset((void *) s, 0, sizeof(Setting));
psetting = (Setting*) malloc(sizeof(Setting));
memset((void *) psetting, 0, sizeof(Setting));
prog = (Inst *) &s->prog;
stack = (Datum *) &s->stack;
prog = (Inst *) &psetting->prog;
stack = (Datum *) &psetting->stack;
stackp = stack;
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 */
{
if (stackp >= &stack[NSTACK])
abort();
// execerror("stack overflow", (char *) 0);
assert (!(stackp >= &stack[NSTACK]));
*stackp++ = d;
return 0;
}
Datum pop() /* pop and return top elem from stack */
{
if (stackp <= stack)
abort();
// execerror("stack underflow", (char *) 0);
assert (!(stackp <= stack));
return *--stackp;
}
@ -62,7 +51,7 @@ int constpush() /* push constant onto stack */
{
Datum d;
d.val = ((Symbol *)*pc++)->u.val;
printf("pushing %d\n", d.val);
//printf("pushing %d\n", d.val);
push(d);
return 0;
}
@ -71,7 +60,7 @@ int strpush() /* push constant onto stack */
{
Datum d;
d.str = ((Symbol *)*pc++)->u.str;
printf("pushing %s\n", d.str);
//printf("pushing %s\n", d.str);
push(d);
return 0;
}
@ -81,7 +70,7 @@ int varpush() /* push variable onto stack */
{
Datum d;
d.sym = (Symbol *)(*pc++);
printf("var pushing %s", d.sym->name);
//printf("var pushing %s", d.sym->name);
push(d);
return 0;
}
@ -103,7 +92,7 @@ int add() /* add top two elems on stack */
Datum d1, d2;
d2 = 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;
push(d1);
return 0;
@ -196,15 +185,14 @@ int print() /* pop top value from stack, print it */
{
Datum d;
d = pop();
printf("\t%d\n", d.val);
//printf("\t%d\n", d.val);
return 0;
}
Inst *code(Inst f) /* install one instruction or operand */
{
Inst *oprogp = progp;
if (progp >= &prog[NPROG])
abort();
assert (!(progp >= &prog[NPROG]));
*progp++ = f;
return oprogp;
}

View file

@ -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 */
char *name;
short type; /* NAME, NUM or STRING */
@ -14,15 +23,33 @@ typedef union Datum { /* interpreter stack type */
Symbol *sym;
} Datum;
namespace Private {
Symbol *install(char *, int, int, char *), *lookup(char *);
extern Datum pop();
typedef int (*Inst)(); /* machine instruction */
#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 *prog;
extern int eval();
@ -40,7 +67,11 @@ extern int lt();
extern int gt();
extern void initcode(char *);
extern void initsetting();
extern void savesetting(char *);
extern void execute(Inst *);
}
#endif

View file

@ -5,6 +5,8 @@
%option outfile="engines/private/lex.yy.cpp"
%{
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include <stdio.h>
#include <string.h>
#include "grammar.h"
@ -39,7 +41,7 @@ namespace Private {
int parse(char *code) {
initcode("init");
initsetting();
//_lines[0] = _lines[1] = _lines[2] = code;
YY_BUFFER_STATE bp;

View file

@ -3,9 +3,11 @@
%output "engines/private/grammar.tab.cpp"
%{
#include "grammar.h"
#define FORBIDDEN_SYMBOL_ALLOW_ALL
#include "private/grammar.h"
#define code1(c1) Private::code(c1);
#define code2(c1,c2) Private::code(c1); Private::code(c2)
#define code3(c1,c2,c3) Private::code(c1); Private::code(c2); Private::code(c3)
@ -18,13 +20,12 @@ int yydebug=1;
using namespace Private;
extern FILE *yyin;
extern int yylex();
extern int yyparse();
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
//fprintf(stderr,"error: %s\n",str);
}
int yywrap()
@ -38,11 +39,13 @@ int yywrap()
struct Symbol *sym; /* symbol table pointer */
char *s;
int *i;
int narg;
}
%token<s> NAME
%token<sym> STRING NUM
%token LTE GTE NEQ EQ IFTOK ELSETOK GOTOTOK DEBUGTOK DEFINETOK SETTINGTOK RANDOMTOK
%type<narg> params
%%
@ -50,9 +53,9 @@ lines: line lines
| line
;
line: DEBUGTOK '{' debug '}' { printf("debug\n"); }
| DEFINETOK NAME '{' define '}' { printf("define %s\n", $NAME); }
| SETTINGTOK NAME '{' statements '}' { initcode($NAME); }
line: DEBUGTOK '{' debug '}' { /*printf("debug\n");*/ }
| DEFINETOK NAME '{' define '}' { /*printf("define %s\n", $NAME);*/ }
| SETTINGTOK NAME '{' statements '}' { savesetting($NAME); initsetting(); }
;
debug: /* nothing */
@ -74,21 +77,21 @@ statement: GOTOTOK expr ';' statements
;
define: /* nothing */
| NAME ',' fcall ',' define { }
| NAME ',' fcall { }
| NAME ',' fcall ',' define { Private::install($NAME, NAME, 0, NULL); }
| NAME ',' fcall { Private::install($NAME, NAME, 0, NULL); }
| NAME ',' define { Private::install($NAME, NAME, 0, NULL); }
| NAME { Private::install($NAME, NAME, 0, NULL); }
;
fcall: GOTOTOK '(' NAME ')'
| NAME '(' params ')'
fcall: GOTOTOK '(' params ')'
| NAME '(' params ')' { /*printf("%s( .. %d)\n", $NAME, $params);*/ }
;
params: /* nothing */
| params ',' fcall
| params ',' expr
| expr
| fcall
params: /* nothing */ { $$ = 0; }
| fcall ',' params { $$ = $3 + 1; }
| expr ',' params { $$ = $3 + 1; }
| expr { $$ = 1; }
| fcall { $$ = 1; }
;
value: NUM { code2(Private::constpush, (Private::Inst)$NUM); }

View file

@ -64,6 +64,22 @@ Common::Error PrivateEngine::run() {
file->read(buf, 191000);
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:
_screenW = 640;
_screenH = 480;
@ -103,7 +119,7 @@ Common::Error PrivateEngine::run() {
// Simple main event loop
Common::Event evt;
_videoDecoder = new Video::SmackerDecoder();
playVideo("intro/intro.smk");
//playVideo("intro/intro.smk");
while (!shouldQuit()) {
g_system->getEventManager()->pollEvent(evt);
g_system->delayMillis(10);

View file

@ -6,15 +6,15 @@
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 *p;
p = (char*) malloc(n);
if (p == 0)
abort(); //execerror("out of memory", (char *) 0);
assert(p != NULL);
// abort(); //execerror("out of memory", (char *) 0);
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)
sp->u.str = s;
else
abort();
assert(0); //abort();
sp->next = symlist; /* put at front of list */
symlist = sp;