Skip to content

Commit

Permalink
multiple issues with list append still unresolved
Browse files Browse the repository at this point in the history
  • Loading branch information
zazedd committed Aug 12, 2023
1 parent 0efefe6 commit a4e5189
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dune runtest
- [x] ++ (string concat)
- [ ] I don't know yet
- [x] 'a list
- [x] - (append)
- [x] @ (append)
- [x] :: (cons)
- [ ] Garbage Collection

Expand Down
1 change: 1 addition & 0 deletions src/ast/parsed.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type op =
| Mult
| Div
| Mod
| LstAppend
| Cons
| Concat
| Eq
Expand Down
2 changes: 1 addition & 1 deletion src/evaluating/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ and eval_bop ctx op e1 e2 =
| Div, VInt a, VInt b -> (VInt (a / b), ctx)
| Mod, VInt a, VInt b -> (VInt (a mod b), ctx)
| Concat, VString a, VString b -> (VString (a ^ b), ctx)
| Add, VList a, VList b -> (VList (List.append a b), ctx)
| LstAppend, VList a, VList b -> (VList (List.append a b), ctx)
| Cons, a, VList b -> (VList (a :: b), ctx)
| Eq, a, b -> (VBool (a = b), ctx)
| Ineq, a, b -> (VBool (a <> b), ctx)
Expand Down
1 change: 1 addition & 0 deletions src/parsing/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rule read =
| "*" { MULT }
| "/" { DIV }
| "%" { MODULUS }
| "@" { AT }
| "::" { CONS }
| "=" { EQUALS }
| "==" { EQ }
Expand Down
2 changes: 2 additions & 0 deletions src/parsing/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
%token MULT
%token DIV
%token MODULUS
%token AT
%token CONS
%token EQ
%token INEQ
Expand Down Expand Up @@ -124,6 +125,7 @@ bop:
| MULT { Mult }
| DIV { Div }
| MODULUS { Mod }
| AT { LstAppend }
| CONS { Cons }
| EQ { Eq }
| INEQ { Ineq }
Expand Down
7 changes: 5 additions & 2 deletions src/typing/typecheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ and if_branch ctx e2 e3 pos =

and typeof_bop ctx op e1 e2 pos =
match (op, typeof ctx e1 |> fst |> head, typeof ctx e2 |> fst |> head) with
| Add, (TList _ as a), (TList _ as b) -> unify_bop ctx a b (TList a) pos
| LstAppend, (TList _ as t1), (TList _ as t2) -> unify_bop ctx t1 t2 t1 pos
| LstAppend, t1, (TList _ as t2) -> unify_bop ctx t1 t2 t2 pos
| LstAppend, (TList _ as t1), t2 -> unify_bop ctx t1 t2 t1 pos
| LstAppend, t1, t2 -> unify_bop ctx t1 t2 (TList (newvar ())) pos
| Cons, t1, (TList t2 as b) -> unify_bop ctx t1 t2 b pos
| (Add as op), t1, t2
| (Subt as op), t1, t2
Expand All @@ -112,7 +115,7 @@ and typeof_bop ctx op e1 e2 pos =
and typeof_bop_int ctx op t1 t2 pos =
match op with
| Add | Subt | Mult | Div | Mod -> unify_bop ctx t1 t2 TInt pos
| Concat | Cons | Eq | Ineq | Lt | Lte | Bt | Bte -> assert false
| Concat | LstAppend | Cons | Eq | Ineq | Lt | Lte | Bt | Bte -> assert false

and typeof_bop_equalities ctx t1 t2 pos =
match (t1 |> head, t2 |> head) with
Expand Down
6 changes: 3 additions & 3 deletions test/eval_test/eval_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ let%test "list tests" =

let%test "list append" =
match
( test "[1; 2] + [3; 4]" ECtx.empty |> string_of_val,
test "[\"str\"; \"str2\"] + [\"str3\"; \"str4\"]" ECtx.empty
( test "[1; 2] @ [3; 4]" ECtx.empty |> string_of_val,
test "[\"str\"; \"str2\"] @ [\"str3\"; \"str4\"]" ECtx.empty
|> string_of_val )
with
| "[1; 2; 3; 4]", "[\"str\"; \"str2\"; \"str3\"; \"str4\"]" -> true
| _ -> false

let%test "list append" =
let%test "list cons" =
match
( test "1 :: [2; 3]" ECtx.empty |> string_of_val,
test "\"str\" :: [\"str2\"; \"str3\"]" ECtx.empty |> string_of_val )
Expand Down

0 comments on commit a4e5189

Please sign in to comment.