Advertisement
Guest User

Untitled

a guest
May 25th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.71 KB | None | 0 0
  1. ; Israel Hill idh
  2. ; Michael Rosenfield mer95
  3. ; Sarah Whelan slw96
  4.  
  5. ; Part 3
  6.  
  7. (load "classParser.scm")
  8. (load "lex.scm")
  9.  
  10. ;main interpret function
  11. (define interpret
  12. (lambda (filename classname)
  13. (lookup 'return (stateFunctionCall '(funcall main) (lookup (string->symbol classname) (interpretClasses (parser filename) (initialEmpty))) (lambda (v) v)))))
  14.  
  15. (define interpretClasses
  16. (lambda (l state)
  17. (cond
  18. ((null? l) state)
  19. ((eq? (car l) 'class)(Add (className l) (decideStateClass (classBody l) (makeClass l) (lambda (v) v) (lambda (v) (v)) (lambda (v) v) (lambda (v) v)) state))
  20. (else (interpretClasses (cdr l) (interpretClasses (car l) state))))))
  21.  
  22. (define initialEmpty
  23. (lambda ()
  24. '(()())))
  25.  
  26. (define makeClass
  27. (lambda (l)
  28. (cond
  29. ((null? (classHeader l)) (cons (classHeader l) (cons (initialEmpty) (cons (initialEmpty) (cons (initialEmpty) '())))))
  30. (else (cons (car (cdr (classHeader l))) (cons (initialEmpty) (cons (initialEmpty) (cons (initialEmpty) '()))))))))
  31.  
  32. (define decideStateClass
  33. (lambda (l state return continue break exit)
  34. (cond
  35. ((null? l) (return state))
  36. ((list? (operator l)) (decideStateClass (operator l) state (lambda (v) (decideStateClass (cdr l) v return continue break exit)) continue break exit))
  37. ((eq? (operator l) 'function) (cons (classParent state) (cons (classFields state) (cons (stateFunction l (classMethods state) return continue break exit) (classInitials state)))))
  38. ;((eq? (operator l) 'static-function) (cons (classParent state) (cons (classFields state) (cons (stateFunction l (classMethods state) return continue break exit) (classInitials state)))))
  39. ((eq? (operator l) 'static-function) (stateFunction l (classMethods state) (lambda (v) (cons (classParent state) (cons (classFields state) (cons v (classInitials state))))) continue break exit))
  40. ((eq? (operator l) 'static-var) (cons (classParent state) (cons (stateDeclaration l (classFields state) return continue break exit) (cons (classMethods state) (classInitials state)))))
  41. ((eq? (operator l) 'var) (cons (classParent state) (cons (classFields state) (cons (classMethods state) (stateDeclaration l (classInitials state) return break continue exit)))))
  42. (else (return state)))))
  43.  
  44. (define parserOutput
  45. (lambda (filename classname)
  46. (parser filename)))
  47.  
  48. ;adds global variables and creates functions
  49. (define interpretOuter
  50. (lambda (l)
  51. (decideStateOuter l (initialStateWithReturn) (lambda (v) v) (lambda (v) (v)) (lambda (v) v) (lambda (v) v))))
  52.  
  53. ;the default state
  54. (define initialStateWithReturn
  55. (lambda ()
  56. (cons '(true false return) (cons (cons (box #t) (cons (box #f)(cons (box 'noReturnValueSet) '()))) '()))))
  57.  
  58. ;the additonal layer for each function call
  59. (define initialState
  60. (lambda ()
  61. (cons '(true false) (cons (cons (box #t) (cons (box #f) '())) '()))))
  62.  
  63. ;;;;;; Interpret Each Class
  64.  
  65. ;only allows variables and functions
  66. (define decideStateOuter
  67. (lambda (l state return continue break exit)
  68. (cond
  69. ((null? l) (return state))
  70. ((list? (operator l)) (decideStateOuter (operator l) state (lambda (v) (decideStateOuter (cdr l) v return continue break exit)) continue break exit))
  71. ((eq? (operator l) 'function) (stateFunction l state return continue break exit))
  72. ((eq? (operator l) 'var) (stateDeclaration l state return continue break exit))
  73. ((eq? (operator l) '=) (stateAssign l state return continue break exit))
  74. (else (return state)))))
  75.  
  76. ;;;;;; Interpret Function
  77.  
  78. ;decide state determines and changes the state of a statement
  79. (define decideState
  80. (lambda (l state return continue break exit)
  81. (cond
  82. ((null? l) (return state))
  83. ((atom? l) (return state))
  84. ((list? (operator l)) (decideState (operator l) state (lambda (v) (decideState (cdr l) v return continue break exit)) continue break exit))
  85. ((eq? (operator l) 'return) (stateReturn l state return continue break exit))
  86. ((eq? (operator l) 'while) (stateWhile l state return continue (lambda (v) (return (removeLayer v))) exit))
  87. ((eq? (operator l) 'function) (stateFunction l state return continue break exit))
  88. ((eq? (operator l) 'funcall) (stateFunctionCall l state return))
  89. ((eq? (operator l) 'var) (stateDeclaration l state return continue break exit))
  90. ((eq? (operator l) 'if) (stateIf l state return continue break exit))
  91. ((eq? (operator l) 'break) (break state))
  92. ((eq? (operator l) 'begin) (stateBlock l (addLayer state) return continue break exit))
  93. ((eq? (operator l) 'continue) (continue (removeLayer state)))
  94. ((eq? (operator l) '=) (stateAssign l state return continue break exit))
  95. (else (return state))
  96. )))
  97.  
  98. ;handles return statements by exiting to the beginning of the program (using the exit continuation set at the inital call of stateDecide)
  99. (define stateReturn
  100. (lambda (l state return continue break exit)
  101. (cond
  102. ((eq? (getValue (cdr l) state) '#t) (exit (Update 'return 'true state)))
  103. ((eq? (getValue (cdr l) state) '#f) (exit (Update 'return 'false state)))
  104. (else (exit (Update 'return (getValue (cdr l) state) state))))))
  105.  
  106. ;handles variable declarations
  107. (define stateDeclaration
  108. (lambda (l state return continue break exit)
  109. (cond
  110. ((not (null? (lookup (leftoperand l) (topLayer state)))) (error 'variableAlreadyDeclared))
  111. ((null? (cdr (cdr l))) (return (Add (leftoperand l) 'declared state)))
  112. (else (return (Add (leftoperand l) (getValue (rightoperand l) state) state))))))
  113.  
  114. ;handles variable assignments
  115. (define stateAssign
  116. (lambda (l state return continue break exit)
  117. (let ([variable (lookup (leftoperand l) state)])
  118. (cond
  119. ((null? variable) (error 'usingBeforeDeclaringOrOutOfScope))
  120. ((or (eq? variable 'declared) (atom? variable)) (return (Update (leftoperand l) (getValue (rightoperand l) state) state)))
  121. (else (return (Add (leftoperand l)(getValue (rightoperand l) state) state)))))))
  122.  
  123. ;handles if statements
  124. (define stateIf
  125. (lambda (l state return continue break exit)
  126. (cond
  127. ((getTruth (condition l) state) (decideState (ifBody l) state return continue break exit))
  128. ((null? (elseBody l))(return state))
  129. (else (decideState (elseBody l) state return continue break exit)))))
  130.  
  131. ;handles blocks
  132. (define stateBlock
  133. (lambda (l state return continue break exit)
  134. (cond
  135. ((null? l) (return (removeLayer state)))
  136. (else (decideState (front l) state (lambda (v)
  137. (stateBlock (remaining l) v return continue break exit)) (lambda (v) (return v)) break exit)))))
  138.  
  139. ;handles while loops
  140. (define stateWhile
  141. (lambda (l state return continue break exit)
  142. (cond
  143. ((null? l) state)
  144. ((getTruth (leftoperand l) state) (decideState (leftoperand l) state (lambda (v1)
  145. (decideState (rightoperand l) v1 (lambda (v2)(decideState l v2 return continue break exit)) continue break exit)) continue break exit))
  146. (else (decideState (leftoperand l) state return continue break exit)))))
  147.  
  148. ;handles function definitions
  149. (define stateFunction
  150. (lambda (l state return continue break exit)
  151. (return (Add (functionName l) (makeClosure (functionBody l) (functionParamList l) state) state))))
  152.  
  153. ;evaluate a functioncall
  154. (define stateFunctionCall
  155. (lambda (l state return)
  156. (cond
  157. ((eq? (lookup (functionCallName l) state) '()) (error 'illegalFunctionCall))
  158. (else (let ([closure (lookup (functionCallName l) state)])(decideState (functionClosureBody closure)
  159. (copyParams (functionCallParamList l) state (functionClosureParamList closure)
  160. (addLayer (getLastN state (getFunctionClosureLayerNum closure)))
  161. )
  162. (lambda (v) (return state))
  163. (lambda (v) (return state)) (lambda (v) (return state)) (lambda (v) (return state))))))))
  164.  
  165. ;;;;;; Value
  166.  
  167. ;returns the value of an expression
  168. (define getValue
  169. (lambda (expression state)
  170. (cond
  171. ((number? expression) expression)
  172. ((and (atom? expression) (eq? (lookup expression state) 'declared)) (error 'usingBeforeAssigning))
  173. ((and (atom? expression) (eq? (lookup expression state) '())) (error 'usingBeforeDeclaringOrOutOfScope))
  174. ((atom? expression) (lookup expression state))
  175. ((eq? (operator expression) 'funcall) (lookup 'return (stateFunctionCall expression state (lambda (v) v))))
  176.  
  177. ((eq? '+ (operator expression)) (+ (getValue (leftoperand expression) state)
  178. (getValue (rightoperand expression) state)))
  179. ((eq? '/ (operator expression)) (quotient (getValue (leftoperand expression) state)
  180. (getValue (rightoperand expression) state)))
  181. ((eq? '% (operator expression)) (remainder (getValue (leftoperand expression) state)
  182. (getValue (rightoperand expression) state)))
  183. ((eq? '* (operator expression)) (* (getValue (leftoperand expression) state)
  184. (getValue (rightoperand expression) state)))
  185. ((and (eq? '- (operator expression))(not (null? (cdr (cdr expression)))))
  186. (- (getValue (leftoperand expression) state)(getValue (rightoperand expression) state)))
  187. ((eq? '- (operator expression)) (- (getValue (leftoperand expression) state)))
  188. ((eq? '= (operator expression)) (getValue (leftoperand expression)(Update (leftoperand expression) (getValue (rightoperand expression) state) state)))
  189. ((eq? 'var (operator expression)) (getValue (rightoperand expression) state))
  190.  
  191. ((eq? '!= (operator expression)) (getTruth expression state))
  192. ((eq? '== (operator expression)) (getTruth expression state))
  193. ((eq? '<= (operator expression)) (getTruth expression state))
  194. ((eq? '>= (operator expression)) (getTruth expression state))
  195. ((eq? '< (operator expression)) (getTruth expression state))
  196. ((eq? '> (operator expression)) (getTruth expression state))
  197. ((eq? '! (operator expression)) (getTruth expression state))
  198. ((eq? '&& (operator expression)) (getTruth expression state))
  199. ((eq? '|| (operator expression)) (getTruth expression state))
  200. ((null? (cdr expression)) (getValue (operator expression) state))
  201. (else (error 'illegalExpression)))
  202. ))
  203.  
  204. ;evaluates boolean result of an expression
  205. (define getTruth
  206. (lambda (expression state)
  207. (cond
  208. ((number? expression) expression)
  209. ((not (pair? expression)) (lookup expression state))
  210. ((eq? '< (operator expression)) (< (getValue (leftoperand expression) state)
  211. (getValue (rightoperand expression) state)))
  212. ((eq? '> (operator expression)) (> (getValue (leftoperand expression) state)
  213. (getValue (rightoperand expression) state)))
  214. ((eq? '<= (operator expression)) (<= (getValue (leftoperand expression) state)
  215. (getValue (rightoperand expression) state)))
  216. ((eq? '>= (operator expression)) (>= (getValue (leftoperand expression) state)
  217. (getValue (rightoperand expression) state)))
  218. ((eq? '== (operator expression)) (eq? (getValue (leftoperand expression) state)
  219. (getValue (rightoperand expression) state)))
  220. ((eq? '!= (operator expression)) (not(eq? (getValue (leftoperand expression) state)
  221. (getValue (rightoperand expression) state))))
  222. ((eq? '&& (operator expression)) (and (getValue (leftoperand expression) state)
  223. (getValue (rightoperand expression) state)))
  224. ((eq? '|| (operator expression)) (or (getValue (leftoperand expression) state)
  225. (getValue (rightoperand expression) state)))
  226. ((eq? '! (operator expression)) (not(getValue (leftoperand expression)state)))
  227. ((eq? (operator expression) 'funcall) (lookup 'return (stateFunctionCall expression state (lambda (v) v))))
  228. ((eq? '= (operator expression)) (getValue (leftoperand expression) (Update (leftoperand expression) (getValue (rightoperand expression) state) state)))
  229. )))
  230.  
  231. ;;;;;; Function Helpers
  232.  
  233. ;make a function closure to store in the environment
  234. (define makeClosure
  235. (lambda (body params state)
  236. (cons params (cons body (cons (getNumLayers state) '())))))
  237.  
  238. ;copy the actual paramters into the formal parameters
  239. (define copyParams
  240. (lambda (actual state formal stateFromClosure)
  241. (cond
  242. ((not (equalNumElements? actual formal)) (error 'mismatchParameters))
  243. ((null? actual) stateFromClosure)
  244. (else (copyParams (remaining actual) state (remaining formal) (Add (front formal) (getValue (front actual) state) stateFromClosure))))))
  245.  
  246. ;a check for equal number of parameters
  247. (define equalNumElements?
  248. (lambda (l1 l2)
  249. (cond
  250. ((and (null? l1) (null? l2)) #t)
  251. ((and (null? l1) (not (null? l2))) #f)
  252. ((and (null? l2) (not (null? l1))) #f)
  253. (else (equalNumElements? (cdr l1) (cdr l2))))))
  254.  
  255. ;get the number of layers in the state to store with each function
  256. (define getNumLayers
  257. (lambda (l)
  258. (cond
  259. ((null? l) 0)
  260. ((atom? (singleLayerTest l)) 1)
  261. (else (+ 1 (getNumLayers (cdr l)))))))
  262.  
  263. ;get the last 'n' number of layers
  264. (define getLastN
  265. (lambda (l1 n)
  266. (cond
  267. ((and (eq? 1 n) (atom? (singleLayerTest l1))) l1)
  268. (else (getLastNInner l1 '() n)))))
  269.  
  270. ;getLastN helper
  271. (define getLastNInner
  272. (lambda (l1 l2 n)
  273. (cond
  274. ((null? l1) l2)
  275. ((and (zero? n) (null? (cdr l2))) (car l2))
  276. ((zero? n) l2)
  277. (else (getLastNInner (removeLast l1) (cons (getLast l1) l2) (- n 1))))))
  278.  
  279. ;getLastNInner helper
  280. (define getLast
  281. (lambda (l)
  282. (cond
  283. ((null? (cdr l)) (car l))
  284. (else (getLast (cdr l))))))
  285.  
  286. ;getLastNInner helper
  287. (define removeLast
  288. (lambda (l)
  289. (cond
  290. ((null? (cdr l)) '())
  291. (else (cons (car l) (removeLast (cdr l)))))))
  292.  
  293. ;;;;;; Environment
  294.  
  295. ;looks up the value of a variable handles multiple layers
  296. (define lookup
  297. (lambda (name state)
  298. (cond
  299. ((null? state) '())
  300. ((atom? (singleLayerTest state))(lookup-helper name state))
  301. ((and (list? (singleLayerTest state)) (not (null? (lookup-helper name (variableList state))))) (lookup-helper name (variableList state)))
  302. (else (lookup name (removeLayer state))))))
  303.  
  304. ;lookup a variable's value in the layer it is given
  305. (define lookup-helper
  306. (lambda (name state)
  307. (cond
  308. ((null? (variableList state)) '())
  309. ((eq? (firstVariable state) name) (unbox(firstValue state)))
  310. (else (lookup-helper name (cons (remainingVariables state) (cons(remainingValues state) '())))))))
  311.  
  312. ;updates the value of a variable in the state handles multiple layers
  313. (define Update
  314. (lambda (name value state)
  315. (cond
  316. ((null? state) '())
  317. ((and (atom? (singleLayerTest state)) (not (null? (lookup-helper name state)))) (Update-helper name value state))
  318. ((and (list? (singleLayerTest state)) (not (null? (lookup-helper name (variableList state))))) (cons (Update-helper name value (variableList state)) (cdr state)))
  319. (else (cons (variableList state) (Update name value (removeLayer state)))))))
  320.  
  321. ;updates the value of a variable in the layer it was given
  322. (define Update-helper
  323. (lambda (name value state)
  324. (cond
  325. ((null? (variableList state)) '())
  326. ((eq? (firstVariable state) name) (begin (set-box! (firstValue state) value) state))
  327. (else (cons
  328. (cons (firstVariable state) (variableList (Update-helper name value (cons (remainingVariables state) (cons(remainingValues state) '()))) ))
  329. (cons
  330. (cons (firstValue state) (valueList (Update-helper name value (cons (remainingVariables state) (cons(remainingValues state) '())))))
  331. '()))))))
  332.  
  333. ;add a variable to the state handles multiple layers
  334. (define Add
  335. (lambda (name value state)
  336. (cond
  337. ((or (null? (car state))(atom? (singleLayerTest state)))(Add-helper name value state))
  338. ((list? (variableList state))(cons (Add-helper name value (variableList state)) (remaining state)))
  339. (else (Add-helper name value state)))))
  340.  
  341. ;adds a variable to the layer it is given
  342. (define Add-helper
  343. (lambda (name value state)
  344. (cond
  345. ((null? (variableList state))
  346. (cons (append (variableList state) (cons name '()))
  347. (cons
  348. (append (valueList state) (cons (box value) '()))'())))
  349. (else (cons
  350. (cons (firstVariable state) (variableList (Add-helper name value (cons (remainingVariables state) (cons(remainingValues state) '()))) ))
  351. (cons
  352. (cons (firstValue state) (valueList (Add-helper name value (cons (remainingVariables state) (cons(remainingValues state) '())))))
  353. '()))))))
  354.  
  355. ;adds a layer to the current state
  356. (define addLayer
  357. (lambda (state)
  358. (cond
  359. ((not (list? (singleLayerTest state)))(cons (initialState)(cons state '())))
  360. (else (cons (initialState) state)))))
  361.  
  362. ;remove a layer from the current state
  363. (define removeLayer cdr)
  364.  
  365. ;get the top layer of the environment
  366. (define topLayer
  367. (lambda (state)
  368. (cond
  369. ((null? (variableList state)) state)
  370. ((atom? (singleLayerTest state)) state)
  371. (else (variableList state)))))
  372.  
  373. ;;;;;; Abstraction
  374.  
  375. (define front car)
  376. (define remaining cdr)
  377.  
  378. ;abstraction of operators
  379. (define operator car)
  380. (define leftoperand cadr)
  381. (define rightoperand caddr)
  382.  
  383. ;checks if the input is an atom
  384. (define atom? (lambda (x) (and (not (pair? x)) (not (null? x)))))
  385.  
  386. ;get all but the first variable in the state
  387. (define remainingVariables cdar)
  388. ;get all but the first associated variable in the state
  389. (define remainingValues cdadr)
  390. ;get the first variable in the state
  391. (define firstVariable caar)
  392. ;get the first associated value in the state
  393. (define firstValue caadr)
  394. ;get all the current variables in the state
  395. (define variableList car)
  396. ;get all the current values of the associated variables in the state
  397. (define valueList cadr)
  398.  
  399. ;abstractions for the function definition passed by the parser
  400. (define functionName cadr)
  401. (define functionBody cadddr)
  402. (define functionParamList caddr)
  403.  
  404. ;abstractions for the function closures
  405. (define functionClosureBody cadr)
  406. (define functionClosureParamList car)
  407. (define getFunctionClosureLayerNum caddr)
  408.  
  409. ;abstractions for the function calls
  410. (define functionCallName cadr)
  411. (define functionCallParamList cddr)
  412.  
  413. ;abstractions for if statements
  414. (define condition cadr)
  415. (define ifBody caddr)
  416. (define elseBody cdddr)
  417.  
  418. ;a way to tell if the state has more than one layer
  419. (define singleLayerTest caar)
  420.  
  421. ;class closure
  422. (define parent car)
  423. (define classFields cadr)
  424. (define classMethods caddr)
  425. (define classInitials cadddr)
  426.  
  427. ;class parser
  428. (define className cadr)
  429. (define classHeader caddr)
  430. (define classBody cadddr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement