Unix Programming - Re: [Bison] How to execute an if .... then .... else statement / interpreter

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2004 > Re: [Bison] How to execute an if .... then .... else statement / interpreter





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: [Bison] How to execute an if .... then .... else statement / interpreter
Pascal Bourguignon

2004-08-24, 7:06 pm

"Johan" <me@knoware.nl> writes:

> Hi,
>
> Suppose I have a rule to match
>
> IF expression THEN
> statement_list_1
> ELSE
> statement_list_2
> ENDDO
>
> where expression is
>
> NUMBER > 0
> NUMBER < 0
> NUMBER = 0
>
> How do you execute the statement_list_1 or statement_list_2 depending on
> your expression. I can parse the rules but how to execute ?


Of course, with the parse tree, and given the simplicity of this
language, you could interpret it as well. But for more complex
languages, I feel it's better to compile it, otherwise you may have to
handle two stacks, one for data, and one for chunk of "code" generated
on the fly, which is complex enough for certain languages.



(defstruct env
(stack (make-array '(100) :element-type 'integer
:fill-pointer 0 :adjustable t)))


(defun interpret-expression (expr env)
(cond
((atom expr) (error "Invalid syntax expr ~S." expr))
(t (case (first expr)
((integer) (vector-push-extend (second expr) (env-stack env)))
((< = > )
(interpret-expression (second expr) env)
(interpret-expression (third expr) env)
(vector-push-extend (if (funcall (first expr)
(vector-pop (env-stack env))
(vector-pop (env-stack env)))
0 1) (env-stack env)))
(:otherwise
(error "Invalid expression expr ~S." expr))))));;interpret-expression


(defun interpret-statement (stat env)
(cond ((atom stat) (error "Invalid statement ~S." stat))
(t (case (first stat)
((print)
(interpret-expression (second stat) env)
(print (vector-pop (env-stack env))))
((if)
(interpret-expression (second stat) env)
(interpret-statements (if (zerop (vector-pop (env-stack env)))
(fourth stat)
(third stat)) env))
(:otherwise
(error "Invalid statement ~S." stat))))));;interpret-statement


(defun interpret-statements (statements env)
(map nil (lambda (statement) (interpret-statement statement env)) statements))


(defun interpret-program (sexp)
(interpret-statements sexp (make-env)))


CL-USER> (interpret-program '((print (integer 0))
(if (< (integer 42) (integer 0))
((if (= (integer 2) (integer 0))
((print (integer 1)))
((print (integer 2)))))
((print (integer 3))))
(print (integer 4))))


0
3
4
NIL

--
__Pascal Bourguignon__ http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com