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
// 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"
@ -887,6 +887,7 @@ string DebuggerParser::run(const string& command) {
// special case: parser testing
if(strncmp(command.c_str(), "expr ", 5) == 0) {
delete lastExpression;
commandResult = "parser test: status==";
int status = YaccParser::parse(command.c_str() + 5);
commandResult += debugger->valueToString(status);
@ -897,7 +898,8 @@ string DebuggerParser::run(const string& command) {
}
if(command == "expr") {
commandResult = "result==" + debugger->valueToString(lastExpression->evaluate());
if(lastExpression)
commandResult = "result==" + debugger->valueToString(lastExpression->evaluate());
return commandResult;
}

View File

@ -10,7 +10,7 @@ all: stella.y
calctest: stella.y calctest.c YaccParser.cxx YaccParser.hxx
bison -y -d stella.y
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
strip calctest

View File

@ -13,7 +13,7 @@
// See the file "license" for information on usage and redistribution of
// 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
// Copyright (C) 2002-2004 The ScummVM project
@ -34,6 +34,7 @@
#include "ConstExpression.hxx"
#include "DivExpression.hxx"
#include "EqualsExpression.hxx"
#include "EquateExpression.hxx"
#include "Expression.hxx"
#include "GreaterEqualsExpression.hxx"
#include "GreaterExpression.hxx"
@ -69,7 +70,7 @@ const char *input, *c;
enum {
ST_DEFAULT,
ST_NUMBER,
ST_IDENTIFIER,
ST_OPERATOR,
ST_SPACE
};
@ -90,6 +91,11 @@ int parse(const char *in) {
/* 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=='*' || \
x=='/' || x=='<' || x=='>' || \
x=='|' || x=='&' || x=='^' || \
@ -97,6 +103,7 @@ int parse(const char *in) {
x==')' || x=='=' ) )
int yylex() {
static char idbuf[255];
char o, p;
yylval.val = 0;
while(*c != '\0') {
@ -106,8 +113,8 @@ int yylex() {
yylval.val = 0;
if(isspace(*c)) {
c++;
} else if(isdigit(*c)) {
state = ST_NUMBER;
} else if(is_identifier(*c)) {
state = ST_IDENTIFIER;
} else if(is_operator(*c)) {
state = ST_OPERATOR;
} else {
@ -116,28 +123,24 @@ int yylex() {
break;
case ST_NUMBER:
while(isdigit(*c)) {
yylval.val *= 10;
yylval.val += (*c++ - '0');
//fprintf(stderr, "yylval==%d, *c==%c\n", yylval, *c);
}
state = ST_DEFAULT;
return NUMBER;
/*
if(isdigit(*c)) {
yylval *= 10;
yylval += (*c - '0');
c++;
//fprintf(stderr, "*c==0? %d\n", *c==0);
if(*c == '\0') return NUMBER;
break;
} else {
case ST_IDENTIFIER:
{
char *bufp = idbuf;
while(is_identifier(*c)) {
*bufp++ = *c++;
//fprintf(stderr, "yylval==%d, *c==%c\n", yylval, *c);
}
*bufp = '\0';
state = ST_DEFAULT;
return NUMBER;
}
*/
if(Debugger::debugger().equates()->getAddress(idbuf) > -1) {
yylval.equate = idbuf;
return EQUATE;
} else {
yylval.val = atoi(idbuf);
return NUMBER;
}
}
case ST_OPERATOR:
o = *c++;
@ -145,8 +148,8 @@ int yylex() {
if(isspace(*c)) {
state = ST_SPACE;
return o;
} else if(isdigit(*c)) {
state = ST_NUMBER;
} else if(is_identifier(*c)) {
state = ST_IDENTIFIER;
return o;
} else {
state = ST_DEFAULT;
@ -181,8 +184,8 @@ int yylex() {
yylval.val = 0;
if(isspace(*c)) {
state = ST_SPACE;
} else if(isdigit(*c)) {
state = ST_NUMBER;
} else if(is_identifier(*c)) {
state = ST_IDENTIFIER;
} else if(is_operator(*c)) {
state = ST_OPERATOR;
} else {

View File

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

View File

@ -15,10 +15,18 @@ void yyerror(char *e) {
%union {
int val;
char *equate;
Expression *exp;
}
/* Terminals */
%token <val> NUMBER
%token <equate> EQUATE
/* Non-terminals */
%type <exp> expression
/* Operator associativity and precedence */
%left '-' '+'
%left '*' '/' '%'
%left LOG_OR
@ -30,7 +38,6 @@ void yyerror(char *e) {
%nonassoc '<' '>' GTE LTE NE EQ
%nonassoc UMINUS
%type <exp> expression
%%
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, " ()"); $$ = $2; }
| 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,
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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 {
#ifndef YYSTYPE
typedef union {
int val;
char *equate;
Expression *exp;
} YYSTYPE;
/* Line 1318 of yacc.c. */
#line 68 "y.tab.h"
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
} yystype;
# define YYSTYPE yystype
# define YYSTYPE_IS_TRIVIAL 1
#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;
#endif /* not BISON_Y_TAB_H */