Advertisement
Guest User

Untitled

a guest
Jun 6th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 8.17 KB | None | 0 0
  1. #lang pl 04
  2.  
  3. ;; ================= Q1 & Q2 ====================
  4.  
  5. ;; Defining two new types
  6. (define-type BIT = (U 0 1))
  7. (define-type Bit-List = (Listof BIT))
  8.  
  9. ;; The actual interpreter
  10. #| BNF for the ROL language:
  11.  
  12.  <ROL> ::= { reg-len = <num> <RegE>}
  13.  
  14.  <RegE> ::= {<Bits>}
  15.           | {and <RegE> <RegE>}
  16.           | {or <RegE> <RegE>}
  17.           | {shl <RegE>}
  18.           | {with {<id> <RegE>} <RegE>}
  19.           | {fun { <id> } <RegE>}
  20.           | {call <RegE> <RegE>}
  21.           | <id>
  22.          
  23.  <Bits> ::= 0
  24.           | 1
  25.           | <Bits> ...
  26.  
  27.  |#
  28. (define-type RegE
  29.   [Reg Bit-List]
  30.   [And RegE RegE]
  31.   [Or RegE RegE]
  32.   [Shl RegE]
  33.   [Id Symbol]
  34.   [With Symbol RegE RegE]
  35.   [Fun Symbol RegE]
  36.   [Call RegE RegE] )
  37. ;; Next is a technical function that converts (casts)
  38. ;; (any) list into a bit-list. We use it in parse-sexpr.
  39. (: list->bit-list : (Listof Any) -> Bit-List)
  40. ;; to cast a list of bits as a bit-list
  41. (define (list->bit-list lst)
  42.   (cond [(null? lst) null]
  43.         [(eq? (first lst) 1)
  44.          (cons 1 (list->bit-list (rest lst)))]
  45.         [else (cons 0 (list->bit-list (rest lst)))]))
  46.  
  47. (: parse-sexpr : Sexpr -> RegE)
  48. ;; to convert the main s-expression into ROL
  49. (define (parse-sexpr sexpr)
  50.   (match sexpr
  51.     [(list 'reg-len '= (number: n) rest) (cond
  52.                                            [(> n 0) (parse-sexpr-RegL rest n)]
  53.                                            [else (error 'parse-sexpr "Register length must be at least 1")])]  
  54.     [else (error 'parse-sexpr "bad syntax in ~s" sexpr)]))
  55.  
  56. (: parse-sexpr-RegL : Sexpr Number -> RegE)
  57. ;; to convert s-expressions into RegEs
  58. (define (parse-sexpr-RegL sexpr reg-len)
  59.   (match sexpr
  60.     [(list (and a (or 1 0)) ... ) (cond
  61.                                     [(equal? (length a) reg-len) (Reg a)]
  62.                                     [else (error 'parse-sexpr "wrong number of bits in ~s" a)])]
  63.     [(list 'and l r) (And (parse-sexpr l) (parse-sexpr r))]
  64.     [(list 'or l r) (Or (parse-sexpr l) (parse-sexpr r))]
  65.     [(list 'shl reg) (Shl (parse-sexpr reg))]
  66.     [(cons 'with more)
  67.      (match sexpr
  68.          [(list 'with (list (symbol: name) named) body)
  69.           (With name (parse-sexpr named) (parse-sexpr body))]
  70.          [else (error 'parse-sexpr "bad `with' syntax in ~s" sexpr)])]
  71.     [(cons 'fun more)
  72.      (match sexpr
  73.          [(list 'fun (list (symbol: name)) body)
  74.           (Fun name (parse-sexpr body))]
  75.          [else (error 'parse-sexpr "bad `fun' syntax in ~s" sexpr)])]
  76.     [(list 'call fun arg) (Call (parse-sexpr fun) (parse-sexpr arg))]
  77.     [else (error 'parse-sexpr "bad syntax in ~s" sexpr)]))
  78. (: parse : String -> RegE)
  79. ;; parses a string containing a RegE expression to a RegE AST
  80. (define (parse str)
  81.   (parse-sexpr (string->sexpr str)))
  82.  
  83. ;; ================= Q3 ====================
  84.  
  85. (: subst : RegE Symbol RegE -> RegE)
  86. ;; substitutes the second argument with the third argument in the
  87. ;; first argument, as per the rules of substitution; the resulting
  88. ;; expression contains no free instances of the second argument   ....
  89. #| Formal specs for `subst':
  90.      (`BL' is a Bit-List, `E1', `E2' are <RegE>s, `x' is some <id>, `y' is a *different* <id>)
  91.         BL[v/x]                = BL
  92.         {and E1 E2}[v/x]      = {and E1[v/x] E2[v/x]}
  93.         {or E1 E2}[v/x]       = {or E1[v/x] E2[v/x]}
  94.         {shl E}[v/x]          = {shl E[v/x]}
  95.         y[v/x]                = y
  96.         x[v/x]                = v
  97.         {with {y E1} E2}[v/x] = {with {y E1[v/x]} E2[v/x]}
  98.         {with {x E1} E2}[v/x] = {with {x E1[v/x]} E2}
  99.         {fun {y} E}[v/x]      = {fun {y} E[v/x]}
  100.         {fun {x} E}[v/x]      = {fun {x} E}
  101.         {call E1 E2}[v/x]     = {call E1[v/x] E2[v/x]}
  102. |#
  103. (define (subst e1 id e2)
  104.   (cases e1
  105.     [(Reg bit-list) e1]
  106.     [(And l r) (And (subst l id e2) (subst r id e2))]
  107.     [(Or l r) (Or (subst l id e2) (subst r id e2))]
  108.     [(Shl e) (Shl (subst e id e2))]
  109.     [(Id x) (if (equal? x id) e2 e1)]
  110.     [(With x exp1 exp2) (if (equal? x id)
  111.                             (With x (subst exp1 id e2) exp2)
  112.                             (With x (subst exp1 id e2) (subst exp2 id e2)))]
  113.     [(Fun bound-id bound-body)
  114.        (if (eq? bound-id id)
  115.          e1
  116.          (Fun bound-id (subst bound-body id e2)))]
  117.     [(Call l r) (Call (subst l id e2) (subst r id e2))]))
  118.  
  119. ;; ================= Q4 ====================
  120.  
  121. (: reg-arith-op : (BIT BIT -> BIT) RegE RegE -> RegE)
  122. ;; Consumes two registers and some binary bit operation 'op',
  123. ;; and returns the register obtained by applying op on the
  124. ;; i'th bit of both registers for all i.
  125. (define (reg-arith-op op reg1 reg2)
  126.   (Reg (bit-arith-op op (Reg->bit-list reg1) (Reg->bit-list reg2))))
  127.  
  128. (: Reg->bit-list : RegE -> Bit-List)
  129. (define (Reg->bit-list e)
  130.   (cases e
  131.     [(Reg n) n]
  132.     [else (error 'reg-arith-op "expects a bit-list, got: ~s" e)]))
  133.  
  134. (: bit-arith-op : (BIT BIT -> BIT) Bit-List Bit-List -> Bit-List)
  135. (define (bit-arith-op op bit-list1 bit-list2)
  136.   (map op (bit-list1) (bit-list2)))
  137.  
  138. (: and-op : BIT BIT -> BIT)
  139. (define (and-op bit1 bit2)
  140.   (* bit1 bit2))
  141.  
  142. (: or-op : BIT BIT -> BIT)
  143. (define (or-op bit1 bit2)
  144.   (if (equal? (and-op bit1 bit2) 1)
  145.            1
  146.            (+ bit1 bit2)))
  147.  
  148. (: shl-op : Bit-List -> Bit-List)
  149. (define (shl-op bitlist)
  150.   (if (equal? (length bitlist) 1)
  151.       bitlist
  152.     (append (rest bitlist) (list (first bitlist)))))
  153.  
  154. (: eval : RegE -> RegE)
  155. ;; evaluates RegE expressions by reducing them to RegE that is
  156. ;; of the two variants (Reg..) or (Fun..)  ....
  157.  #| Formal specs for `eval':
  158.     eval(bl)          = bl
  159.     eval({and E1 E2}) = (<x1 bit-and y1> <x2 bit-and y2> ... <xk bit-and yk>)  
  160.        where eval(E1) = (x1 x2 ... xk) and eval(E2) = (y1 y2 ... yk)
  161.  
  162.     eval({or E1 E2}) = (<x1 bit-or y1> <x2 bit-or y2> ... <xk bitor yk>)  
  163.       where eval(E1) = (x1 x2 ... xk) and eval(E2) = (y1 y2 ... yk)
  164.        
  165.     eval({shl E}) = (x2 ... xk x1), where eval(E) = (x1 x2 ... xk)
  166.     eval(id)        = error!
  167.     eval({with {x E1} E2}) = eval(E2[eval(E1)/x])
  168.     eval(FUN) = FUN ;     assuming FUN is a function expression
  169.     eval({call E1 E2}) = eval(Ef[eval(E2)/x])  
  170.                                if eval(E1)={fun {x} Ef}
  171.                        = error!   otherwise
  172.   |#
  173. (define (eval expr)
  174.   (cases expr
  175.     [(Reg bit-list) expr]
  176.     [(And l r) (reg-arith-op and-op (eval l) (eval l))]
  177.     [(Or l r) (reg-arith-op or-op (eval l) (eval l))]
  178.     [(Shl exp) (Reg (shl-op (Reg->bit-list (eval exp))))]
  179.     [(With bound-id named-expr bound-body)
  180.      (eval (subst bound-body
  181.                   bound-id
  182.                   (eval named-expr)))]
  183.     [(Id name) (error 'eval "free identifier: ~s" name)]
  184.     [(Fun bound-id bound-body) expr]
  185.     [(Call fun-expr arg-expr)
  186.      (let ([fval (eval fun-expr)])
  187.        (cases fval
  188.          [(Fun bound-id bound-body)
  189.           (eval (subst bound-body
  190.                        bound-id
  191.                        (eval arg-expr)))]
  192.          [else (error 'eval "`call' expects a function, got: ~s"
  193.                       fval)]))]))
  194.  
  195. ;; ================= Q5 ====================
  196. (: run : String -> Bit-List)
  197. ;; evaluate a ROL program contained in a string
  198. (define (run str)
  199.   (let ([result (eval (parse str))])
  200.     (cases result
  201.       [(Reg n) n]
  202.       [else (error 'run "evaluation returned a non-number: ~s" result)])))
  203.  
  204.  
  205. ;; tests
  206. (test (run "{ reg-len = 4 {1 0 0 0}}") => '(1 0 0 0))
  207. ;;(test (run "{ reg-len = 4 {shl {1 0 0 0}}}") => '(0 0 0 1))
  208. ;;(test (run "{ reg-len = 4 {and {shl {1 0 1 0}}{shl {1 0 1 0}}}}") => '(0 1 0 1))
  209. ;;(test (run "{ reg-len = 4 { or {and {shl {1 0 1 0}} {shl {1 0 0 1}}} {1 0 1 0}}}") => '(1 0 1 1))
  210. ;;(test (run "{ reg-len = 2 { or {and {shl {1 0}} {1 0}} {1 0}}}") => '(1 0))
  211. ;;(test (run "{ reg-len = 4 {with {x {1 1 1 1}} {shl y}}}") =error> "free identifier")
  212. ;;(test (run "{ reg-len = 2 { with {x { or {and {shl {1 0}} {1 0}} {1 0}}} {shl x}}}") => '(0 1))
  213. ;;(test (run "{ reg-len = 4 {or {1 1 1 1} {0 1 1}}}") =error>"wrong number of bits in")
  214. ;;(test (run "{ reg-len = 0 {}}") =error> "Register length must be at least 1")
  215. ;;(test (run "{ reg-len = 3 {with {identity {fun {x} x}} {with {foo {fun {x} {or x {1 1 0}}}} {call {call identity foo} {0 1 0}}}}}"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement