Re: [Bison] How to execute an if .... then .... else statement / interpreter
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Re: [Bison] How to execute an if .... then .... else statement / interpreter




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

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


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-25-04 12:06 AM

"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.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:35 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register