Skip to content

Commit a3eeeff

Browse files
author
Vitor Greati
authored
Merge pull request #49 from greati/parse#44
Fetch variables from memory
2 parents 21bf676 + 1cce9d8 commit a3eeeff

File tree

6 files changed

+58
-48
lines changed

6 files changed

+58
-48
lines changed

examples/declaration.gph

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
c : int;
2-
c : float;
31
a, b : int = 2 + 2, 2^3;
2+
3+
c : int = b + 1;
4+
5+
6+
print a;

interpreter/src/Execution/Memory.hs

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Syntactic.Syntax
1010
-- Variable attributes
1111
type Name = String
1212
type Scope = String
13+
type Scopes = [Scope]
1314
type Values = [Value]
1415

1516
-- Memory structure
@@ -26,8 +27,14 @@ elabVar s n c m
2627
| otherwise = Right (M.insert ci c m)
2728
where ci = (n,s)
2829

29-
fetchVarValue :: Memory -> Name -> Scope -> Either String Value
30-
fetchVarValue m n s
30+
fetchVarValue :: Memory -> Name -> Scopes -> Either String Value
31+
fetchVarValue m n [] = Left ("Variable " ++ n ++ " not found in any scope.")
32+
fetchVarValue m n (s:ss) = case getVarScopeValue m n s of
33+
Left i -> fetchVarValue m n ss
34+
Right i -> Right i
35+
36+
getVarScopeValue :: Memory -> Name -> Scope -> Either String Value
37+
getVarScopeValue m n s
3138
| M.notMember (n,s) m = Left ("Variable " ++ n ++ " in scope " ++ s ++ " not declared.")
3239
| otherwise = Right (head v)
3340
where (_,v) = (m M.!(n,s))

interpreter/src/Execution/Semantic.hs

+28-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import qualified Data.Map as M
88

99

1010
type Filename = String
11-
type Scopes = [Scope]
1211

1312
-- |Scopes in the execution.
1413
scopes :: Scopes
@@ -50,6 +49,10 @@ execStmt :: Stmt -> Memory -> Scopes -> IO (Memory, Scopes)
5049
execStmt d@(DeclStmt _) m ss = do
5150
m' <- varDeclStmt d m ss
5251
return (m', ss)
52+
execStmt (PrintStmt e) m ss = do
53+
putStrLn (show (eval m ss e))
54+
return (m, ss)
55+
5356

5457
-- |Executes a declaration statement.
5558
varDeclStmt :: Stmt -> Memory -> Scopes -> IO Memory
@@ -63,12 +66,12 @@ varDeclStmt (DeclStmt (VarDeclaration [] t (_:es))) m ss = do
6366
error "Too many expressions in right side."
6467
varDeclStmt (DeclStmt (VarDeclaration (x:xs'@(y:xs)) t (e:[]))) m ss = do
6568
do
66-
case elabVar (head ss) ((\(Ident x) -> x) x) (t, ([eval e])) m of
69+
case elabVar (head ss) ((\(Ident x) -> x) x) (t, ([eval m ss e])) m of
6770
(Left i) -> error i
6871
(Right i) -> varDeclStmt (DeclStmt (VarDeclaration xs' t (e:[]))) i ss
6972
varDeclStmt (DeclStmt (VarDeclaration (x:xs) t (e:es))) m ss = do
7073
do
71-
case elabVar (head ss) ((\(Ident x) -> x) x) (t, ([eval e])) m of
74+
case elabVar (head ss) ((\(Ident x) -> x) x) (t, ([eval m ss e])) m of
7275
(Left i) -> error i
7376
(Right i) -> varDeclStmt (DeclStmt (VarDeclaration xs t es)) i ss
7477

@@ -80,11 +83,11 @@ getType (Char c) = GChar
8083
getType (Bool b) = GBool
8184
getType (List (x:_)) = GList (getType x)
8285

83-
evalList :: [ArithExpr] -> [Value]
84-
evalList [x] = [eval x]
85-
evalList (x:y:xs) = if getType z /= getType (eval y) then error "Type mismatch in List "
86-
else z:(evalList (y:xs))
87-
where z = (eval x)
86+
evalList :: Memory -> Scopes -> [ArithExpr] -> [Value]
87+
evalList m ss [x] = [eval m ss x]
88+
evalList m ss (x:y:xs) = if getType z /= getType (eval m ss y) then error "Type mismatch in List "
89+
else z:(evalList m ss (y:xs))
90+
where z = (eval m ss x)
8891

8992

9093
-- | Default values for each type
@@ -103,24 +106,27 @@ fromValue :: Value -> Integer
103106
fromValue (Integer i) = i
104107

105108

106-
eval :: ArithExpr -> Value
107-
eval (ArithTerm (LitTerm (Lit v))) = v
108-
eval (ArithUnExpr MinusUnOp e) = minusUn (eval e)
109-
eval (ArithUnExpr PlusUnOp e) = plusUn (eval e)
110-
eval (ArithUnExpr NotUnOp e) = not' (eval e)
111-
eval (ArithBinExpr MinusBinOp e1 e2) = minusBin (eval e1) (eval e2)
112-
eval (ArithBinExpr PlusBinOp e1 e2) = plusBin (eval e1) (eval e2)
113-
eval (ArithBinExpr TimesBinOp e1 e2) = timesBin (eval e1) (eval e2)
114-
eval (ArithBinExpr DivBinOp e1 e2) = divBin (eval e1) (eval e2)
115-
eval (ArithBinExpr ExpBinOp e1 e2) = expBin (eval e1) (eval e2)
116-
eval (ExprLiteral (ListLit es )) = List (evalList es)
117-
eval (ArithBinExpr PlusPlusBinOp e1 e2) = case eval e1 of
118-
l1@(List (x:xs)) -> case eval e2 of
109+
eval :: Memory -> Scopes -> ArithExpr -> Value
110+
eval m ss (ArithTerm (LitTerm (Lit v))) = v
111+
eval m ss (ArithUnExpr MinusUnOp e) = minusUn (eval m ss e)
112+
eval m ss (ArithUnExpr PlusUnOp e) = plusUn (eval m ss e)
113+
eval m ss (ArithUnExpr NotUnOp e) = not' (eval m ss e)
114+
eval m ss (ArithBinExpr MinusBinOp e1 e2) = minusBin (eval m ss e1) (eval m ss e2)
115+
eval m ss (ArithBinExpr PlusBinOp e1 e2) = plusBin (eval m ss e1) (eval m ss e2)
116+
eval m ss (ArithBinExpr TimesBinOp e1 e2) = timesBin (eval m ss e1) (eval m ss e2)
117+
eval m ss (ArithBinExpr DivBinOp e1 e2) = divBin (eval m ss e1) (eval m ss e2)
118+
eval m ss (ArithBinExpr ExpBinOp e1 e2) = expBin (eval m ss e1) (eval m ss e2)
119+
eval m ss (ExprLiteral (ListLit es )) = List (evalList m ss es)
120+
eval m ss (ArithBinExpr PlusPlusBinOp e1 e2) = case eval m ss e1 of
121+
l1@(List (x:xs)) -> case eval m ss e2 of
119122
l2@(List (y:ys)) -> if (getType x == getType y) then plusPlusBinList l1 l2
120123
else plusPlusBin l1 l2
121-
k -> case eval e2 of
124+
k -> case eval m ss e2 of
122125
l2@(List (y:ys)) -> if (getType k == getType y) then plusPlusBin k l2
123126
else error "Type mismatch ++ operator "
127+
eval m ss (ArithTerm (IdTerm (Ident i))) = case fetchVarValue m i ss of
128+
Left i -> error i
129+
Right i -> i
124130

125131

126132
plusPlusBinList :: Value -> Value -> Value

interpreter/src/Syntactic/Parser.hs

+2-8
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,8 @@ readStmt = do
158158
printStmt :: GenParser GphTokenPos st Stmt
159159
printStmt = do
160160
(tok GTokPrint)
161-
do
162-
do
163-
i <- anyIdent
164-
return (PrintStmt (IdTerm i))
165-
<|>
166-
do
167-
i <- stringLit
168-
return (PrintStmt (LitTerm i))
161+
e <- expression
162+
return (PrintStmt e)
169163

170164
startIdent :: GenParser GphTokenPos st ArithExpr
171165
startIdent = do

interpreter/src/Syntactic/Syntax.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ data EdgeType = LeftEdge | RightEdge | DoubleEdge deriving (Show, Eq)
6464
data Edge = Edge EdgeType ArithExpr ArithExpr deriving (Show, Eq)
6565

6666
data Stmt = ReadStmt Identifier |
67-
PrintStmt Term |
67+
PrintStmt ArithExpr |
6868
DeclStmt VarDeclaration | --[Identifier] GType [ArithExpr] |
6969
AttrStmt [ArithExpr] [ArithExpr] |
7070
SubCallStmt SubprogCall |

interpreter/src/Syntactic/Values.hs

+13-13
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ data Value = Integer Integer |
1515
Quadruple (Value, Value, Value, Value) |
1616
Map (M.Map Value Value) |
1717
Graph (G.Graph Value Value)
18-
deriving (Eq, Show)
18+
deriving (Eq)
1919

20-
--instance Show Value where
21-
-- show (Integer x) = show x
22-
-- show (Float x) = show x
23-
-- show (Char x) = show x
24-
-- show (String x) = show x
25-
-- show (Bool x) = show x
26-
-- show (List x) = show x
27-
-- show (Pair x) = show x
28-
-- show (Triple x) = show x
29-
-- show (Quadruple x) = show x
30-
-- show (Map x) = show x
31-
-- show (Graph x) = show x
20+
instance Show Value where
21+
show (Integer x) = show x
22+
show (Float x) = show x
23+
show (Char x) = show x
24+
show (String x) = show x
25+
show (Bool x) = show x
26+
show (List x) = show x
27+
show (Pair x) = show x
28+
show (Triple x) = show x
29+
show (Quadruple x) = show x
30+
show (Map x) = show x
31+
show (Graph x) = show x
3232

0 commit comments

Comments
 (0)