Skip to content

Instantly share code, notes, and snippets.

@JakubOboza
Created May 2, 2012 21:32
Show Gist options
  • Save JakubOboza/2580670 to your computer and use it in GitHub Desktop.
Save JakubOboza/2580670 to your computer and use it in GitHub Desktop.
data Op = Integer Int | Function String deriving (Show)
newtype Program = List Op
data Ast = Const Int | Binary Ast String Ast
--i robisz funkcje
--Ast -> Program
--koniec
value (Integer val) = val
sample_input = [Integer 1, Integer 5, Function "+", Integer 6, Function "*", Integer 67, Integer 13, Integer 49, Function "+", Function "-", Function "+" ]
-- just put shit on stacks
eval0 [] num_stack foo_stack = (num_stack, foo_stack)
eval0 (i:is) num_stack foo_stack =
case i of
Integer num -> eval0 is (num : num_stack) foo_stack
Function foo -> eval0 is num_stack (foo : foo_stack)
-- perform some calculations
eval1 [] num_stack = num_stack
eval1 (i:is) num_stack =
case i of
Integer num -> eval1 is (num : num_stack)
Function foo -> eval1 is (eval_foo foo num_stack)
eval_foo foo num_stack =
case foo of
"+" -> eval_plus num_stack
"-" -> eval_minus num_stack
"*" -> eval_mul num_stack
-- switch to doubles "/" -> eval_div num_stack
eval_plus num_stack =
let left = head num_stack in
let right = head $ tail num_stack in
(left + right) : (tail $ tail num_stack)
eval_minus num_stack =
let left = head num_stack in
let right = head $ tail num_stack in
(left - right) : (tail $ tail num_stack)
eval_mul num_stack =
let left = head num_stack in
let right = head $ tail num_stack in
(left * right) : (tail $ tail num_stack)
-- transform to normal 2 + 4
--eval2 [] num_stack normal_stack = normal_stack
--eval2 (i:is) num_stack foo_stack =
-- case i of
-- Integer num -> eval2 is (num : num_stack) normal_stack
-- Function foo -> eval2 is num_stack (foo : foo_stack) normal_stack
-- type system super fucker
--eval_div num_stack =
-- let left = head num_stack in
-- let right = head $ tail num_stack in
-- (left / right) : (tail $ tail num_stack)
result0 = eval0 sample_input [] []
result1 = eval1 sample_input []
--result2 = eval2 sample_input [] [] []
main = do
putStrLn $ show result0
putStrLn $ show result1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment