Article View: comp.lang.misc
Article #469Re: What I'd really like to see in an if-statement...
From: Peter da Silva
Date: Mon, 14 Aug 1989 12:01
Date: Mon, 14 Aug 1989 12:01
76 lines
2457 bytes
2457 bytes
In article <14278@haddock.ima.isc.com>, karl@haddock.ima.isc.com (Karl Heuer) writes:
> Okay, then, here's one way to parse such constructs. [ complex explanation
of run-time evaluation of !a < b <= c! using pairs of values ]
Well, that's one way of implementing the run-time code. Doesn't say anything
about parsing it, though. Parsing is easy... in yacc you'd do something like
this:
boolean : expression
| boolean relop expression
;
In a recursive-descent parser:
parse_relop() /* Called on seeing a relation operator */
{
while(is_relop(next_token)) {
shift();
expression();
}
}
Code generation, now. You want to end up with something like this:
For the expression !a < b <= c!...
evaluate A
evaluate B
cmp A,B
bge FAIL
evaluate C, with A possibly scratched.
cmp B,C
ble SUCCEED
FAIL: action on failure (go to end of if stmt, push 0, whatever)
...
SUCCEED:action on success (may be inside if stmt, push 1, whatever)
For a stupid stack machine compiler, putting it in the RD parser above:
parse_relop()
{
LABEL fail = genlab();
LABEL succeed = genlab();
LABEL end = genlab();
while(1) {
TOKEN saved;
saved = next_token;
shift();
expression();
if(is_relop(next_token)) {
gen(SAVE_TOP); /* copies top under second */
gen_branch(reverse_sense(saved), fail);
continue;
} else {
gen_branch(saved, succeed);
break;
}
}
gen_target(fail);
gen_push(FALSE);
gen_branch(always, end);
gen_target(succeed);
gen_push(TRUE);
gen_target(end);
}
You will note that there is considerable room for optimisation :->.
--
Peter da Silva, Xenix Support, Ferranti International Controls Corporation.
Business: peter@ficc.uu.net, +1 713 274 5180. | "The sentence I am now
Personal: peter@sugar.hackercorp.com. `-_-' | writing is the sentence
Quote: Have you hugged your wolf today? 'U` | you are now reading"
Technical Details
Message-ID:
<5669@ficc.uu.net>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!brutus.cs.uiuc.edu!wuarchive!texbell!sugar!ficc!peterReferences:
<8577@batcomputer.tn.cornell.edu> <14251@haddock.ima.isc.com> <14278@haddock.ima.isc.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!brutus.cs.uiuc.edu!wuarchive!texbell!sugar!ficc!peter From: peter@ficc.uu.net (Peter da Silva) Newsgroups: comp.lang.misc Subject: Re: What I'd really like to see in an if-statement... Message-ID: <5669@ficc.uu.net> Date: 14 Aug 89 12:01:37 GMT References: <8577@batcomputer.tn.cornell.edu> <14251@haddock.ima.isc.com> <14278@haddock.ima.isc.com> Organization: Xenix Support, FICC Lines: 76