Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions LMJ.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ and binop =
| OpAdd (** Binary operator [+]. *)
| OpSub (** Binary operator [-]. *)
| OpMul (** Binary operator [*]. *)
| OpLt (** Binary operator [<]. *)
| OpEq (** Binary operator [==]. *)
| OpLt (** Binary operator [<]. *)
| OpGt (** Binary operator [>]. *)

| OpAnd (** Binary operator [&&]. *)
| OpOr (** BInary operator [||]. *)

and unop = UOpNot (** Unary operator [!]. *)

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ git checkout master
make
```

The branch `ast_typed` has an abstract syntax tree decorated with type information. This tree is produced by the typechecker and it can be useful if you want to augment `MiniJava`.

3 changes: 3 additions & 0 deletions TMJ.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ and binop = LMJ.binop =
| OpSub
| OpMul
| OpLt
| OpEq
| OpGt
| OpAnd
| OpOr

and unop = LMJ.unop = UOpNot

Expand Down
3 changes: 3 additions & 0 deletions lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ rule get_token = parse
| '-' { MINUS }
| '*' { TIMES }
| "&&" { AND }
| "||" { OR }
| "<" { LT }
| "==" { EQ }
| ">" { GT }
| '(' { LPAREN }
| ')' { RPAREN }
| '[' { LBRACKET }
Expand Down
3 changes: 3 additions & 0 deletions mj2c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ let binop2c
| OpSub -> fprintf out "-"
| OpMul -> fprintf out "*"
| OpLt -> fprintf out "<"
| OpEq -> fprintf out "=="
| OpGt -> fprintf out ">"
| OpAnd -> fprintf out "&&"
| OpOr -> fprintf out "||"

(** [type2c out typ] transpiles the type [typ] to C on the output channel [out]. *)
let type2c
Expand Down
13 changes: 11 additions & 2 deletions parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
%token INTEGER BOOLEAN
%token <string Location.t> IDENT
%token CLASS PUBLIC STATIC VOID MAIN STRING EXTENDS RETURN
%token PLUS MINUS TIMES NOT LT AND
%token PLUS MINUS TIMES NOT LT AND EQ
%token PLUS MINUS TIMES NOT LT GT AND OR
%token COMMA SEMICOLON
%token ASSIGN
%token LPAREN RPAREN LBRACKET RBRACKET LBRACE RBRACE
Expand All @@ -17,8 +18,9 @@
%token IF ELSE WHILE
%token EOF

%left OR
%left AND
%nonassoc LT
%nonassoc LT GT
%left PLUS MINUS
%left TIMES
%nonassoc NOT
Expand Down Expand Up @@ -146,7 +148,10 @@ raw_expression:
| MINUS { OpSub }
| TIMES { OpMul }
| LT { OpLt }
| EQ { OpEq }
| GT { OpGt }
| AND { OpAnd }
| OR { OpOr }

instruction:
| b = block
Expand All @@ -164,6 +169,10 @@ instruction:
| IF LPAREN c = expression RPAREN i1 = instruction ELSE i2 = instruction
{ IIf (c, i1, i2) }

// if sans else
| IF LPAREN c = expression RPAREN i1 = instruction // ce qu'on lit
{ IIf (c, i1, IBlock([])) } // ce qu'on reconnait

| WHILE LPAREN c = expression RPAREN i = instruction
{ IWhile (c, i) }

Expand Down
9 changes: 8 additions & 1 deletion printTMJ.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ let binop out = function
fprintf out "*"
| OpLt ->
fprintf out "<"
| OpEq ->
fprintf out "=="
| OpGt ->
fprintf out ">"
| OpAnd ->
fprintf out "&&"
| OpOr ->
fprintf out "||"

(** [expr out e], [expr0 out e], ..., [expr6 out e] print the expression [e]
on the output channel [out]. [expr] is a synonym for [expr6].
Expand Down Expand Up @@ -103,7 +109,8 @@ and expr5 out e = match e.raw_expression with
expr4 out e

and expr6 out e = match e.raw_expression with
| EBinOp ((OpLt | OpAnd) as op, e1, e2) ->
| EBinOp ((OpLt | OpAnd | OpEq) as op, e1, e2) ->
| EBinOp ((OpLt | OpGt | OpAnd | OpOr) as op, e1, e2) ->
fprintf out "%a %a %a"
expr6 e1
binop op
Expand Down
6 changes: 6 additions & 0 deletions print_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ let print_binop out = function
fprintf out "OpMul"
| OpLt ->
fprintf out "OpLt"
| OpEq ->
fprintf out "OpEq"
| OpGt ->
fprintf out "OpGt"
| OpAnd ->
fprintf out "OpAnd"
| OpOr ->
fprintf out "OpOr"

(** [print_expression prefix out e] prints the expression [e] on the output channel [out].
[prefix] is the string already printed just before [e]. *)
Expand Down
6 changes: 6 additions & 0 deletions print_tokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ let print_token show_loc out = function
fprintf out "NOT"
| LT ->
fprintf out "LT"
| EQ ->
fprintf out "EQ"
| GT ->
fprintf out "GT"
| AND ->
fprintf out "AND"
| OR ->
fprintf out "OR"
| COMMA ->
fprintf out "COMMA"
| SEMICOLON ->
Expand Down
Binary file added test/good/TestEq
Binary file not shown.
23 changes: 23 additions & 0 deletions test/good/TestEq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
class TestEq {
public static void main(String[] args) {
if (1 == 1) System.out.println(42);
else System.out.println(0);
}
}
*/
#include <stdio.h>
#include <stdlib.h>
#include "tgc.h"
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
struct array { int* array; int length; };
tgc_t gc;
int main(int argc, char *argv[]) {
tgc_start(&gc, &argc);
if ((1 == 1)) printf("%d\n", 42);
else printf("%d\n", 0);
tgc_stop(&gc);

return 0;
}
6 changes: 6 additions & 0 deletions test/good/TestEq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TestEq {
public static void main(String[] args){
if(1==1) System.out.println(42);
else System.out.println(0);
}
}
6 changes: 6 additions & 0 deletions test/good/TestGt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TestGt {
public static void main(String[] args){
if(2>1) System.out.println(42);
else System.out.println(0);
}
}
6 changes: 6 additions & 0 deletions test/good/TestOr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TestOr {
public static void main(String[] args) {
if(true || false) System.out.println(42);
else System.out.println(0);
}
}
3 changes: 3 additions & 0 deletions typechecking.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ and typecheck_expression (cenv : class_env) (venv : variable_env) (vinit : S.t)
| OpSub
| OpMul -> TypInt, TypInt
| OpLt -> TypInt, TypBool
| OpEq -> TypInt, TypBool
| OpGt -> TypInt, TypBool
| OpAnd -> TypBool, TypBool
| OpOr -> TypBool, TypBool
in
let e1' = typecheck_expression_expecting cenv venv vinit instanceof expected e1 in
let e2' = typecheck_expression_expecting cenv venv vinit instanceof expected e2 in
Expand Down