Parsing/evaluating shortcutting operators

Jeremy J Starcher <r3jjs@yahoo.com>
Tue, 13 Jan 2009 22:56:42 GMT

          From comp.compilers

Related articles
Parsing/evaluating shortcutting operators r3jjs@yahoo.com (Jeremy J Starcher) (2009-01-13)
Re: Parsing/evaluating shortcutting operators bartc@freeuk.com (Bartc) (2009-01-15)
Re: Parsing/evaluating shortcutting operators r3jjs@yahoo.com (Jeremy J Starcher) (2009-01-17)
| List of all articles for this month |

From: Jeremy J Starcher <r3jjs@yahoo.com>
Newsgroups: comp.compilers
Date: Tue, 13 Jan 2009 22:56:42 GMT
Organization: at&t http://my.att.net/
Keywords: code, question, comment
Posted-Date: 14 Jan 2009 20:10:34 EST

I have written a strongly typed BASIC compiler and virtual machine in
Javascript, just for the exercise in compiler writing and "behind the
scenes" insight of high level languages after coming from an assembly/C
background.


I decided use a basic stack machine for simplicity.


I am unsure how to proceed in setting up shortcutting operators
however. For example, this code:


declare boolean b


b = (1 == 2) || (3 == 4)


Which results in the expression
1 2 == 3 4 == || b =


And the opcodes of:
(calli calls an internal/library function)


push_int 1
push_int 2
calli_integer_compare
push_int 3
push_int 4
calli_integer_compare
calli boolean compare
push_var 1
calli boolean_assign


The only approach I can think of is to really mangle my source and turn it
into more like this pcode: (Variables beginning with t are temp
variables.)




t1 = 1 == 2
if !t1 { // False value, so go onto the next case.
    t1 = 2 == 3
}
b = t1


Is there a better approach?
Does someone have an example of a parser that handles shortcutting
operators that I can take a look at?
[Shortcut evaluation is really a shorthand for the if/else you discovered.
That's pretty much how you have to do it. -John]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.