Skip to content

Commit

Permalink
♻️ 💚 fix compileApplication and pass tests 09 & 10
Browse files Browse the repository at this point in the history
  • Loading branch information
laendoor committed Dec 15, 2021
1 parent 36bd03b commit 8cc1cfe
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
3 changes: 3 additions & 0 deletions debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ if [[ $TEST_FOONES == "--foones" ]]; then
rm -f "$TEST_NAME.foones.mam"
fi

# move as a test
# cp "$TEST_NAME.output" "$TEST_NAME.expected"

# clean
rm -f "$TEST_NAME.ast"
rm -f "$TEST_NAME.mam"
Expand Down
38 changes: 13 additions & 25 deletions src/Mamarracho.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ compileExpr e _ = error $ "Expression NOT implemented: " ++ show e
compileVariable :: ID -> Reg -> Mam ()
compileVariable _id reg = do
case varType _id of
TVar -> compileVarValue _id reg
TOper -> compileOperation _id reg
TPrinter -> compilePrimitivePrint _id reg
TOper -> compilePrimitiveOperation _id reg
TVar -> compileVarValue _id reg

compilePrimitivePrint :: String -> Reg -> Mam ()
compilePrimitivePrint _id reg = do
Expand Down Expand Up @@ -108,31 +108,19 @@ compileArithmeticOperation mamOp e1 e2 reg = do
Store (reg, 1, r1)
]

compilePrimitiveOperation :: ID -> Reg -> Mam ()
compilePrimitiveOperation _id _ = error $ "compilePrimitiveOperation " ++ _id ++ " not implemented"
compileOperation :: ID -> Reg -> Mam ()
compileOperation x _ = error $ "compileOperation " ++ x ++ " not implemented"

compileApplication :: Expr -> Expr -> Reg -> Mam ()
compileApplication (ExprVar _id) e2 reg = do
case varType _id of
TPrinter -> do
compileExpr e2 reg
compilePrimitivePrint _id reg
TOper -> do
compileExpr e2 reg
compilePrimitiveOperation _id reg
TVar -> do
r1 <- lookupEnvRegister _id
r2 <- localReg
r3 <- localReg
compileExpr e2 r2
addCode [
MovReg (Global "fun", r1),
MovReg (Global "arg", r2),
Load (r3, Global "fun", 1),
ICall r3,
MovReg (reg, Global "res")
]
compileApplication e1 e2 reg = do
compileApplication e1@(ExprVar x) e2 reg = do
case varType x of
TVar -> compileApplicationFunction e1 e2 reg
TOper -> compileExpr e2 reg >> compileOperation x reg
TPrinter -> compileExpr e2 reg >> compilePrimitivePrint x reg
compileApplication e1 e2 reg = compileApplicationFunction e1 e2 reg

compileApplicationFunction :: Expr -> Expr -> Reg -> Mam ()
compileApplicationFunction e1 e2 reg = do
r1 <- localReg
r2 <- localReg
r3 <- localReg
Expand Down
6 changes: 4 additions & 2 deletions test/mam.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ function mam_test() {
}

@test "mam::test09" {
skip
mam_test "test/mamarracho/test09"
}

Expand All @@ -72,8 +71,11 @@ function mam_test() {
mam_test "test/mamarracho/test09.2"
}

@test "mam::test09.3" {
mam_test "test/mamarracho/test09.3"
}

@test "mam::test10" {
skip
mam_test "test/mamarracho/test10"
}

Expand Down
1 change: 1 addition & 0 deletions test/mamarracho/test09.3.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
9 changes: 9 additions & 0 deletions test/mamarracho/test09.3.flecha
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def test x =
unsafePrintInt x;
unsafePrintChar '\n'

def true x y = x
def cond x y z = x y z

def main = test (cond true 1 2)

0 comments on commit 8cc1cfe

Please sign in to comment.