Skip to content

Commit

Permalink
Simplify io specification.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotmarcot committed Sep 28, 2012
1 parent d13ef7b commit bb7a71b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 61 deletions.
28 changes: 8 additions & 20 deletions Scilab/Interpreter.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
module
Scilab.Interpreter
(interpret, Value (..), Valuable (..), vecL, scalarD)
where
module Scilab.Interpreter (interpret) where

-- base
import Control.Applicative ((<$>), (<*))
import Control.Monad (void)
import Control.Monad (void, (>=>))
import Control.Arrow (first, second)

-- deepseq
Expand All @@ -31,10 +28,10 @@ import Control.Monad.State.Class (gets, modify)
-- scilab
import Scilab.Parser

interpret :: [Value] -> T.Text -> ([Value], [Value])
interpret :: [Double] -> T.Text -> ([Double], [Double])
interpret input = run input . parser

run :: [Value] -> [Command] -> ([Value], [Value])
run :: [Double] -> [Command] -> ([Double], [Double])
run input cs = runWriter $ snd <$> execStateT (execs cs) (M.empty, input)

execs :: [Command] -> Scilab ()
Expand Down Expand Up @@ -98,7 +95,7 @@ eval (ENot e) = dof not e
eval (ENegate e) = dofD negate e
eval (ENumber n) = return $ scalar n
eval (EStr t) = return $ String $ V.singleton t
eval (ECall "input" _) = head <$> gets snd <* modify (second tail)
eval (ECall "input" _) = scalar <$> head <$> gets snd <* modify (second tail)
eval (ECall "disp" [e]) = disp e >> return undefined
eval (ECall "sqrt" [e]) = dofD sqrt e
eval (ECall "factorial" [e]) = dofD (product . enumFromTo 1) e
Expand Down Expand Up @@ -128,10 +125,7 @@ eval (EVecFromToStep from step to)
return $ vec $ V.fromList [nfrom, (nfrom + nstep) .. nto]

disp :: Expr -> Scilab ()
disp e
= do
e_ <- eval e
tell [e_]
disp = evalVec >=> tell . V.toList

evalVec :: Valuable a => Expr -> Scilab (V.Vector a)
evalVec e = getVec <$> eval e
Expand Down Expand Up @@ -166,7 +160,7 @@ dof f e = vec <$> V.map f <$> evalVec e
dofD :: (Double -> Double) -> Expr -> Scilab Value
dofD = dof

type Scilab = StateT (M.Map T.Text Value, [Value]) (Writer [Value])
type Scilab = StateT (M.Map T.Text Value, [Double]) (Writer [Double])

readVar :: T.Text -> Scilab Value
readVar var = (M.! var) <$> getVars
Expand All @@ -176,7 +170,7 @@ getVars = gets fst

data Value
= Number {valueBool :: Bool, valueVec :: V.Vector Double}
| String {valueStrVec :: V.Vector T.Text}
| String {_valueStrVec :: V.Vector T.Text}
deriving (Show, Eq)

instance NFData Value where
Expand Down Expand Up @@ -214,9 +208,3 @@ instance Valuable Bool where
isDouble _ = False

instance Valuable Int

vecL :: [Double] -> Value
vecL = vec . V.fromList

scalarD :: Double -> Value
scalarD = scalar
78 changes: 37 additions & 41 deletions tests/tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ parse
execution :: Test
execution
= TestList
[([], [scalarD 2])
~=? interpret [scalarD 2] "a = input(\"\")\ndisp(a)",
([], [vecL [1 .. 4]]) ~=? interpret [] "disp(1 : 4)",
([], [vecL [1 ,3 .. 7]]) ~=? interpret [] "disp(1 : 2 : 7)",
([], [vecL []]) ~=? interpret [] "disp(3 : 1)",
([], [vecL [3, 2, 1]]) ~=? interpret [] "disp(3 : -1 : 1)",
([], [scalarD 450])
[([], [2]) ~=? interpret [2] "a = input(\"\")\ndisp(a)",
([], [1 .. 4]) ~=? interpret [] "disp(1 : 4)",
([], [1 ,3 .. 7]) ~=? interpret [] "disp(1 : 2 : 7)",
([], []) ~=? interpret [] "disp(3 : 1)",
([], [3, 2, 1]) ~=? interpret [] "disp(3 : -1 : 1)",
([], [450])
~=? interpret
(map scalarD [11, 29, 46, 47, 57, 24, 50, 92, 10, 84])
[11, 29, 46, 47, 57, 24, 50, 92, 10, 84]
("total = 0\n"
<> "i = 1\n"
<> "while i <= 10 do\n"
Expand All @@ -67,9 +66,9 @@ execution
<> " i = i + 1\n"
<> "end\n"
<> "disp(total)"),
([], [vecL [5.5, 14.5, 23, 23.5, 28.5, 12, 25, 46, 5, 42]])
([], [5.5, 14.5, 23, 23.5, 28.5, 12, 25, 46, 5, 42])
~=? interpret
(map scalarD [11, 29, 46, 47, 57, 24, 50, 92, 10, 84])
[11, 29, 46, 47, 57, 24, 50, 92, 10, 84]
("v = []\n"
<> "i = 1\n"
<> "while i <= 10 do\n"
Expand All @@ -78,35 +77,33 @@ execution
<> " i = i + 1\n"
<> "end\n"
<> "disp(v / 2)"),
([], [vecL [5.5, 14.5, 23, 23.5, 28.5, 12, 25, 46, 5, 42]])
([], [5.5, 14.5, 23, 23.5, 28.5, 12, 25, 46, 5, 42])
~=? interpret
(map scalarD [11, 29, 46, 47, 57, 24, 50, 92, 10, 84])
[11, 29, 46, 47, 57, 24, 50, 92, 10, 84]
("v = []\n"
<> "for i = 1 : 10 do\n"
<> " n = input(\"\")\n"
<> " v = [v n]\n"
<> "end\n"
<> "disp(v / 2)"),
([],
[vecL
[3.3166247903554,
5.385164807134504,
6.782329983125268,
6.855654600401044,
7.54983443527075,
4.898979485566356,
7.0710678118654755,
9.591663046625438,
3.1622776601683795,
9.16515138991168,
-1,
-1,
-1,
-1,
-1]])
[3.3166247903554,
5.385164807134504,
6.782329983125268,
6.855654600401044,
7.54983443527075,
4.898979485566356,
7.0710678118654755,
9.591663046625438,
3.1622776601683795,
9.16515138991168,
-1,
-1,
-1,
-1,
-1])
~=? interpret
(map scalarD
[11, 29, 46, 47, 57, 24, 50, 92, 10, 84, -18, -29, -25, -40, -35])
[11, 29, 46, 47, 57, 24, 50, 92, 10, 84, -18, -29, -25, -40, -35]
("v = []\n"
<> "for i = 1 : 15 do\n"
<> " n = input(\"\")\n"
Expand All @@ -118,21 +115,20 @@ execution
<> " v = [v n]\n"
<> "end\n"
<> "disp(v)"),
([], [scalarD (-1)]) ~=? interpret [] "a = 1; disp(-a)",
([], [vecL [-1, -1, -1]]) ~=? interpret [] "disp(-1^[1 2 3])",
([], [vecL [-1, 1, -1]]) ~=? interpret [] "disp((-1)^[1 2 3])",
([], [scalarD 2])
~=? interpret [] "a = [1]\na(1) = 2\ndisp(a(1))",
([], [scalarD 2]) ~=? interpret [] "a = 1\na(1) = 2\ndisp(a(1))",
([], map scalarD [1, 2])
([], [-1]) ~=? interpret [] "a = 1; disp(-a)",
([], [-1, -1, -1]) ~=? interpret [] "disp(-1^[1 2 3])",
([], [-1, 1, -1]) ~=? interpret [] "disp((-1)^[1 2 3])",
([], [2]) ~=? interpret [] "a = [1]\na(1) = 2\ndisp(a(1))",
([], [2]) ~=? interpret [] "a = 1\na(1) = 2\ndisp(a(1))",
([], [1, 2])
~=? interpret
[]
"i = 1\nwhile [i <= 2]\n disp(i)\n i = i + 1\nend",
([], [scalarD 1])
([], [1])
~=? interpret [] "for x = 1 do\n disp(x)\nend",
([], [scalarD 1])
~=? interpret [scalarD 1] "a = input(\"\")\r\ndisp(a)",
([], map scalarD [1, 2]) ~=? interpret [] "printf(1, 2)"]
([], [1])
~=? interpret [1] "a = input(\"\")\r\ndisp(a)",
([], [1, 2]) ~=? interpret [] "printf(1, 2)"]

loop :: Test
loop
Expand Down

0 comments on commit bb7a71b

Please sign in to comment.