2005-07-03 14:18:54 +00:00
|
|
|
//============================================================================
|
|
|
|
//
|
|
|
|
// SSSS tt lll lll
|
|
|
|
// SS SS tt ll ll
|
|
|
|
// SS tttttt eeee ll ll aaaa
|
|
|
|
// SSSS tt ee ee ll ll aa
|
|
|
|
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
|
|
|
|
// SS SS tt ee ll ll aa aa
|
|
|
|
// SSSS ttt eeeee llll llll aaaaa
|
|
|
|
//
|
|
|
|
// Copyright (c) 1995-2005 by Bradford W. Mott and the Stella team
|
|
|
|
//
|
|
|
|
// See the file "license" for information on usage and redistribution of
|
|
|
|
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
|
|
|
//
|
2005-07-15 02:19:07 +00:00
|
|
|
// $Id: YaccParser.cxx,v 1.8 2005-07-15 02:19:07 urchlay Exp $
|
2005-07-03 14:18:54 +00:00
|
|
|
//
|
|
|
|
// Based on code from ScummVM - Scumm Interpreter
|
|
|
|
// Copyright (C) 2002-2004 The ScummVM project
|
|
|
|
//============================================================================
|
2005-07-01 04:29:18 +00:00
|
|
|
|
|
|
|
//#include "YaccParser.hxx"
|
|
|
|
|
2005-07-13 02:54:13 +00:00
|
|
|
//#ifdef __cplusplus
|
|
|
|
//extern "C" {
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
#include "Expression.hxx"
|
2005-07-15 02:19:07 +00:00
|
|
|
#include "CpuDebug.hxx"
|
2005-07-13 04:49:19 +00:00
|
|
|
|
|
|
|
#include "BinAndExpression.hxx"
|
|
|
|
#include "BinNotExpression.hxx"
|
|
|
|
#include "BinOrExpression.hxx"
|
|
|
|
#include "BinXorExpression.hxx"
|
2005-07-15 02:19:07 +00:00
|
|
|
#include "ByteMethodExpression.hxx"
|
2005-07-15 01:20:11 +00:00
|
|
|
#include "ByteDerefExpression.hxx"
|
|
|
|
#include "WordDerefExpression.hxx"
|
2005-07-13 02:54:13 +00:00
|
|
|
#include "ConstExpression.hxx"
|
2005-07-13 04:49:19 +00:00
|
|
|
#include "DivExpression.hxx"
|
|
|
|
#include "EqualsExpression.hxx"
|
2005-07-14 15:13:58 +00:00
|
|
|
#include "EquateExpression.hxx"
|
2005-07-13 04:49:19 +00:00
|
|
|
#include "Expression.hxx"
|
|
|
|
#include "GreaterEqualsExpression.hxx"
|
|
|
|
#include "GreaterExpression.hxx"
|
2005-07-15 01:40:34 +00:00
|
|
|
#include "HiByteExpression.hxx"
|
|
|
|
#include "LoByteExpression.hxx"
|
2005-07-13 04:49:19 +00:00
|
|
|
#include "LessEqualsExpression.hxx"
|
|
|
|
#include "LessExpression.hxx"
|
|
|
|
#include "LogAndExpression.hxx"
|
|
|
|
#include "LogOrExpression.hxx"
|
|
|
|
#include "LogNotExpression.hxx"
|
|
|
|
#include "MinusExpression.hxx"
|
|
|
|
#include "ModExpression.hxx"
|
|
|
|
#include "MultExpression.hxx"
|
|
|
|
#include "NotEqualsExpression.hxx"
|
|
|
|
#include "PlusExpression.hxx"
|
|
|
|
#include "ShiftLeftExpression.hxx"
|
|
|
|
#include "ShiftRightExpression.hxx"
|
|
|
|
#include "UnaryMinusExpression.hxx"
|
2005-07-03 14:18:54 +00:00
|
|
|
|
2005-07-01 04:29:18 +00:00
|
|
|
namespace YaccParser {
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2005-07-13 02:54:13 +00:00
|
|
|
#include "y.tab.h"
|
|
|
|
yystype result;
|
2005-07-01 04:29:18 +00:00
|
|
|
#include "y.tab.c"
|
|
|
|
|
|
|
|
|
2005-07-13 02:54:13 +00:00
|
|
|
Expression *getResult() {
|
|
|
|
return result.exp;
|
2005-07-01 04:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *input, *c;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ST_DEFAULT,
|
2005-07-14 15:13:58 +00:00
|
|
|
ST_IDENTIFIER,
|
2005-07-01 04:29:18 +00:00
|
|
|
ST_OPERATOR,
|
|
|
|
ST_SPACE
|
|
|
|
};
|
|
|
|
|
|
|
|
int state = ST_DEFAULT;
|
|
|
|
|
|
|
|
//extern int yylval; // bison provides this
|
|
|
|
|
|
|
|
void setInput(const char *in) {
|
|
|
|
input = c = in;
|
|
|
|
state = ST_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse(const char *in) {
|
|
|
|
setInput(in);
|
|
|
|
return yyparse();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hand-rolled lexer. Hopefully faster than flex... */
|
|
|
|
|
2005-07-15 01:20:11 +00:00
|
|
|
inline bool is_base_prefix(char x) { return ( (x=='\\' || x=='$' || x=='#') ); }
|
2005-07-14 15:13:58 +00:00
|
|
|
|
2005-07-15 01:20:11 +00:00
|
|
|
inline bool is_identifier(char x) {
|
|
|
|
return ( (x>='0' && x<='9') ||
|
|
|
|
(x>='a' && x<='z') ||
|
|
|
|
(x>='A' && x<='Z') ||
|
|
|
|
x=='.' || x=='_' );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool is_operator(char x) {
|
|
|
|
return ( (x=='+' || x=='-' || x=='*' ||
|
|
|
|
x=='/' || x=='<' || x=='>' ||
|
|
|
|
x=='|' || x=='&' || x=='^' ||
|
|
|
|
x=='!' || x=='~' || x=='(' ||
|
|
|
|
x==')' || x=='=' || x=='%' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: error checking!
|
|
|
|
int const_to_int(char *c) {
|
|
|
|
// what base is the input in?
|
|
|
|
BaseFormat base = Debugger::debugger().parser()->base();
|
|
|
|
|
|
|
|
switch(*c) {
|
|
|
|
case '\\':
|
|
|
|
base = kBASE_2;
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
base = kBASE_10;
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '$':
|
|
|
|
base = kBASE_16;
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: // not a base_prefix, use default base
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
switch(base) {
|
|
|
|
case kBASE_2:
|
|
|
|
while(*c) {
|
|
|
|
ret *= 2;
|
|
|
|
ret += (*c - '0'); // FIXME: error check!
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
case kBASE_10:
|
|
|
|
while(*c) {
|
|
|
|
ret *= 10;
|
|
|
|
ret += (*c - '0'); // FIXME: error check!
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
case kBASE_16:
|
|
|
|
while(*c) { // FIXME: error check!
|
|
|
|
int dig = (*c - '0');
|
|
|
|
if(dig > 9) dig = tolower(*c) - 'a';
|
|
|
|
ret *= 16;
|
|
|
|
ret += dig;
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "INVALID BASE in lexer!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-07-01 04:29:18 +00:00
|
|
|
|
2005-07-15 02:19:07 +00:00
|
|
|
CPUDEBUG_BYTE_METHOD getByteSpecial(char *c) {
|
|
|
|
if(strcmp(c, "a") == 0)
|
|
|
|
return &CpuDebug::a;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-01 04:29:18 +00:00
|
|
|
int yylex() {
|
2005-07-14 15:13:58 +00:00
|
|
|
static char idbuf[255];
|
2005-07-01 04:29:18 +00:00
|
|
|
char o, p;
|
2005-07-13 02:54:13 +00:00
|
|
|
yylval.val = 0;
|
2005-07-01 04:29:18 +00:00
|
|
|
while(*c != '\0') {
|
|
|
|
//fprintf(stderr, "looking at %c, state %d\n", *c, state);
|
|
|
|
switch(state) {
|
|
|
|
case ST_SPACE:
|
2005-07-13 02:54:13 +00:00
|
|
|
yylval.val = 0;
|
2005-07-01 04:29:18 +00:00
|
|
|
if(isspace(*c)) {
|
|
|
|
c++;
|
2005-07-15 01:20:11 +00:00
|
|
|
} else if(is_identifier(*c) || is_base_prefix(*c)) {
|
2005-07-14 15:13:58 +00:00
|
|
|
state = ST_IDENTIFIER;
|
2005-07-01 04:29:18 +00:00
|
|
|
} else if(is_operator(*c)) {
|
|
|
|
state = ST_OPERATOR;
|
|
|
|
} else {
|
|
|
|
state = ST_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2005-07-14 15:13:58 +00:00
|
|
|
case ST_IDENTIFIER:
|
|
|
|
{
|
2005-07-15 02:19:07 +00:00
|
|
|
CPUDEBUG_BYTE_METHOD meth;
|
|
|
|
|
2005-07-14 15:13:58 +00:00
|
|
|
char *bufp = idbuf;
|
2005-07-15 01:20:11 +00:00
|
|
|
*bufp++ = *c++; // might be a base prefix
|
|
|
|
while(is_identifier(*c)) { // may NOT be base prefixes
|
2005-07-14 15:13:58 +00:00
|
|
|
*bufp++ = *c++;
|
|
|
|
//fprintf(stderr, "yylval==%d, *c==%c\n", yylval, *c);
|
|
|
|
}
|
|
|
|
*bufp = '\0';
|
2005-07-01 04:29:18 +00:00
|
|
|
state = ST_DEFAULT;
|
|
|
|
|
2005-07-14 15:13:58 +00:00
|
|
|
if(Debugger::debugger().equates()->getAddress(idbuf) > -1) {
|
|
|
|
yylval.equate = idbuf;
|
|
|
|
return EQUATE;
|
2005-07-15 02:19:07 +00:00
|
|
|
} else if( (meth = getByteSpecial(idbuf)) ) {
|
|
|
|
yylval.byteMethod = meth;
|
|
|
|
return BYTE_METHOD;
|
2005-07-14 15:13:58 +00:00
|
|
|
} else {
|
2005-07-15 01:20:11 +00:00
|
|
|
yylval.val = const_to_int(idbuf);
|
2005-07-14 15:13:58 +00:00
|
|
|
return NUMBER;
|
|
|
|
}
|
|
|
|
}
|
2005-07-01 04:29:18 +00:00
|
|
|
|
|
|
|
case ST_OPERATOR:
|
|
|
|
o = *c++;
|
|
|
|
if(!*c) return o;
|
|
|
|
if(isspace(*c)) {
|
|
|
|
state = ST_SPACE;
|
|
|
|
return o;
|
2005-07-15 01:20:11 +00:00
|
|
|
} else if(is_identifier(*c) || is_base_prefix(*c)) {
|
2005-07-14 15:13:58 +00:00
|
|
|
state = ST_IDENTIFIER;
|
2005-07-01 04:29:18 +00:00
|
|
|
return o;
|
|
|
|
} else {
|
|
|
|
state = ST_DEFAULT;
|
|
|
|
p = *c++;
|
|
|
|
//fprintf(stderr, "o==%c, p==%c\n", o, p);
|
|
|
|
if(o == '>' && p == '=')
|
|
|
|
return GTE;
|
|
|
|
else if(o == '<' && p == '=')
|
|
|
|
return LTE;
|
|
|
|
else if(o == '!' && p == '=')
|
|
|
|
return NE;
|
|
|
|
else if(o == '=' && p == '=')
|
|
|
|
return EQ;
|
|
|
|
else if(o == '|' && p == '|')
|
|
|
|
return LOG_OR;
|
|
|
|
else if(o == '&' && p == '&')
|
|
|
|
return LOG_AND;
|
|
|
|
else if(o == '<' && p == '<')
|
|
|
|
return SHL;
|
|
|
|
else if(o == '>' && p == '>')
|
|
|
|
return SHR;
|
|
|
|
else {
|
|
|
|
c--;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ST_DEFAULT:
|
|
|
|
default:
|
2005-07-13 02:54:13 +00:00
|
|
|
yylval.val = 0;
|
2005-07-01 04:29:18 +00:00
|
|
|
if(isspace(*c)) {
|
|
|
|
state = ST_SPACE;
|
2005-07-15 01:20:11 +00:00
|
|
|
} else if(is_identifier(*c) || is_base_prefix(*c)) {
|
2005-07-14 15:13:58 +00:00
|
|
|
state = ST_IDENTIFIER;
|
2005-07-01 04:29:18 +00:00
|
|
|
} else if(is_operator(*c)) {
|
|
|
|
state = ST_OPERATOR;
|
|
|
|
} else {
|
2005-07-13 02:54:13 +00:00
|
|
|
yylval.val = *c++;
|
|
|
|
return yylval.val;
|
2005-07-01 04:29:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//fprintf(stderr, "end of input\n");
|
|
|
|
return 0; // hit NUL, end of input.
|
|
|
|
}
|
|
|
|
|
2005-07-15 01:20:11 +00:00
|
|
|
|
2005-07-01 04:29:18 +00:00
|
|
|
#if 0
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int l;
|
|
|
|
|
|
|
|
set_input(argv[1]);
|
|
|
|
while( (l = yylex()) != 0 )
|
|
|
|
printf("ret %d, %d\n", l, yylval);
|
|
|
|
|
|
|
|
printf("%d\n", yylval);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2005-07-03 14:18:54 +00:00
|
|
|
|
2005-07-13 02:54:13 +00:00
|
|
|
//#ifdef __cplusplus
|
|
|
|
//}
|
|
|
|
//#endif
|