Skip to content

Commit

Permalink
string concat
Browse files Browse the repository at this point in the history
  • Loading branch information
zazedd committed Aug 9, 2023
1 parent 73613c2 commit e80ab4b
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ dune runtest
- [ ] > , =>
- [ ] <, =<
- [ ] String ops
- [ ] ^ (string concat)
- [x] ++ (string concat)
- [ ] ==
- [ ] !=
- [ ] > , =>
Expand Down
2 changes: 1 addition & 1 deletion src/ast/parsed.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Common

type op = Add | Subt | Mult | Div | Mod | Eq
type op = Add | Subt | Mult | Div | Mod | Concat | Eq
type vals = Unit | Int of int | Char of char | String of string | Bool of bool

type t =
Expand Down
1 change: 1 addition & 0 deletions src/evaluating/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ and eval_bop ctx op e1 e2 =
| Mult, VInt a, VInt b -> (VInt (a * b), ctx)
| 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)
| Eq, VInt a, VInt b -> (VBool (a = b), ctx)
| _ -> op_error ()

Expand Down
4 changes: 3 additions & 1 deletion src/parsing/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ let newl = '\n'
let digit = ['0'-'9']
let int = '-'? digit+
let letter = ['_' 'a'-'z' 'A'-'Z'] ['a'-'z' 'A'-'Z' '_' '0'-'9']*
let letter_space = ['_' 'a'-'z' ' ' 'A'-'Z'] ['a'-'z' 'A'-'Z' ' ' '_' '0'-'9']*
let char = '\'' ['a'-'z'] '\''
let str = '\"' letter+ '\"'
let str = '\"' letter_space+ '\"'
let ident = letter+

rule read =
parse
| white { read lexbuf }
| newl { new_line read lexbuf }
| ";" { SEMICOLON }
| "++" { PLUSPLUS }
| "+" { PLUS }
| "-" { MINUS }
| "*" { MULT }
Expand Down
2 changes: 2 additions & 0 deletions src/parsing/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
%token DOTDOT
%token BAR

%token PLUSPLUS
%token PLUS
%token MINUS
%token MULT
Expand Down Expand Up @@ -96,6 +97,7 @@ expr:

bop:
| PLUS { Add }
| PLUSPLUS { Concat }
| MINUS { Subt }
| MULT { Mult }
| DIV { Div }
Expand Down
3 changes: 2 additions & 1 deletion src/typing/typecheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ and typeof_bop ctx op e1 e2 pos =
| (Div as op), t1, t2
| (Mod as op), t1, t2 ->
typeof_bop_int ctx op t1 t2 pos
| Concat, t1, t2 -> unify_bop ctx t1 t2 TString TString pos
| Eq, t1, t2 -> unify_bop ctx t1 t2 TInt TBool pos

and typeof_bop_int ctx op t1 t2 pos =
match op with
| Add | Subt | Mult | Div | Mod -> unify_bop ctx t1 t2 TInt TInt pos
| Eq -> assert false
| Concat | Eq -> assert false

and typeof_let ctx name binding in_body =
enter_level ();
Expand Down
4 changes: 2 additions & 2 deletions test/parsing_test/parsing_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ let%test "char variable" =
| _ -> false

let%test "string variable" =
match test "let a = \"testing\"" with
match test "let a = \"testing stuff\"" with
| Let
{
name = "a";
binding = { expr = Const (String "testing"); _ };
binding = { expr = Const (String "testing stuff"); _ };
in_body = None;
} ->
true
Expand Down

0 comments on commit e80ab4b

Please sign in to comment.