Related articles |
---|
Calculating expressions with several types div@infonet.nnov.ru (=?koi8-r?B?4c7Uz84=?=) (2000-12-11) |
Re: Calculating expressions with several types lgcl01@es.co.nz (Fairbairn Family) (2000-12-13) |
Re: Calculating expressions with several types chris@cjl1.demon.co.uk (Chris Locke) (2000-12-13) |
Re: Calculating expressions with several types jparis11@home.com (Jean Pariseau) (2000-12-18) |
From: | =?koi8-r?B?4c7Uz84=?= <div@infonet.nnov.ru> |
Newsgroups: | comp.compilers |
Date: | 11 Dec 2000 02:02:23 -0500 |
Organization: | Compilers Central |
Keywords: | interpreter, question, comment |
Posted-Date: | 11 Dec 2000 02:02:23 EST |
Hello!
Some weeks ago I began writing my own interpreter.
Everything was good until I wanted to use different types (int,str,real).
But I've no idea how to calculate an expression with several types!
The algorithm for calculating integer expressions is ready.
By the way, I use recursive-descent algorithm.
Do you know how to extend it to support these types?
I need your help VERY-VERY much. I'll be very grateful for your help. :-)
I'm a student...
Don Antonio.
Thank you.
The part of the source code is follows:
int evalute () {
gettoken();
return relop();
}
void assign() {
curvar=vartable.find(tokstr);
if( curvar==NULL ) showerr(ERR_VARNAME);
TVariable *t=curvar;
gettoken();
if( curtok!=EQUAL ) showerr(ERR_MISSED_EQUAL);
t->value=evalute();
}
int relop() {
int r=expr();
switch(curtok) {
case LESS:
gettoken();
r=r<expr();
break;
case BIGGER:
gettoken();
r=r>expr();
break;
case LE:
gettoken();
r=r<=expr();
break;
case BE:
gettoken();
r=r>=expr();
break;
case ISEQ:
gettoken();
r=r==expr();
break;
case NOTEQ:
gettoken();
r=r!=expr();
break;
}
return r;
}
int expr() {
int r=term();
for(;;) {
switch(curtok) {
case PLUS:
gettoken();
r+=term();
break;
case MINUS:
gettoken();
r-=term();
break;
default:
return r;
}
}
}
int term() {
int r=prim();
int p;
for(;;) {
gettoken();
switch(curtok) {
case MULT:
gettoken();
r*=prim();
break;
case DIV:
gettoken();
p=prim();
if( p==0 ) showerr(ERR_DIV_ZERO);
r/=p;
break;
case MOD:
gettoken();
p=prim();
if( p==0 ) showerr(ERR_DIV_ZERO);
r%=p;
break;
default:
return r;
}
}
}
int prim() {
int r;
switch(curtok) {
case NUMBER: r=curnum;
break;
case VAR: curvar=vartable.find(tokstr);
if( curvar==NULL ) showerr(ERR_VARNAME);
r=curvar->value;
break;
case MINUS: gettoken();
r=-prim();
break;
case LB: gettoken();
r=expr();
if( curtok!=RB ) showerr(ERR_MISSED_BRACKET);
break;
/*default: showerr(ERR_ILLEGAL_TOKEN);*/
}
return r;
}
[You have to keep track of the type of each subexpression, and execute
the operators as needed, doing type conversions if you need to. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.