Skip to content

Commit

Permalink
refactor(Vm): PushArg
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwozy committed Jan 13, 2024
1 parent 57a52ed commit bfaf7d5
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions LobsterLang/src/Vm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ instance Eq Operator where
data Instruction = Push Value
| PushArg Int
| PushEnv String
| PutArg Value
| PutArg
| Call
| JumpIfFalse Int
| JumpIfTrue Int
Expand All @@ -161,7 +161,7 @@ instance Show Instruction where
show (Push val) = "Push " ++ show val
show (PushArg x) = "PushArg " ++ show x
show (PushEnv x) = "PushEnv " ++ show x
show (PutArg v) = "PutArg " ++ show v
show (PutArg) = "PutArg"
show Call = "Call"
show (JumpIfFalse x) = "JumpIfFalse " ++ show x
show (JumpIfTrue x) = "JumpIfTrue " ++ show x
Expand Down Expand Up @@ -357,7 +357,7 @@ exec env arg (Call : xs) stack = case Stack.pop stack of
(Just v, stack3) -> exec env arg (Call:xs)
(Stack.push
(Stack.push stack3 (IntVal (nb' - 1)))
(Function (PutArg v:body) (nb - 1)))
(Function (Push v:PutArg:body) (nb - 1)))
(Nothing, _) -> Left "Error: stack is empty"
(_, _) -> Left "Error: stack is invalid for a function call"
(Just a, _) -> Left ("Error: not an Operation or a function " ++ show a)
Expand All @@ -381,7 +381,9 @@ exec env arg (PushEnv x:xs) stack = case isInEnv x env of
Just (Function func nb) -> exec env arg (Push (Function func nb):xs) stack
Just (ListVal list) -> exec env arg (Push (ListVal list):xs) stack
exec env arg (Push val:xs) stack = exec env arg xs (Stack.push stack val)
exec env arg (PutArg val:xs) stack = exec env (arg ++ [val]) xs stack
exec env arg (PutArg:xs) stack = case Stack.pop stack of
(Nothing, _) -> Left "Error: stack is empty"
(Just val, stack1) -> exec env (arg ++ [val]) xs stack1
exec env arg (JumpIfFalse val:xs) stack
| Prelude.null xs = Left "Error: no jump possible"
| Prelude.null stack = Left "Error: stack is empty"
Expand Down

0 comments on commit bfaf7d5

Please sign in to comment.