git @ Cat's Eye Technologies Carriage / f24941a
Add tests to README. Alter refimpl to output in expected format. Chris Pressey 2 years ago
4 changed file(s) with 30 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
8989
9090 ### Examples
9191
92 -> Tests for functionality "Evaluate Carriage Program"
93
94 -> Functionality "Evaluate Carriage Program" is implemented by
95 -> shell command "bin/carriage run %(test-body-file)"
96
9297 #### Basic Stack Manipulation
9398
9499 As a simple example, the Carriage program
95100
96 111-~+
101 111-~+
97102
98103 will be turned into a function which we might spell out in, say, Erlang, as
99104
100 fun(S) -> add(pick(sub(one(one(one(S))))))
105 fun(S) -> add(pick(sub(one(one(one(S))))))
101106
102107 which will be applied to a stack
103108
104 (fun(S) -> add(pick(sub(one(one(one(S)))))))(["1","1","1","-","~","+"])
109 (fun(S) -> add(pick(sub(one(one(one(S)))))))(["1","1","1","-","~","+"])
105110
106111 which could be stated more succinctly as
107112
108 add(pick(sub(one(one(one(["1","1","1","-","~","+"]))))))
113 add(pick(sub(one(one(one(["1","1","1","-","~","+"]))))))
109114
110115 and whose evaluation could be depicted as
111116
112 add(pick(sub(one(one(["1","1","1","-","~","+",1])))))
113 add(pick(sub(one(["1","1","1","-","~","+",1,1]))))
114 add(pick(sub(["1","1","1","-","~","+",1,1,1])))
115 add(pick(["1","1","1","-","~","+",1,0]))
116 add(["1","1","1","-","~","+",1,1])
117 add(pick(sub(one(one(["1","1","1","-","~","+",1])))))
118 add(pick(sub(one(["1","1","1","-","~","+",1,1]))))
119 add(pick(sub(["1","1","1","-","~","+",1,1,1])))
120 add(pick(["1","1","1","-","~","+",1,0]))
121 add(["1","1","1","-","~","+",1,1])
117122
118123 finally evaluating to the result stack
119124
120 ["1","1","1","-","~","+",2]
125 ["1","1","1","-","~","+",2]
121126
122127 (Note that stacks are being depicted bottom-to-top. I realize that's not how you'd typically
123128 implement them as lists in a functional language. Please just ignore that detail.)
129
130 And here is our Falderal test for confirming that implementations
131 get this result from evaluating this program:
132
133 111-~+
134 ===> ["1","1","1","-","~","+",2]
124135
125136 #### Function Creation and Application
126137
151162 Finally, the apply instruction pops the function, and applies it to the
152163 stack: the 2 is popped, 1 is added to it, and the result, 3, is pushed
153164 back on.
165
166 And here is our Falderal test for confirming that implementations
167 get this result from evaluating this program:
168
169 11+$11+111+@!
170 ===> ["1","1","+","$","1","1","+","1","1","1","+","@","!",3]
154171
155172 ##### Note on "slice"
156173
1010 driver [progElem, resultElem, runButtonElem] =
1111 onEvent runButtonElem Click $ \_ -> do
1212 Just prog <- getValue progElem
13 setProp resultElem "textContent" $ show $ Evaluator.run prog
13 setProp resultElem "textContent" $ show $ reverse $ Evaluator.run prog
77 instance Show Elem where
88 show (Int i) = show i
99 show (Fn _) = "<fn>"
10 show (Sym c) = show c
10 show (Sym c) = show [c]
1111
1212 pop (e:s) = (e, s)
1313 push s e = (e:s)
1111 case args of
1212 ["run", fileName] -> do
1313 text <- readFile fileName
14 putStrLn $ show $ Evaluator.run text
14 putStrLn $ show $ reverse $ Evaluator.run text
1515 return ()
1616 _ -> do
1717 abortWith "Usage: carriage run <carriage-program-text-filename>"