1 / 20

A Tiny Language Interpreter, written in RPAL

A Tiny Language Interpreter, written in RPAL. Programming Language Concepts Supplemental Lecture. Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida. Tiny’s Interpreter, in RPAL. let EQ x y = Istruthvalue x & Istruthvalue y -> (x & y) or (not x & not y)

ophira
Download Presentation

A Tiny Language Interpreter, written in RPAL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Tiny Language Interpreter, written in RPAL Programming Language Concepts Supplemental Lecture Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

  2. Tiny’s Interpreter, in RPAL let EQ x y = Istruthvalue x & Istruthvalue y -> (x & y) or (not x & not y) | Isstring x & Isstring y or Isinteger x & Isinteger y -> x eq y | false in let COMP f g x = let R = f x in R @EQ 'error' -> 'error' | g R in

  3. Tiny’s Interpreter, in RPAL let PIPE x f = x @EQ 'error' -> 'error' | (f x) in let Return v s = (v,s) in let Check Dom (v,s) = Dom eq 'Num' -> Isinteger v -> (v,s) | 'error' | Dom eq 'Bool' -> Istruthvalue v -> (v,s) | 'error' | 'error' in

  4. Tiny’s Interpreter, in RPAL let Dummy s = s in let Cond F1 F2 (v,s) = s @PIPE (v -> F1 | F2) in let Replace m i v x = x @EQ i -> v | m x in let Head i = i 1 in let Tail T = rtail T (Order T) where rec rtail T N = N eq 1 -> nil | (rtail T (N-1) aug (T N)) in

  5. Tiny’s Interpreter, in RPAL let rec EE E (m,i,o) = Isinteger E -> Return E (m,i,o) | Isstring E -> ( E eq 'true' -> Return true (m,i,o) | E eq 'false' -> Return false (m,i,o) | E eq 'read' -> Null i -> 'error' | (Head i,(m,Tail i,o)) | (let R = m E in R @EQ 'undef' -> 'error' | (R,(m,i,o)) ) )

  6. Tiny’s Interpreter, in RPAL | Istuple E -> ( (E 1) @EQ 'not' -> (m,i,o) @PIPE EE(E 2) @PIPE (Check 'Bool') @PIPE (fn(v,s).(not v,s))

  7. Tiny’s Interpreter, in RPAL | (E 1) @EQ '<=' -> (m,i,o) @PIPE EE(E 2) @PIPE (Check 'Num') @PIPE (fn(v1,s1). s1 @PIPE EE(E 3) @PIPE (Check 'Num') @PIPE (fn(v2,s2).(v1 le v2,s2)) )

  8. Tiny’s Interpreter, in RPAL | (E 1) @EQ '+' -> (m,i,o) @PIPE EE(E 2) @PIPE (Check 'Num') @PIPE (fn(v1,s1). S1 @PIPE EE(E 3) @PIPE (Check 'Num') @PIPE (fn(v2,s2).(v1 + v2,s2)) ) | 'error' // not 'not', '<=', '+' ) | 'error' // not a tuple in

  9. Tiny’s Interpreter, in RPAL let rec CC C s = not (Istuple C) -> 'error' |(C 1) @EQ ':=' -> s @PIPE EE (C 3) @PIPE (fn(v,s). (Replace (s 1) (C 2) v,s 2,s 3)) |(C 1) @EQ 'print' -> s @PIPE EE (C 2) @PIPE (fn(v,s). (s 1,s 2,s 3 aug v))

  10. Tiny’s Interpreter, in RPAL | (C 1) @EQ 'if' -> s @PIPE EE (C 2) @PIPE (Check 'Bool') @PIPE (Cond (CC(C 3)) (CC(C 4))) | (C 1) @EQ 'while' -> s @PIPE EE (C 2) @PIPE (Check 'Bool') @PIPE Cond (CC(';',C 3,C)) Dummy

  11. Tiny’s Interpreter, in RPAL |(C 1) @EQ ';' -> s @PIPE CC (C 2) @PIPE CC (C 3) | 'error' // not ':=', 'if', ... in let PP P = not (Istuple P) -> (fn i. 'error') | not ((P 1) @EQ 'program') -> (fn i. 'error') | ((fn i. CC (P 2) ((fn i.'undef'),i,nil) //start state! ) @COMP (fn s.(s 3)) ) in

  12. Tiny’s Interpreter, in RPAL Print ( PP ('program', // test program (';', (':=', 'x',3), ('print', 'x') ) ) (nil aug 3) // the input ) Whew ! Now, RUN IT !!

  13. Tiny’s Interpreter, in RPAL • Executable semantic specification of Tiny. • Add a parser, and voilà ... Tiny is implemented ! • Could even write the parser in RPAL ...  • Inefficient, but who cares ... • 'error' (and others) should probably be '<error>', so we allow those as variable names in Tiny. • Subject to change: • Alter order of evaluation of operands. • Allow comparison of booleans.

  14. Extending Tiny • First, add more comparison operators, and lots of arithmetic operators (easy). Example: | (E 1) @EQ '-' -> (m,i,o) @PIPE EE(E 2) @PIPE (Check 'Num') @PIPE (fn(v1,s1). s1 @PIPE EE(E 3) @PIPE (Check 'Num') @PIPE (fn(v2,s2).(v1 - v2,s2)) )

  15. Extending Tiny • Let’s add the '=' comparison operator. Allow for Num and Bool. This allows type mixing ! | (E 1) @EQ '=' -> (m,i,o) @PIPE EE(E 2) @PIPE (Check 'Num') @PIPE (fn(v1,s1). s1 @PIPE EE(E 3) @PIPE (Check 'Num') @PIPE (fn(v2,s2).(v1 eq v2,s2)) )

  16. Add prefix auto-increment operator (E 1) @EQ '++' -> (m,i,o) @PIPE EE(E 2) @PIPE (Check 'Num') @PIPE (fn(v,s). (v+1,(Replace m (E 2)(v+1),i,o) For postfix (n++), change this to v !

  17. Adding the one-armed ‘if’ to Tiny | (C 1) @EQ 'if' -> s @PIPE EE (C 2) @PIPE (Check 'Bool') @PIPE Cond (CC(C 3)) (Order C eq 4 -> CC(C 4) | Dummy )

  18. Adding a ‘do’ statement to Tiny | (C 1) @EQ 'do' -> s @PIPE (C 2) @PIPE EE (C 3) @PIPE (Check 'Bool') @PIPE Cond (CC(C)) Dummy

  19. Adding the C ‘for’ loop to Tiny | (C 1) @EQ 'for' -> s @PIPE CC ('block', C 2, ('while', C 3, ('block', C 5, C 4) ) )

  20. A Tiny Language Interpreter, written in RPAL Programming Language Concepts Supplemental Lecture Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida

More Related