YaccParser now supports EquateExpressions

git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@645 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
urchlay 2005-07-14 15:13:58 +00:00
parent 59524329ef
commit 8476dcd6d1
7 changed files with 791 additions and 1002 deletions

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
// //
// $Id: DebuggerParser.cxx,v 1.55 2005-07-14 11:51:03 stephena Exp $ // $Id: DebuggerParser.cxx,v 1.56 2005-07-14 15:13:58 urchlay Exp $
//============================================================================ //============================================================================
#include "bspf.hxx" #include "bspf.hxx"
@ -887,6 +887,7 @@ string DebuggerParser::run(const string& command) {
// special case: parser testing // special case: parser testing
if(strncmp(command.c_str(), "expr ", 5) == 0) { if(strncmp(command.c_str(), "expr ", 5) == 0) {
delete lastExpression;
commandResult = "parser test: status=="; commandResult = "parser test: status==";
int status = YaccParser::parse(command.c_str() + 5); int status = YaccParser::parse(command.c_str() + 5);
commandResult += debugger->valueToString(status); commandResult += debugger->valueToString(status);
@ -897,6 +898,7 @@ string DebuggerParser::run(const string& command) {
} }
if(command == "expr") { if(command == "expr") {
if(lastExpression)
commandResult = "result==" + debugger->valueToString(lastExpression->evaluate()); commandResult = "result==" + debugger->valueToString(lastExpression->evaluate());
return commandResult; return commandResult;
} }

View File

@ -10,7 +10,7 @@ all: stella.y
calctest: stella.y calctest.c YaccParser.cxx YaccParser.hxx calctest: stella.y calctest.c YaccParser.cxx YaccParser.hxx
bison -y -d stella.y bison -y -d stella.y
g++ -DPRINT -I../debugger -O2 -c YaccParser.cxx g++ -DPRINT -I../debugger -O2 -c YaccParser.cxx
g++ -I../debugger -O2 -c calctest.c g++ -DBM -I../debugger -O2 -c calctest.c
g++ -I../debugger -O2 -Wall -o calctest calctest.o YaccParser.o ../debugger/*Expression.o g++ -I../debugger -O2 -Wall -o calctest calctest.o YaccParser.o ../debugger/*Expression.o
strip calctest strip calctest

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of // See the file "license" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES. // this file, and for a DISCLAIMER OF ALL WARRANTIES.
// //
// $Id: YaccParser.cxx,v 1.4 2005-07-13 04:49:19 urchlay Exp $ // $Id: YaccParser.cxx,v 1.5 2005-07-14 15:13:58 urchlay Exp $
// //
// Based on code from ScummVM - Scumm Interpreter // Based on code from ScummVM - Scumm Interpreter
// Copyright (C) 2002-2004 The ScummVM project // Copyright (C) 2002-2004 The ScummVM project
@ -34,6 +34,7 @@
#include "ConstExpression.hxx" #include "ConstExpression.hxx"
#include "DivExpression.hxx" #include "DivExpression.hxx"
#include "EqualsExpression.hxx" #include "EqualsExpression.hxx"
#include "EquateExpression.hxx"
#include "Expression.hxx" #include "Expression.hxx"
#include "GreaterEqualsExpression.hxx" #include "GreaterEqualsExpression.hxx"
#include "GreaterExpression.hxx" #include "GreaterExpression.hxx"
@ -69,7 +70,7 @@ const char *input, *c;
enum { enum {
ST_DEFAULT, ST_DEFAULT,
ST_NUMBER, ST_IDENTIFIER,
ST_OPERATOR, ST_OPERATOR,
ST_SPACE ST_SPACE
}; };
@ -90,6 +91,11 @@ int parse(const char *in) {
/* hand-rolled lexer. Hopefully faster than flex... */ /* hand-rolled lexer. Hopefully faster than flex... */
#define is_identifier(x) ( (x>='0' && x<='9') || \
(x>='a' && x<='z') || \
(x>='A' && x<='Z') || \
(x=='.' && x<='_') )
#define is_operator(x) ( (x=='+' || x=='-' || x=='*' || \ #define is_operator(x) ( (x=='+' || x=='-' || x=='*' || \
x=='/' || x=='<' || x=='>' || \ x=='/' || x=='<' || x=='>' || \
x=='|' || x=='&' || x=='^' || \ x=='|' || x=='&' || x=='^' || \
@ -97,6 +103,7 @@ int parse(const char *in) {
x==')' || x=='=' ) ) x==')' || x=='=' ) )
int yylex() { int yylex() {
static char idbuf[255];
char o, p; char o, p;
yylval.val = 0; yylval.val = 0;
while(*c != '\0') { while(*c != '\0') {
@ -106,8 +113,8 @@ int yylex() {
yylval.val = 0; yylval.val = 0;
if(isspace(*c)) { if(isspace(*c)) {
c++; c++;
} else if(isdigit(*c)) { } else if(is_identifier(*c)) {
state = ST_NUMBER; state = ST_IDENTIFIER;
} else if(is_operator(*c)) { } else if(is_operator(*c)) {
state = ST_OPERATOR; state = ST_OPERATOR;
} else { } else {
@ -116,28 +123,24 @@ int yylex() {
break; break;
case ST_NUMBER: case ST_IDENTIFIER:
while(isdigit(*c)) { {
yylval.val *= 10; char *bufp = idbuf;
yylval.val += (*c++ - '0'); while(is_identifier(*c)) {
*bufp++ = *c++;
//fprintf(stderr, "yylval==%d, *c==%c\n", yylval, *c); //fprintf(stderr, "yylval==%d, *c==%c\n", yylval, *c);
} }
*bufp = '\0';
state = ST_DEFAULT; state = ST_DEFAULT;
return NUMBER;
/* if(Debugger::debugger().equates()->getAddress(idbuf) > -1) {
if(isdigit(*c)) { yylval.equate = idbuf;
yylval *= 10; return EQUATE;
yylval += (*c - '0');
c++;
//fprintf(stderr, "*c==0? %d\n", *c==0);
if(*c == '\0') return NUMBER;
break;
} else { } else {
state = ST_DEFAULT; yylval.val = atoi(idbuf);
return NUMBER; return NUMBER;
} }
*/ }
case ST_OPERATOR: case ST_OPERATOR:
o = *c++; o = *c++;
@ -145,8 +148,8 @@ int yylex() {
if(isspace(*c)) { if(isspace(*c)) {
state = ST_SPACE; state = ST_SPACE;
return o; return o;
} else if(isdigit(*c)) { } else if(is_identifier(*c)) {
state = ST_NUMBER; state = ST_IDENTIFIER;
return o; return o;
} else { } else {
state = ST_DEFAULT; state = ST_DEFAULT;
@ -181,8 +184,8 @@ int yylex() {
yylval.val = 0; yylval.val = 0;
if(isspace(*c)) { if(isspace(*c)) {
state = ST_SPACE; state = ST_SPACE;
} else if(isdigit(*c)) { } else if(is_identifier(*c)) {
state = ST_NUMBER; state = ST_IDENTIFIER;
} else if(is_operator(*c)) { } else if(is_operator(*c)) {
state = ST_OPERATOR; state = ST_OPERATOR;
} else { } else {

View File

@ -20,7 +20,8 @@ int main(int argc, char **argv) {
Expression *e = YaccParser::getResult(); Expression *e = YaccParser::getResult();
for(int i=0; i<100000000; i++) { for(int i=0; i<100000000; i++) {
printf("%d\n", e->evaluate()); // printf("%d\n", e->evaluate());
e->evaluate();
} }
#endif #endif

View File

@ -15,10 +15,18 @@ void yyerror(char *e) {
%union { %union {
int val; int val;
char *equate;
Expression *exp; Expression *exp;
} }
/* Terminals */
%token <val> NUMBER %token <val> NUMBER
%token <equate> EQUATE
/* Non-terminals */
%type <exp> expression
/* Operator associativity and precedence */
%left '-' '+' %left '-' '+'
%left '*' '/' '%' %left '*' '/' '%'
%left LOG_OR %left LOG_OR
@ -30,7 +38,6 @@ void yyerror(char *e) {
%nonassoc '<' '>' GTE LTE NE EQ %nonassoc '<' '>' GTE LTE NE EQ
%nonassoc UMINUS %nonassoc UMINUS
%type <exp> expression
%% %%
statement: expression { fprintf(stderr, "\ndone\n"); result.exp = $1; } statement: expression { fprintf(stderr, "\ndone\n"); result.exp = $1; }
@ -61,5 +68,6 @@ expression: expression '+' expression { fprintf(stderr, " +"); $$ = new PlusExpr
| '>' expression { fprintf(stderr, " >"); /* $$ = new HiByteExpression($2); */ } | '>' expression { fprintf(stderr, " >"); /* $$ = new HiByteExpression($2); */ }
| '(' expression ')' { fprintf(stderr, " ()"); $$ = $2; } | '(' expression ')' { fprintf(stderr, " ()"); $$ = $2; }
| NUMBER { fprintf(stderr, " %d", $1); $$ = new ConstExpression($1); } | NUMBER { fprintf(stderr, " %d", $1); $$ = new ConstExpression($1); }
| EQUATE { fprintf(stderr, " %s", $1); $$ = new EquateExpression($1); }
; ;
%% %%

File diff suppressed because it is too large Load Diff

View File

@ -1,76 +1,29 @@
/* A Bison parser, made by GNU Bison 2.0. */ #ifndef BISON_Y_TAB_H
# define BISON_Y_TAB_H
/* Skeleton parser for Yacc-like parsing with Bison, #ifndef YYSTYPE
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. typedef union {
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* As a special exception, when this file is copied by Bison into a
Bison output file, you may use that output file without restriction.
This special exception was added by the Free Software Foundation
in version 1.24 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
NUMBER = 258,
LOG_OR = 259,
LOG_AND = 260,
LOG_NOT = 261,
SHL = 262,
SHR = 263,
EQ = 264,
NE = 265,
LTE = 266,
GTE = 267,
UMINUS = 268
};
#endif
#define NUMBER 258
#define LOG_OR 259
#define LOG_AND 260
#define LOG_NOT 261
#define SHL 262
#define SHR 263
#define EQ 264
#define NE 265
#define LTE 266
#define GTE 267
#define UMINUS 268
#if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED)
#line 16 "stella.y"
typedef union YYSTYPE {
int val; int val;
char *equate;
Expression *exp; Expression *exp;
} YYSTYPE; } yystype;
/* Line 1318 of yacc.c. */ # define YYSTYPE yystype
#line 68 "y.tab.h"
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
# define YYSTYPE_IS_TRIVIAL 1 # define YYSTYPE_IS_TRIVIAL 1
#endif #endif
# define NUMBER 257
# define EQUATE 258
# define LOG_OR 259
# define LOG_AND 260
# define LOG_NOT 261
# define SHR 262
# define SHL 263
# define GTE 264
# define LTE 265
# define NE 266
# define EQ 267
# define UMINUS 268
extern YYSTYPE yylval; extern YYSTYPE yylval;
#endif /* not BISON_Y_TAB_H */