Skip to content

Commit 75bc86e

Browse files
committed
make assign expression
1 parent c62c7a9 commit 75bc86e

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

lib/ast.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ type token =
2121
(* unknown *)
2222
| KWD of char
2323
(* special chars *)
24+
| EQUALS
25+
| ASSIGN
2426
| LEFT_PAREN
2527
| RIGHT_PAREN
26-
| EQUALS
2728
| COMMA
2829
| SEMICOLON
2930
(* end of file *)

lib/ast.mli

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
open Core
22

33
type token =
4+
(* commands *)
45
| DEF
56
| EXTERN
7+
(* control *)
68
| IF
79
| THEN
810
| ELSE
911
| FOR
1012
| IN
13+
(* operators *)
1114
| BINARY
1215
| UNARY
16+
(* var definition *)
1317
| VAR
18+
(* primary *)
1419
| IDENT of string
1520
| NUMBER of float
21+
(* unknown *)
1622
| KWD of char
23+
(* special chars *)
24+
| EQUALS
25+
| ASSIGN
1726
| LEFT_PAREN
1827
| RIGHT_PAREN
19-
| EQUALS
2028
| COMMA
2129
| SEMICOLON
30+
(* end of file *)
2231
| EOF
2332
[@@deriving sexp]
2433

lib/lexer.mll

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
| newline { read lexbuf }
1717
| "def" { DEF }
1818
| "extern" { EXTERN }
19-
| "if" { IF }
20-
| "then" { THEN }
21-
| "else" { ELSE }
22-
| "for" { FOR }
23-
| "in" { IN }
24-
| "binary" { BINARY }
25-
| "unary" { UNARY }
26-
| "var" { VAR }
27-
| id { IDENT (Lexing.lexeme lexbuf) }
28-
| float { NUMBER (float_of_string (Lexing.lexeme lexbuf)) }
29-
| "=" { EQUALS }
30-
| "(" { LEFT_PAREN }
31-
| ")" { RIGHT_PAREN }
32-
| "," { COMMA }
33-
| ";" { SEMICOLON }
19+
| "if" { IF }
20+
| "then" { THEN }
21+
| "else" { ELSE }
22+
| "for" { FOR }
23+
| "in" { IN }
24+
| "binary" { BINARY }
25+
| "unary" { UNARY }
26+
| "var" { VAR }
27+
| id { IDENT (Lexing.lexeme lexbuf) }
28+
| float { NUMBER (float_of_string (Lexing.lexeme lexbuf)) }
29+
| ":=" { ASSIGN }
30+
| "(" { LEFT_PAREN }
31+
| ")" { RIGHT_PAREN }
32+
| "," { COMMA }
33+
| ";" { SEMICOLON }
3434
(* '#' marks the beginning of a comment *)
3535
| "#" { read_comment lexbuf }
36-
| _ { KWD (Lexing.lexeme_char lexbuf 0) }
37-
| eof { EOF }
36+
| _ { KWD (Lexing.lexeme_char lexbuf 0) }
37+
| eof { EOF }
3838

3939
and read_comment =
4040
parse

lib/parser.mly

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
%token <float> NUMBER
1313
%token <char> KWD
1414
%token EQUALS
15+
%token ASSIGN
1516
%token LEFT_PAREN
1617
%token RIGHT_PAREN
1718
%token COMMA
@@ -28,7 +29,7 @@
2829
| Some p -> p
2930
%}
3031

31-
%start < [`Expr of Ast.Expr.No_binop.func
32+
%start < [ `Expr of Ast.Expr.No_binop.func
3233
| `Extern of Ast.proto
3334
| `Def of Ast.Expr.No_binop.func
3435
| `Eof ]> toplevel
@@ -75,7 +76,7 @@ block:
7576

7677
(* var
7778
* ::= 'var' identifier ('=' expression)? *)
78-
var: name = IDENT; e = option(EQUALS; e = expr { e }) { (name, e) }
79+
var: name = IDENT; e = option(ASSIGN; e = expr { e }) { (name, e) }
7980

8081
(* unary
8182
* ::= primary

0 commit comments

Comments
 (0)