Skip to content

Commit

Permalink
DIRECTOR: Lingo: Turn builtins into normal symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
sev- committed Aug 3, 2016
1 parent c8f0b74 commit 783ff8f
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 436 deletions.
10 changes: 9 additions & 1 deletion engines/director/lingo/lingo-builtins.cpp
Expand Up @@ -58,7 +58,15 @@ static struct BuiltinProto {

void Lingo::initBuiltIns() {
for (BuiltinProto *blt = builtins; blt->name; blt++) {
_builtins[blt->name] = new Builtin(blt->func, blt->nparams);
Symbol *sym = new Symbol;

sym->name = (char *)calloc(strlen(blt->name) + 1, 1);
Common::strlcpy(sym->name, blt->name, strlen(blt->name));
sym->type = BLTIN;
sym->nargs = blt->nparams;
sym->u.func = blt->func;

_handlers[blt->name] = sym;
}
}

Expand Down
6 changes: 6 additions & 0 deletions engines/director/lingo/lingo-code.cpp
Expand Up @@ -702,6 +702,12 @@ void Lingo::c_call() {

Symbol *sym = g_lingo->_handlers[name];

if (sym->type == BLTIN) {
(*sym->u.func)();

return;
}

for (int i = nargs; i < sym->nargs; i++) {
Datum d;

Expand Down
804 changes: 395 additions & 409 deletions engines/director/lingo/lingo-gr.cpp

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions engines/director/lingo/lingo-gr.y
Expand Up @@ -351,14 +351,9 @@ expr: INT {
| STRING {
$$ = g_lingo->code1(g_lingo->c_stringpush);
g_lingo->codeString($1->c_str()); }
| BLTIN '(' arglist ')' {
if ($3 != g_lingo->_builtins[*$1]->nargs)
error("Built-in function %s expects %d arguments but got %d", $1->c_str(), g_lingo->_builtins[*$1]->nargs, $3);

$$ = g_lingo->code1(g_lingo->_builtins[*$1]->func);
delete $1; }
| BLTINNOARGS {
$$ = g_lingo->code1(g_lingo->_builtins[*$1]->func);
$$ = g_lingo->code1(g_lingo->_handlers[*$1]->u.func);
$$ = g_lingo->code2(g_lingo->c_constpush, 0); // Put dummy value
delete $1; }
| ID '(' arglist ')' {
$$ = g_lingo->code1(g_lingo->c_call);
Expand Down
24 changes: 11 additions & 13 deletions engines/director/lingo/lingo-lex.cpp
Expand Up @@ -1237,53 +1237,51 @@ YY_RULE_SETUP
count();
yylval.s = new Common::String(yytext);

if (g_lingo->_builtins.contains(yytext)) {
if (g_lingo->_builtins[yytext]->nargs == -1)
if (g_lingo->_handlers.contains(yytext)) {
if (g_lingo->_handlers[yytext]->type == BLTIN && g_lingo->_handlers[yytext]->nargs == -1)
return BLTINNOARGS;
else
return BLTIN;
}

return ID;
}
YY_BREAK
case 48:
YY_RULE_SETUP
#line 181 "engines/director/lingo/lingo-lex.l"
#line 179 "engines/director/lingo/lingo-lex.l"
{ count(); yylval.f = atof(yytext); return FLOAT; }
YY_BREAK
case 49:
YY_RULE_SETUP
#line 182 "engines/director/lingo/lingo-lex.l"
#line 180 "engines/director/lingo/lingo-lex.l"
{ count(); yylval.i = strtol(yytext, NULL, 10); return INT; }
YY_BREAK
case 50:
YY_RULE_SETUP
#line 183 "engines/director/lingo/lingo-lex.l"
#line 181 "engines/director/lingo/lingo-lex.l"
{ count(); return *yytext; }
YY_BREAK
case 51:
/* rule 51 can match eol */
YY_RULE_SETUP
#line 184 "engines/director/lingo/lingo-lex.l"
#line 182 "engines/director/lingo/lingo-lex.l"
{ return '\n'; }
YY_BREAK
case 52:
YY_RULE_SETUP
#line 185 "engines/director/lingo/lingo-lex.l"
#line 183 "engines/director/lingo/lingo-lex.l"
{ count(); yylval.s = new Common::String(&yytext[1]); yylval.s->deleteLastChar(); return STRING; }
YY_BREAK
case 53:
YY_RULE_SETUP
#line 186 "engines/director/lingo/lingo-lex.l"
#line 184 "engines/director/lingo/lingo-lex.l"

YY_BREAK
case 54:
YY_RULE_SETUP
#line 188 "engines/director/lingo/lingo-lex.l"
#line 186 "engines/director/lingo/lingo-lex.l"
ECHO;
YY_BREAK
#line 1287 "engines/director/lingo/lingo-lex.cpp"
#line 1285 "engines/director/lingo/lingo-lex.cpp"
case YY_STATE_EOF(INITIAL):
yyterminate();

Expand Down Expand Up @@ -2283,7 +2281,7 @@ void yyfree (void * ptr )

#define YYTABLES_NAME "yytables"

#line 188 "engines/director/lingo/lingo-lex.l"
#line 186 "engines/director/lingo/lingo-lex.l"



Expand Down
6 changes: 2 additions & 4 deletions engines/director/lingo/lingo-lex.l
Expand Up @@ -169,11 +169,9 @@ whitespace [\t ]
count();
yylval.s = new Common::String(yytext);
if (g_lingo->_builtins.contains(yytext)) {
if (g_lingo->_builtins[yytext]->nargs == -1)
if (g_lingo->_handlers.contains(yytext)) {
if (g_lingo->_handlers[yytext]->type == BLTIN && g_lingo->_handlers[yytext]->nargs == -1)
return BLTINNOARGS;
else
return BLTIN;
}
return ID;
Expand Down
5 changes: 3 additions & 2 deletions engines/director/lingo/lingo.h
Expand Up @@ -85,6 +85,7 @@ struct Symbol { /* symbol table entry */
int i; /* VAR */
double f; /* FLOAT */
ScriptData *defn; /* FUNCTION, PROCEDURE */
void (*func)(void); /* BUILTIN */
Common::String *s; /* STRING */
FloatArray *arr; /* ARRAY, POINT, RECT */
} u;
Expand Down Expand Up @@ -288,11 +289,12 @@ class Lingo {

Common::Array<CFrame *> _callstack;
Common::Array<Common::String *> _argstack;
BuiltinHash _builtins;
TheEntityHash _theEntities;
TheEntityFieldHash _theEntityFields;
Common::Array<int> _labelstack;

SymbolHash _handlers;

int _linenumber;
int _colnumber;

Expand All @@ -315,7 +317,6 @@ class Lingo {

SymbolHash _globalvars;
SymbolHash *_localvars;
SymbolHash _handlers;

int _pc;

Expand Down
3 changes: 3 additions & 0 deletions engines/director/lingo/tests/math.lingo
Expand Up @@ -11,6 +11,9 @@ put cos(z2)
set x = 2 + 3 * (4 / 2)
put x

put power(2, 8)
updatestage

-- Type conversion
put (1024/4096)*100 -- 0
put (1024/4096)*100.0 -- 0.0
Expand Down

0 comments on commit 783ff8f

Please sign in to comment.