Skip to content

Commit

Permalink
feat: better if interpreter + new filetest
Browse files Browse the repository at this point in the history
  • Loading branch information
Raskc committed Jan 12, 2024
1 parent e144b6e commit a159e28
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 39 deletions.
4 changes: 1 addition & 3 deletions Testfile/test_call/call1.ccs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
test() => (
let x = 5;
test();
print("haha");
return(x);
);

test();
print(test());
1 change: 1 addition & 0 deletions Testfile/test_call/call2.ccs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ let c = 3;

test(b, b, c) => (
let d = (a + b + c);
return (d);
);

test(a b c);
7 changes: 4 additions & 3 deletions Testfile/test_if/if1.ccs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let a = 1;
let a = 2;
let b = 1;

if (a == b) then:
(
let x = 10;
)
print("lol");
)
print("ahahaha");
15 changes: 10 additions & 5 deletions Testfile/test_if/if2.ccs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
let a = 5;
let b = 5;
let b = 4;
let c = 0;

if (a <= b) then: (
let x = 10;
if (a == b) then: (
c = 1;
print("aaaha");
) else: (
let x = 11;
)
print("lol");
print("mdr");
)

print(c);
21 changes: 11 additions & 10 deletions Testfile/test_if/if3.ccs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
let a = 5;
let b = 4;
let b = 5;

if (a == b) then: (
let x = 10;
if (a > x) then:
show("a is greater");
let x = 5;
if (a == x) then:
print("a is greater");
else:
show ("a is smaller");
print("a is smaller");
) else: (
let x = 11;
if (a < x) then:
show("x is greater");
let x = 5;
if (a == x) then:
print("x is greater");
else:
show ("x is smaller");
)
print("x is smaller");
)
print("outside");
6 changes: 6 additions & 0 deletions Testfile/test_if/if4.ccs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let a = 1;
let b = 2;

(a == b) ? let x = 10; : let x = 11;

print(x);
4 changes: 0 additions & 4 deletions Testfile/test_if/if4_ss.ccs

This file was deleted.

7 changes: 7 additions & 0 deletions Testfile/test_if/if5.ccs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

if (7 > 6) then: (
print(True);
) else: (
print(False);
)
print("outside");
File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions Testfile/test_named_call/named_call1.ccs

This file was deleted.

6 changes: 0 additions & 6 deletions Testfile/test_named_call/named_call2.ccs

This file was deleted.

3 changes: 0 additions & 3 deletions Testfile/test_named_call/named_call3.ccs

This file was deleted.

35 changes: 35 additions & 0 deletions src/glados-main/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,43 @@ processAst (Define (AstSymbol sym) value) (env, insts) =
processAst (Assign (AstSymbol sym) value) (env, insts) =
let valueInsts = interpretMathOpOrValue value
in (env, insts ++ valueInsts ++ [AssignEnvValue sym])
processAst (AstCall (AstSymbol "print") (AstList args)) (env, insts) =
let argsInsts = concatMap interpretMathOpOrValue args
in (env, insts ++ argsInsts ++ [PushToOutput])
processAst (If cond thenBranch elseBranch) (env, insts) =
let condInsts = interpretCondition cond
processBranch (AstList stmts) = concatMap (snd . (`processAst` (env, []))) stmts
processBranch stmt = snd $ processAst stmt (env, [])
thenInsts = processBranch thenBranch
elseInsts = processBranch elseBranch
jumpOverThen = [JumpIfFalse (length thenInsts + if null elseInsts then 0 else 1)]
skipElseInst = if null elseInsts then [] else [Jump (length elseInsts)]
in (env, insts ++ condInsts ++ jumpOverThen ++ thenInsts ++ skipElseInst ++ elseInsts)
processAst _ (env, insts) = (env, insts)

interpretCondition :: Ast -> Insts
interpretCondition (AstList [left, LogicOperator op, right]) =
case op of
"<=" -> interpretCompositeOp left right ["<", "=="]
">=" -> interpretCompositeOp left right [">", "=="]
_ -> interpretMathOpOrValue left ++ interpretMathOpOrValue right ++ [Push (Operator (logicStringToOperator op)), CallOp]
interpretCondition _ = error "Invalid condition"

interpretCompositeOp :: Ast -> Ast -> [String] -> Insts
interpretCompositeOp left right ops =
let leftInsts = interpretMathOpOrValue left
rightInsts = interpretMathOpOrValue right
opInsts = concatMap (\op -> leftInsts ++ rightInsts ++ [Push (Operator (logicStringToOperator op)), CallOp]) ops
in opInsts ++ [Push (Operator Or), CallOp]

logicStringToOperator :: String -> Operator
logicStringToOperator "==" = Eq
logicStringToOperator "<" = Less
logicStringToOperator ">" = Sup
logicStringToOperator "&&" = And
logicStringToOperator "||" = Or
logicStringToOperator _ = error "Unknown logic operator"

interpretMathOpOrValue :: Ast -> Insts
interpretMathOpOrValue (AstMathOp left op right) = interpretMathOp left op right
interpretMathOpOrValue (AstSymbol sym) = [PushVMEnv sym]
Expand Down

0 comments on commit a159e28

Please sign in to comment.