From 14f911cca7321f5f81a32e1dce2bbb96f09aad6c Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Sat, 6 Jan 2018 17:43:00 +0100 Subject: [PATCH 01/14] change bash file content for local use --- build-local | 2 ++ test-local | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 build-local create mode 100644 test-local diff --git a/build-local b/build-local new file mode 100644 index 0000000..7d12830 --- /dev/null +++ b/build-local @@ -0,0 +1,2 @@ +#!/bin/bash +ocamlbuild Main.byte diff --git a/test-local b/test-local new file mode 100644 index 0000000..54dac45 --- /dev/null +++ b/test-local @@ -0,0 +1,10 @@ +#!/bin/bash +ocamlbuild Main.byte && +FILES=./Evaluator/* +for f in $FILES +do + echo -e "\033[31m Unit test of $f ..." + # take action on each file. $f store current file name + echo -e "\033[0m" + ./_build/Main/Main.byte $f +done From 740524ccbad2cfbe6a8a89b6ed47671baa7331bb Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Sat, 6 Jan 2018 21:54:36 +0100 Subject: [PATCH 02/14] class: add 3 keywords for class (class new this) --- Parsing/LexicalAnalyser.mll | 261 +++++++++++++++++++----------------- 1 file changed, 136 insertions(+), 125 deletions(-) diff --git a/Parsing/LexicalAnalyser.mll b/Parsing/LexicalAnalyser.mll index d17fbce..8ba2f2a 100644 --- a/Parsing/LexicalAnalyser.mll +++ b/Parsing/LexicalAnalyser.mll @@ -1,125 +1,136 @@ -{ - open SyntaxAnalyser - - let print_lexeme = function - | EOL -> print_string "EOL" - | EOF -> print_string "EOF" - | PLUS -> print_string "PLUS" - | MINUS -> print_string "MINUS" - | DIV -> print_string "DIV" - | TIMES -> print_string "TIMES" - | INCRE -> print_string "INCRE" - | DECRE -> print_string "DECRE" - | ASSIGN -> print_string "ASSIGN" - | GT -> print_string "GT" - | LT -> print_string "LT" - | NOT -> print_string "NOT" - | EQUAL -> print_string "EQUAL" - | NOTEQUAL -> print_string "NOTEQUAL" - | LE -> print_string "LE" - | GE -> print_string "GE" - | AND -> print_string "AND" - | OR -> print_string "OR" - | INT i -> print_string "INT("; print_int i; print_string ")" - | ENDOFLINECOMMENT ic -> print_string "ENDOFLINECOMMENT("; print_string ic; print_string ")" - | TRADITIONALCOMMENT mc -> print_string "TRADITIONALCOMMENT("; print_string mc; print_string ")" - | IF -> print_string "IF" - | ELSE -> print_string "ELSE" - | LOWERIDENT s -> print_string "LOWERIDENT("; print_string s; print_string ")" - | UPPERIDENT s -> print_string "UPPERIDENT("; print_string s; print_string ")" - | SEMICOLON -> print_string "SEMICOLON" - | LBRACE -> print_string "LBRACE" - | RBRACE -> print_string "RBRACE" - | LPAR -> print_string "LPAR" - | RPAR -> print_string "RPAR" - | BOOLEAN b -> print_string "BOOL("; print_string(string_of_bool b); print_string ")" - | NULL -> print_string "NULL" - - open Lexing - exception Eof - - type error = - | Illegal_character of char - | Illegal_int of string - exception Error of error * position * position - - let raise_error err lexbuf = - raise (Error(err, lexeme_start_p lexbuf, lexeme_end_p lexbuf)) - - (* Les erreurs. *) - let report_error = function - | Illegal_character c -> - print_string "Illegal character '"; - print_char c; - print_string "' " - | Illegal_int nb -> - print_string "The int "; - print_string nb; - print_string " is illegal " -} - -let lower_ch = ['a'-'z'] -let upper_ch = ['A'-'Z'] -let digit = ['0'-'9'] -let integer = digit+ -let lower_id = lower_ch (lower_ch | upper_ch | digit | '_')* -let upper_id = upper_ch (lower_ch | upper_ch | digit | '_')* -(* Literal *) -let str = '"' ([^ '"'] |'\092''"')* '"' -let boolean = "true" | "false" -let space = [' ' '\009' '\012'] -let newline = ('\010' | '\013' | "\013\010") -let not_newline = [^ '\n' '\r'] -(* Comments *) -let endofline_comment = "//" not_newline* -let not_star = [^ '*'] -let not_star_not_slash = [^ '*' '/'] -let traditional_comment = "/*" not_star* "*"+ (not_star_not_slash not_star* "*"+)* "/" - -rule nexttoken = parse - | newline { Location.incr_line lexbuf; nexttoken lexbuf } - | space+ { nexttoken lexbuf } - | endofline_comment as c { ENDOFLINECOMMENT c} - | traditional_comment as c { TRADITIONALCOMMENT c} - | "if" { IF } - | "else" { ELSE } - | boolean as bl { BOOLEAN (bool_of_string bl) } - | "null" { NULL } - | str as st { STRING st} - | eof { EOF } - | "++" { INCRE } - | "--" { DECRE } - | "+" { PLUS } - | "-" { MINUS } - | "/" { DIV } - | "*" { TIMES } - | "%" { MOD } - | "=" { ASSIGN } - | ">" { GT } - | "<" { LT } - | "!" { NOT } - | "==" { EQUAL } - | "!=" { NOTEQUAL } - | "<=" { LE } - | ">=" { GE } - | "&&" { AND } - | "||" { OR } - | ";" { SEMICOLON } - | "{" { LBRACE } - | "}" { RBRACE } - | "(" { LPAR } - | ")" { RPAR } - | integer as nb { try INT (int_of_string nb) with Failure "int_of_string" -> raise_error (Illegal_int(nb)) lexbuf } - | lower_id as str { LOWERIDENT str } - | upper_id as str { UPPERIDENT str } - | _ as c { raise_error (Illegal_character(c)) lexbuf } - - { - let rec examine_all lexbuf = - let res = nexttoken lexbuf in - print_lexeme res; - print_string " "; - match res with - | EOF -> () - | _ -> examine_all lexbuf - } +{ + open SyntaxAnalyser + + let print_lexeme = function + | EOL -> print_string "EOL" + | EOF -> print_string "EOF" + | PLUS -> print_string "PLUS" + | MINUS -> print_string "MINUS" + | DIV -> print_string "DIV" + | TIMES -> print_string "TIMES" + | INCRE -> print_string "INCRE" + | DECRE -> print_string "DECRE" + | ASSIGN -> print_string "ASSIGN" + | GT -> print_string "GT" + | LT -> print_string "LT" + | NOT -> print_string "NOT" + | EQUAL -> print_string "EQUAL" + | NOTEQUAL -> print_string "NOTEQUAL" + | LE -> print_string "LE" + | GE -> print_string "GE" + | AND -> print_string "AND" + | OR -> print_string "OR" + | INT i -> print_string "INT("; print_int i; print_string ")" + | ENDOFLINECOMMENT ic -> print_string "ENDOFLINECOMMENT("; print_string ic; print_string ")" + | TRADITIONALCOMMENT mc -> print_string "TRADITIONALCOMMENT("; print_string mc; print_string ")" + | IF -> print_string "IF" + | ELSE -> print_string "ELSE" + | LOWERIDENT s -> print_string "LOWERIDENT("; print_string s; print_string ")" + | UPPERIDENT s -> print_string "UPPERIDENT("; print_string s; print_string ")" + | SEMICOLON -> print_string "SEMICOLON" + | LBRACE -> print_string "LBRACE" + | RBRACE -> print_string "RBRACE" + | LPAR -> print_string "LPAR" + | RPAR -> print_string "RPAR" + | BOOLEAN b -> print_string "BOOL("; print_string(string_of_bool b); print_string ")" + | NULL -> print_string "NULL" + + (*Class print*) + | CLASS -> print_string "CLASS" + | NEW -> print_string "NEW" + | THIS -> print_string "THIS" + + + open Lexing + exception Eof + + type error = + | Illegal_character of char + | Illegal_int of string + exception Error of error * position * position + + let raise_error err lexbuf = + raise (Error(err, lexeme_start_p lexbuf, lexeme_end_p lexbuf)) + + (* Les erreurs. *) + let report_error = function + | Illegal_character c -> + print_string "Illegal character '"; + print_char c; + print_string "' " + | Illegal_int nb -> + print_string "The int "; + print_string nb; + print_string " is illegal " +} + +let lower_ch = ['a'-'z'] +let upper_ch = ['A'-'Z'] +let digit = ['0'-'9'] +let integer = digit+ +let lower_id = lower_ch (lower_ch | upper_ch | digit | '_')* +let upper_id = upper_ch (lower_ch | upper_ch | digit | '_')* +(* Literal *) +let str = '"' ([^ '"'] |'\092''"')* '"' +let boolean = "true" | "false" +let space = [' ' '\009' '\012'] +let newline = ('\010' | '\013' | "\013\010") +let not_newline = [^ '\n' '\r'] +(* Comments *) +let endofline_comment = "//" not_newline* +let not_star = [^ '*'] +let not_star_not_slash = [^ '*' '/'] +let traditional_comment = "/*" not_star* "*"+ (not_star_not_slash not_star* "*"+)* "/" + +rule nexttoken = parse + | newline { Location.incr_line lexbuf; nexttoken lexbuf } + | space+ { nexttoken lexbuf } + | endofline_comment as c { ENDOFLINECOMMENT c} + | traditional_comment as c { TRADITIONALCOMMENT c} + | "if" { IF } + | "else" { ELSE } + | boolean as bl { BOOLEAN (bool_of_string bl) } + | "null" { NULL } + | str as st { STRING st} + | eof { EOF } + | "++" { INCRE } + | "--" { DECRE } + | "+" { PLUS } + | "-" { MINUS } + | "/" { DIV } + | "*" { TIMES } + | "%" { MOD } + | "=" { ASSIGN } + | ">" { GT } + | "<" { LT } + | "!" { NOT } + | "==" { EQUAL } + | "!=" { NOTEQUAL } + | "<=" { LE } + | ">=" { GE } + | "&&" { AND } + | "||" { OR } + | ";" { SEMICOLON } + | "{" { LBRACE } + | "}" { RBRACE } + | "(" { LPAR } + | ")" { RPAR } + | integer as nb { try INT (int_of_string nb) with Failure "int_of_string" -> raise_error (Illegal_int(nb)) lexbuf } + | lower_id as str { LOWERIDENT str } + | upper_id as str { UPPERIDENT str } + | _ as c { raise_error (Illegal_character(c)) lexbuf } + + (*Class*) + | "class" { CLASS } + | "new" { NEW } + | "this" { THIS } + + { + let rec examine_all lexbuf = + let res = nexttoken lexbuf in + print_lexeme res; + print_string " "; + match res with + | EOF -> () + | _ -> examine_all lexbuf + } From 78af5d6fc979a4e96c49fc057dc65c9288f379e8 Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Sat, 6 Jan 2018 21:56:13 +0100 Subject: [PATCH 03/14] class: some simple syntax analyse for class --- Parsing/SyntaxAnalyser.mly | 262 ++++++++++++++++++++----------------- 1 file changed, 141 insertions(+), 121 deletions(-) diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 0d0fae3..6f57aba 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -1,121 +1,141 @@ -/* Headers */ -%{ - open Expression -%} - -/* Tokens */ -/* Seperators */ -%token EOF EOL LPAR RPAR SEMICOLON LBRACE RBRACE - -/* Operators*/ -%token ASSIGN -%token PLUS MINUS TIMES DIV MOD -%token INCRE DECRE NOT -%token GT LT EQUAL NOTEQUAL LE GE -%token AND OR - -/* Literals */ -%token INT -%token STRING -%token BOOLEAN -%token NULL - -/* Comments */ -%token ENDOFLINECOMMENT -%token TRADITIONALCOMMENT - -/* Identifiers*/ -%token LOWERIDENT UPPERIDENT - -/* Keywords */ -%token IF ELSE - -/* Start symbols and types */ -%start expression -%type < Expression.expression list> expression - -/* Priority and associativity */ -%right SEMICOLON -%right ASSIGN -%left OR -%left AND -%left EQUAL NOTEQUAL -%left GT GE LT LE -%left PLUS MINUS -%left TIMES DIV MOD -%right UMINUS UPLUS NOT INCRE DECRE -%left PINCRE PDECRE - -/* End of Declarations */ -%% - -/* Start of Rules */ -expression: - - | comment* EOF {[]} - | e=comment_or_expression r=expression EOF - { e::r } - -comment: - | ENDOFLINECOMMENT {} - | TRADITIONALCOMMENT {} - -comment_or_expression: - | comment* e=expr { e } - - -expr: - | e1=expr SEMICOLON - { Semi(e1)} - | LPAR e=expr RPAR - { e } - | o=uop e=expr - { Unop(o,e) } - | MINUS e=expr %prec UMINUS - { Unop(Uminus,e) } - | PLUS e=expr %prec UPLUS - { Unop(Uplus,e) } - | e=expr INCRE %prec PINCRE - { Postop(e,Pincre) } - | e=expr DECRE %prec PDECRE - { Postop(e,Pdecre) } - | e1=expr o=bop e2=expr - { Binop(o,e1,e2) } - | i=INT - { Int i } - | b=BOOLEAN - { Bool b } - | n=NULL - { Null } - | s=STRING - { String s} - | id=LOWERIDENT - { Var id } - | id=LOWERIDENT ASSIGN e=expr - { Assign(id, e) } - | IF LPAR e1=expr RPAR LBRACE e2=expr RBRACE - { IfThen(e1, e2) } - | IF LPAR e1=expr RPAR LBRACE e2=expr RBRACE ELSE LBRACE e3=expr RBRACE - { IfThenElse(e1, e2, e3) } - -%inline uop: - | NOT {Unot} - | INCRE {Uincre} - | DECRE {Udecre} - -%inline bop: - | MINUS { Bsub } - | PLUS { Badd } - | TIMES { Bmul } - | DIV { Bdiv } - | MOD { Bmod } - | GT { Bgt } - | LT { Blt } - | GE { Bge } - | LE { Ble } - | EQUAL { Bequal } - | NOTEQUAL { Bnotequal } - | OR { Bor } - | AND { Band } - -%% +/* Headers */ +%{ + open Expression +%} + +/* Class Keywords */ +%token CLASS NEW THIS + +/* Tokens */ +/* Seperators */ +%token EOF EOL LPAR RPAR SEMICOLON LBRACE RBRACE + +/* Operators*/ +%token ASSIGN +%token PLUS MINUS TIMES DIV MOD +%token INCRE DECRE NOT +%token GT LT EQUAL NOTEQUAL LE GE +%token AND OR + +/* Literals */ +%token INT +%token STRING +%token BOOLEAN +%token NULL + +/* Comments */ +%token ENDOFLINECOMMENT +%token TRADITIONALCOMMENT + +/* Identifiers*/ +%token LOWERIDENT UPPERIDENT + +/* Keywords */ +%token IF ELSE + +/* Start symbols and types */ +%start expression +%type < Expression.expression list> expression + +/* Priority and associativity */ +%right SEMICOLON +%right ASSIGN +%left OR +%left AND +%left EQUAL NOTEQUAL +%left GT GE LT LE +%left PLUS MINUS +%left TIMES DIV MOD +%right UMINUS UPLUS NOT INCRE DECRE +%left PINCRE PDECRE + +/* End of Declarations */ +%% + +/* Start of Rules */ +expression: + + | comment* EOF {[]} + | e=comment_or_expression r=expression EOF + { e::r } + | c=comment_or_class r=expression EOF + { c::r } + +comment: + | ENDOFLINECOMMENT {} + | TRADITIONALCOMMENT {} + +comment_or_expression: + | comment* e=expr { e } + +comment_or_class: + | comment* c=class { c } + +class: + | CLASS LBRACE a=attributes_or_methods RBRACE + +attributes_or_methods: + | comment* EOF {[]} + | a=attribute_or_method r=attributes_or_methods + {a::r} + +attribute_or_method: + | comment* a=attribute SEMICOLON + | comment* m=method + + +expr: + | e1=expr SEMICOLON + { Semi(e1)} + | LPAR e=expr RPAR + { e } + | o=uop e=expr + { Unop(o,e) } + | MINUS e=expr %prec UMINUS + { Unop(Uminus,e) } + | PLUS e=expr %prec UPLUS + { Unop(Uplus,e) } + | e=expr INCRE %prec PINCRE + { Postop(e,Pincre) } + | e=expr DECRE %prec PDECRE + { Postop(e,Pdecre) } + | e1=expr o=bop e2=expr + { Binop(o,e1,e2) } + | i=INT + { Int i } + | b=BOOLEAN + { Bool b } + | n=NULL + { Null } + | s=STRING + { String s} + | id=LOWERIDENT + { Var id } + | id=LOWERIDENT ASSIGN e=expr + { Assign(id, e) } + | IF LPAR e1=expr RPAR LBRACE e2=expr RBRACE + { IfThen(e1, e2) } + | IF LPAR e1=expr RPAR LBRACE e2=expr RBRACE ELSE LBRACE e3=expr RBRACE + { IfThenElse(e1, e2, e3) } + +%inline uop: + | NOT {Unot} + | INCRE {Uincre} + | DECRE {Udecre} + +%inline bop: + | MINUS { Bsub } + | PLUS { Badd } + | TIMES { Bmul } + | DIV { Bdiv } + | MOD { Bmod } + | GT { Bgt } + | LT { Blt } + | GE { Bge } + | LE { Ble } + | EQUAL { Bequal } + | NOTEQUAL { Bnotequal } + | OR { Bor } + | AND { Band } + +%% From 2aea52d989eb989c38a3d44b117a63b822b85bf4 Mon Sep 17 00:00:00 2001 From: KEYU PU Date: Sat, 6 Jan 2018 23:28:18 +0100 Subject: [PATCH 04/14] class: add some rules for methods of class --- Parsing/SyntaxAnalyser.mly | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 6f57aba..187b21a 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -83,7 +83,27 @@ attribute_or_method: | comment* a=attribute SEMICOLON | comment* m=method - +method: + | STATIC t=TYPE id=VAR LPAR p=params RPAR LBRACE e=expr RBRACE + { Method(true, t, id, p, e) } + | STATIC t=TYPE id=VAR LPAR RPAR LBRACE e=expr RBRACE + { Method(true, t, id, [], e) } + | t=TYPE id=VAR LPAR p=params RPAR LBRACE e=expr RBRACE + { Method(false, t, id, p, e) } + | t=TYPE id=VAR LPAR RPAR LBRACE e=expr RBRACE + { Method(false, t, id, [], e) } + +param: + | t=TYPE id=VAR + { Param(t,id) } + +params: + | { [] } + | t=TYPE id=VAR + { [Param(t,id)] } + | t=TYPE id=VAR COMMA r=param+ + { Param(t,id) :: r} + expr: | e1=expr SEMICOLON { Semi(e1)} From f2bcfc34c52de28d7ad9999a106a950eb28025b3 Mon Sep 17 00:00:00 2001 From: KEYU PU Date: Mon, 8 Jan 2018 12:13:55 +0100 Subject: [PATCH 05/14] class: using lowerIdent for method name and attribute name --- Parsing/LexicalAnalyser.mll | 2 ++ Parsing/SyntaxAnalyser.mly | 17 ++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Parsing/LexicalAnalyser.mll b/Parsing/LexicalAnalyser.mll index 8ba2f2a..155c1c5 100644 --- a/Parsing/LexicalAnalyser.mll +++ b/Parsing/LexicalAnalyser.mll @@ -39,6 +39,8 @@ | CLASS -> print_string "CLASS" | NEW -> print_string "NEW" | THIS -> print_string "THIS" + | STATIC -> print_string "STATIC" + | TYPE s -> print_string "TYPE("; print_string s; print_string ")" open Lexing diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 187b21a..ed8eb67 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -33,6 +33,9 @@ /* Keywords */ %token IF ELSE +/* Declarations of variables */ +%token TYPE + /* Start symbols and types */ %start expression %type < Expression.expression list> expression @@ -84,24 +87,24 @@ attribute_or_method: | comment* m=method method: - | STATIC t=TYPE id=VAR LPAR p=params RPAR LBRACE e=expr RBRACE + | STATIC t=TYPE id=LOWERIDENT LPAR p=params RPAR LBRACE e=expr RBRACE { Method(true, t, id, p, e) } - | STATIC t=TYPE id=VAR LPAR RPAR LBRACE e=expr RBRACE + | STATIC t=TYPE id=LOWERIDENT LPAR RPAR LBRACE e=expr RBRACE { Method(true, t, id, [], e) } - | t=TYPE id=VAR LPAR p=params RPAR LBRACE e=expr RBRACE + | t=TYPE id=LOWERIDENT LPAR p=params RPAR LBRACE e=expr RBRACE { Method(false, t, id, p, e) } - | t=TYPE id=VAR LPAR RPAR LBRACE e=expr RBRACE + | t=TYPE id=LOWERIDENT LPAR RPAR LBRACE e=expr RBRACE { Method(false, t, id, [], e) } param: - | t=TYPE id=VAR + | t=TYPE id=LOWERIDENT { Param(t,id) } params: | { [] } - | t=TYPE id=VAR + | t=TYPE id=LOWERIDENT { [Param(t,id)] } - | t=TYPE id=VAR COMMA r=param+ + | t=TYPE id=LOWERIDENT COMMA r=param+ { Param(t,id) :: r} expr: From 31a206b3643d6056d165b5ca8cc4f5d850be6715 Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Thu, 11 Jan 2018 16:11:48 +0100 Subject: [PATCH 06/14] detele type analyse for attribute in class --- Parsing/SyntaxAnalyser.mly | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 6f57aba..0d46117 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -54,7 +54,6 @@ /* Start of Rules */ expression: - | comment* EOF {[]} | e=comment_or_expression r=expression EOF { e::r } @@ -69,20 +68,22 @@ comment_or_expression: | comment* e=expr { e } comment_or_class: - | comment* c=class { c } + | comment* c=class_ { c } -class: - | CLASS LBRACE a=attributes_or_methods RBRACE +class_: + | CLASS UPPERIDENT LBRACE a=attributes_or_methods RBRACE + { a } -attributes_or_methods: - | comment* EOF {[]} - | a=attribute_or_method r=attributes_or_methods - {a::r} +attributes_or_methods: + | comment* { [] } + | a=attribute_or_method r=attributes_or_methods { a::r } attribute_or_method: - | comment* a=attribute SEMICOLON - | comment* m=method + | comment* a=attribute SEMICOLON { a } +attribute: + | id=LOWERIDENT {t} + | id=LOWERIDENT ASSIGN e=expr {t} expr: | e1=expr SEMICOLON From 1609c1f13398102f0c150c233ef73f0bcc5d35b5 Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Fri, 12 Jan 2018 14:59:18 +0100 Subject: [PATCH 07/14] problem of constructor Class_ to be solved --- Evaluator/Class.java | 4 + Parsing/Expression.ml | 192 +++++++++++++++++++++---------------- Parsing/SyntaxAnalyser.mly | 4 +- 3 files changed, 114 insertions(+), 86 deletions(-) create mode 100644 Evaluator/Class.java diff --git a/Evaluator/Class.java b/Evaluator/Class.java new file mode 100644 index 0000000..01edfcc --- /dev/null +++ b/Evaluator/Class.java @@ -0,0 +1,4 @@ +class Point { + x; + y=1; +} diff --git a/Parsing/Expression.ml b/Parsing/Expression.ml index af6f3fc..7115d3f 100644 --- a/Parsing/Expression.ml +++ b/Parsing/Expression.ml @@ -1,84 +1,108 @@ -type binop = - | Badd | Bsub | Bmul | Bdiv | Bmod - | Bgt | Blt | Bge | Ble | Bequal | Bnotequal - | Band | Bor - -type unop = - | Uplus | Uminus | Unot | Uincre | Udecre - -type postfix = - | Pincre | Pdecre - -type expression = - | Int of int - | Var of string - | Bool of bool - | Null - | String of string - | Assign of string * expression - | Binop of binop * expression * expression - | Unop of unop * expression - | Postop of expression * postfix - | Semi of expression - | IfThen of expression * expression - | IfThenElse of expression * expression * expression - - -exception Unbound_variable of string - -let get_op_u = function - | Uplus -> fun x -> x - | Uminus -> fun x -> -x - (* | Unot -> fun x -> not x *) - | Uincre -> fun x -> x + 1 - | Udecre -> fun x -> x - 1 - -let get_op_b op x y = - match op with - | Badd -> x + y - | Bsub -> x - y - | Bmul -> x * y - | Bdiv -> x / y - | Bmod -> x mod y - -let string_of_op_u = function - | Uplus -> "+" - | Uminus -> "-" - | Unot -> "not" - | Uincre -> "++" - | Udecre -> "--" - - let string_of_op_p = function - | Pincre -> "++" - | Pdecre -> "--" - -let string_of_op_b = function - | Badd -> "+" - | Bsub -> "-" - | Bmul -> "*" - | Bdiv -> "/" - | Bmod -> "%" - | Bgt -> " > " - | Blt -> " < " - | Bge -> " >= " - | Ble -> " <= " - | Bequal -> " = " - | Bnotequal -> " != " - | Band -> " && " - | Bor -> " || " - -let rec string_of_expr exp = - match exp with - | Int c -> "Int("^string_of_int c^")" - | Var v -> "Var("^v^")" - | Bool b -> string_of_bool b - | Null -> "null" - | String s -> "String("^s^")" - | Binop(op, e1, e2) -> "(" ^(string_of_expr e1)^ (string_of_op_b op) ^(string_of_expr e2)^ ")" - | Unop(op, e) -> "(" ^ (string_of_op_u op) ^(string_of_expr e)^ ")" - | Postop(e, op) -> "(" ^ (string_of_expr e) ^(string_of_op_p op)^ ")" - | Assign(s,e) -> "Assign(" ^s^ "=" ^(string_of_expr e)^ ")" - | Semi(e1) -> (string_of_expr e1)^ ";\n" - | IfThen(e1,e2) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" })\n" - | IfThenElse(e1,e2,e3) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" } Else {\n"^(string_of_expr e3)^" })\n" - \ No newline at end of file +type binop = + | Badd | Bsub | Bmul | Bdiv | Bmod + | Bgt | Blt | Bge | Ble | Bequal | Bnotequal + | Band | Bor + +type unop = + | Uplus | Uminus | Unot | Uincre | Udecre + +type postfix = + | Pincre | Pdecre + +type expression = + | Int of int + | Var of string + | Bool of bool + | Null + | String of string + | Assign of string * expression + | Binop of binop * expression * expression + | Unop of unop * expression + | Postop of expression * postfix + | Semi of expression + | IfThen of expression * expression + | IfThenElse of expression * expression * expression + +type attr = + | Attr of string + | AttrWithAssign of string + +type attr_or_method = + | Attribute of attr + +type class_ = + | Class_ of attr_or_method list + +exception Unbound_variable of string + +let get_op_u = function + | Uplus -> fun x -> x + | Uminus -> fun x -> -x + (* | Unot -> fun x -> not x *) + | Uincre -> fun x -> x + 1 + | Udecre -> fun x -> x - 1 + +let get_op_b op x y = + match op with + | Badd -> x + y + | Bsub -> x - y + | Bmul -> x * y + | Bdiv -> x / y + | Bmod -> x mod y + +let string_of_op_u = function + | Uplus -> "+" + | Uminus -> "-" + | Unot -> "not" + | Uincre -> "++" + | Udecre -> "--" + +let string_of_op_p = function + | Pincre -> "++" + | Pdecre -> "--" + +let string_of_op_b = function + | Badd -> "+" + | Bsub -> "-" + | Bmul -> "*" + | Bdiv -> "/" + | Bmod -> "%" + | Bgt -> " > " + | Blt -> " < " + | Bge -> " >= " + | Ble -> " <= " + | Bequal -> " = " + | Bnotequal -> " != " + | Band -> " && " + | Bor -> " || " + +let rec string_of_expr exp = + match exp with + | Int c -> "Int("^string_of_int c^")" + | Var v -> "Var("^v^")" + | Bool b -> string_of_bool b + | Null -> "null" + | String s -> "String("^s^")" + | Binop(op, e1, e2) -> "(" ^(string_of_expr e1)^ (string_of_op_b op) ^(string_of_expr e2)^ ")" + | Unop(op, e) -> "(" ^ (string_of_op_u op) ^(string_of_expr e)^ ")" + | Postop(e, op) -> "(" ^ (string_of_expr e) ^(string_of_op_p op)^ ")" + | Assign(s,e) -> "Assign(" ^s^ "=" ^(string_of_expr e)^ ")" + | Semi(e1) -> (string_of_expr e1)^ ";\n" + | IfThen(e1,e2) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" })\n" + | IfThenElse(e1,e2,e3) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" } Else {\n"^(string_of_expr e3)^" })\n" + + + +let string_of_attr = function + | Attr a -> "Attr" ^a + | AttrWithAssign a -> "AttrWithAssign" ^a + +let string_of_attr_or_method = function + | Attribute a -> string_of_attr a + +let rec string_of_attrs_or_methods = function + | [] -> "" + | a::l -> (string_of_attr_or_method a) ^ " " ^ (string_of_attrs_or_methods l) + +let string_of_class = function + | Class_ am -> string_of_attrs_or_methods am diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 0d46117..8f7550d 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -72,8 +72,8 @@ comment_or_class: class_: | CLASS UPPERIDENT LBRACE a=attributes_or_methods RBRACE - { a } - + { Class_(a) } + attributes_or_methods: | comment* { [] } | a=attribute_or_method r=attributes_or_methods { a::r } From dd0dac1784e04934d7d32746909b2b73ff5d3b21 Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Fri, 12 Jan 2018 16:19:18 +0100 Subject: [PATCH 08/14] solve Class compile problem and update Compile.ml file --- Main/Compile.ml | 66 +++++++++++++++++++------------------- Parsing/Expression.ml | 12 +++++-- Parsing/SyntaxAnalyser.mly | 35 +++++++++++--------- 3 files changed, 63 insertions(+), 50 deletions(-) diff --git a/Main/Compile.ml b/Main/Compile.ml index 47f38dd..6ad3b33 100644 --- a/Main/Compile.ml +++ b/Main/Compile.ml @@ -1,33 +1,33 @@ -open LexicalAnalyser -open SyntaxAnalyser -open Expression - -let print_expressions_or_classes exp = - (* Print the AST *) - print_string (string_of_expr exp) - -(* verbose is a boolean that you can use to switch to a verbose output (for example, to dump all the ast) *) -let execute lexbuf verbose = - - try - (* to enable LexicalAnalyser, plase un comment this block *) - (* print_endline ("Doing LexicalAnalyser...."); - LexicalAnalyser.examine_all lexbuf; - print_string "\n"; *) - print_endline ("Doing SyntaxAnalyser...."); - let exp_list = SyntaxAnalyser.expression nexttoken lexbuf in - print_endline("Start printing AST..."); - List.iter print_expressions_or_classes exp_list; - print_newline(); - with - | LexicalAnalyser.Error (kind, s, e) -> - print_string("LexicalAnalyser error: "); - report_error kind; - let pos = Location.curr lexbuf in - Location.print pos; - print_newline(); - | Error -> - print_string("SyntaxAnalyser error: "); - let pos = Location.curr lexbuf in - Location.print pos; - | _ -> print_string("Unknown error") +open LexicalAnalyser +open SyntaxAnalyser +open Expression + +let print_expressions_or_classes exp = + (* Print the AST *) + print_string (string_of_class_or_expr exp) + +(* verbose is a boolean that you can use to switch to a verbose output (for example, to dump all the ast) *) +let execute lexbuf verbose = + + try + (* to enable LexicalAnalyser, plase un comment this block *) + (* print_endline ("Doing LexicalAnalyser...."); + LexicalAnalyser.examine_all lexbuf; + print_string "\n"; *) + print_endline ("Doing SyntaxAnalyser...."); + let exp_list = SyntaxAnalyser.content nexttoken lexbuf in + print_endline("Start printing AST..."); + List.iter print_expressions_or_classes exp_list; + print_newline(); + with + | LexicalAnalyser.Error (kind, s, e) -> + print_string("LexicalAnalyser error: "); + report_error kind; + let pos = Location.curr lexbuf in + Location.print pos; + print_newline(); + | Error -> + print_string("SyntaxAnalyser error: "); + let pos = Location.curr lexbuf in + Location.print pos; + | _ -> print_string("Unknown error") diff --git a/Parsing/Expression.ml b/Parsing/Expression.ml index 7115d3f..482b850 100644 --- a/Parsing/Expression.ml +++ b/Parsing/Expression.ml @@ -31,7 +31,11 @@ type attr_or_method = | Attribute of attr type class_ = - | Class_ of attr_or_method list + | Class_ of string* attr_or_method list + +type class_or_expr = + | Class of class_ + | Expr of expression exception Unbound_variable of string @@ -105,4 +109,8 @@ let rec string_of_attrs_or_methods = function | a::l -> (string_of_attr_or_method a) ^ " " ^ (string_of_attrs_or_methods l) let string_of_class = function - | Class_ am -> string_of_attrs_or_methods am + | Class_(id,am) -> "Class(" ^id^ "{" ^ (string_of_attrs_or_methods am) ^ " })" + +let string_of_class_or_expr = function + | Class c -> string_of_class c + | Expr e -> string_of_expr e diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 8f7550d..8e21dc2 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -33,9 +33,6 @@ /* Keywords */ %token IF ELSE -/* Start symbols and types */ -%start expression -%type < Expression.expression list> expression /* Priority and associativity */ %right SEMICOLON @@ -50,40 +47,48 @@ %left PINCRE PDECRE /* End of Declarations */ + + +/* Start symbols and types */ +%start content +%type < Expression.class_or_expr list> content + %% /* Start of Rules */ -expression: +content: | comment* EOF {[]} - | e=comment_or_expression r=expression EOF - { e::r } - | c=comment_or_class r=expression EOF + | c=class_or_expression r=content EOF { c::r } +class_or_expression: + | e=comment_or_expression {e} + | c=comment_or_class {c} + comment: | ENDOFLINECOMMENT {} | TRADITIONALCOMMENT {} comment_or_expression: - | comment* e=expr { e } + | comment* e=expr { Expr(e) } comment_or_class: - | comment* c=class_ { c } + | comment* c=class_ { Class(c) } class_: - | CLASS UPPERIDENT LBRACE a=attributes_or_methods RBRACE - { Class_(a) } - + | CLASS id=LOWERIDENT LBRACE a=attributes_or_methods RBRACE + { Class_(id,a) } + attributes_or_methods: | comment* { [] } | a=attribute_or_method r=attributes_or_methods { a::r } attribute_or_method: - | comment* a=attribute SEMICOLON { a } + | comment* a=attribute SEMICOLON { Attribute(a) } attribute: - | id=LOWERIDENT {t} - | id=LOWERIDENT ASSIGN e=expr {t} + | id=LOWERIDENT {Attr(id)} + | id=LOWERIDENT ASSIGN expr {AttrWithAssign(id)} expr: | e1=expr SEMICOLON From 62fab09be341fef7d8aa2c616c3ae487bb594ed5 Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Fri, 12 Jan 2018 16:46:09 +0100 Subject: [PATCH 09/14] success of class identification and attribute --- Parsing/LexicalAnalyser.mll | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Parsing/LexicalAnalyser.mll b/Parsing/LexicalAnalyser.mll index 8ba2f2a..826967c 100644 --- a/Parsing/LexicalAnalyser.mll +++ b/Parsing/LexicalAnalyser.mll @@ -83,6 +83,11 @@ let not_star_not_slash = [^ '*' '/'] let traditional_comment = "/*" not_star* "*"+ (not_star_not_slash not_star* "*"+)* "/" rule nexttoken = parse + (*Class*) + | "class" { CLASS } + | "new" { NEW } + | "this" { THIS } + | newline { Location.incr_line lexbuf; nexttoken lexbuf } | space+ { nexttoken lexbuf } | endofline_comment as c { ENDOFLINECOMMENT c} @@ -120,10 +125,7 @@ rule nexttoken = parse | upper_id as str { UPPERIDENT str } | _ as c { raise_error (Illegal_character(c)) lexbuf } - (*Class*) - | "class" { CLASS } - | "new" { NEW } - | "this" { THIS } + { let rec examine_all lexbuf = From 5a82b8075c979133d6f3b2baf678e96925c2cdd7 Mon Sep 17 00:00:00 2001 From: ZHOU XIAOFENG Date: Fri, 12 Jan 2018 16:55:41 +0100 Subject: [PATCH 10/14] update Class.java file for test --- Evaluator/Class.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Evaluator/Class.java b/Evaluator/Class.java index 01edfcc..aa472a1 100644 --- a/Evaluator/Class.java +++ b/Evaluator/Class.java @@ -1,4 +1,3 @@ -class Point { - x; - y=1; +class point{ + i; } From 2f777365ad3c891ee4ddf222d28395da5d7b12c2 Mon Sep 17 00:00:00 2001 From: KEYU PU Date: Sat, 13 Jan 2018 00:01:40 +0100 Subject: [PATCH 11/14] class : identification of method, add some tokens and definie pattern --- Evaluator/Class.java | 3 +++ Parsing/Expression.ml | 31 +++++++++++++++++++++++++++++ Parsing/LexicalAnalyser.mll | 6 +++++- Parsing/SyntaxAnalyser.mly | 39 ++++++++++++++++++++----------------- 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/Evaluator/Class.java b/Evaluator/Class.java index aa472a1..cdd2622 100644 --- a/Evaluator/Class.java +++ b/Evaluator/Class.java @@ -1,3 +1,6 @@ class point{ i; + String t(String str){ + "ceshi" + } } diff --git a/Parsing/Expression.ml b/Parsing/Expression.ml index 482b850..4b0137d 100644 --- a/Parsing/Expression.ml +++ b/Parsing/Expression.ml @@ -22,6 +22,15 @@ type expression = | Semi of expression | IfThen of expression * expression | IfThenElse of expression * expression * expression + | New of string + | This + | Invoke of expression * string * expression list + +type param = + | Param of string * string + +type mthd = + | Method of bool * string * string * (param list) * expression type attr = | Attr of string @@ -29,6 +38,7 @@ type attr = type attr_or_method = | Attribute of attr + | Method_t of mthd type class_ = | Class_ of string* attr_or_method list @@ -94,8 +104,26 @@ let rec string_of_expr exp = | Semi(e1) -> (string_of_expr e1)^ ";\n" | IfThen(e1,e2) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" })\n" | IfThenElse(e1,e2,e3) -> "If("^(string_of_expr e1)^") {\n"^(string_of_expr e2 )^" } Else {\n"^(string_of_expr e3)^" })\n" + | This -> "This(this)" + | Invoke(e, s, l) -> "Invoke("^(string_of_expr e)^"."^s^"("^(string_of_exprs l)^"))" + +and string_of_exprs = function + | [] -> "" + | e::l -> string_of_expr e ^ string_of_exprs l +let rec string_of_param p = match p with + | Param(t,id) -> t^" "^id +let rec string_of_params params = match params with + | [] -> "" + | (p :: l) -> string_of_param(p) ^ ", " ^ string_of_params l + +let string_of_static_bool = function + | true -> "static" + | false -> "non-static" + +let string_of_method = function + | Method(static,s1,s2,p,e) -> "Method("^string_of_static_bool static^ " " ^s1^" "^s2^"(Params("^(string_of_params p)^")){"^(string_of_expr e)^"})" let string_of_attr = function | Attr a -> "Attr" ^a @@ -103,6 +131,7 @@ let string_of_attr = function let string_of_attr_or_method = function | Attribute a -> string_of_attr a + | Method_t m -> string_of_method m let rec string_of_attrs_or_methods = function | [] -> "" @@ -114,3 +143,5 @@ let string_of_class = function let string_of_class_or_expr = function | Class c -> string_of_class c | Expr e -> string_of_expr e + + diff --git a/Parsing/LexicalAnalyser.mll b/Parsing/LexicalAnalyser.mll index a903993..3ef949f 100644 --- a/Parsing/LexicalAnalyser.mll +++ b/Parsing/LexicalAnalyser.mll @@ -34,6 +34,7 @@ | RPAR -> print_string "RPAR" | BOOLEAN b -> print_string "BOOL("; print_string(string_of_bool b); print_string ")" | NULL -> print_string "NULL" + | COMMA -> print_string "COMMA" (*Class print*) | CLASS -> print_string "CLASS" @@ -89,7 +90,10 @@ rule nexttoken = parse | "class" { CLASS } | "new" { NEW } | "this" { THIS } - + | "." { DOT } + | "," { COMMA} + | "static" { STATIC } + | newline { Location.incr_line lexbuf; nexttoken lexbuf } | space+ { nexttoken lexbuf } | endofline_comment as c { ENDOFLINECOMMENT c} diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 3c9933e..924bf81 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -8,7 +8,7 @@ /* Tokens */ /* Seperators */ -%token EOF EOL LPAR RPAR SEMICOLON LBRACE RBRACE +%token EOF EOL LPAR RPAR SEMICOLON LBRACE RBRACE COMMA /* Operators*/ %token ASSIGN @@ -16,6 +16,7 @@ %token INCRE DECRE NOT %token GT LT EQUAL NOTEQUAL LE GE %token AND OR +%token DOT /* Literals */ %token INT @@ -31,17 +32,11 @@ %token LOWERIDENT UPPERIDENT /* Keywords */ -%token IF ELSE +%token IF ELSE STATIC -<<<<<<< HEAD /* Declarations of variables */ %token TYPE -/* Start symbols and types */ -%start expression -%type < Expression.expression list> expression -======= ->>>>>>> 5a82b8075c979133d6f3b2baf678e96925c2cdd7 /* Priority and associativity */ %right SEMICOLON @@ -88,15 +83,19 @@ class_: | CLASS id=LOWERIDENT LBRACE a=attributes_or_methods RBRACE { Class_(id,a) } -attributes_or_methods: - | comment* { [] } +attributes_or_methods: + | comment* { [] } | a=attribute_or_method r=attributes_or_methods { a::r } attribute_or_method: | comment* a=attribute SEMICOLON { Attribute(a) } + | comment* m=method1 { Method_t(m) } + +attribute: + | id=LOWERIDENT {Attr(id)} + | id=LOWERIDENT ASSIGN expr {AttrWithAssign(id)} -<<<<<<< HEAD -method: +method1: | STATIC t=TYPE id=LOWERIDENT LPAR p=params RPAR LBRACE e=expr RBRACE { Method(true, t, id, p, e) } | STATIC t=TYPE id=LOWERIDENT LPAR RPAR LBRACE e=expr RBRACE @@ -117,12 +116,6 @@ params: | t=TYPE id=LOWERIDENT COMMA r=param+ { Param(t,id) :: r} -======= -attribute: - | id=LOWERIDENT {Attr(id)} - | id=LOWERIDENT ASSIGN expr {AttrWithAssign(id)} - ->>>>>>> 5a82b8075c979133d6f3b2baf678e96925c2cdd7 expr: | e1=expr SEMICOLON { Semi(e1)} @@ -156,6 +149,15 @@ expr: { IfThen(e1, e2) } | IF LPAR e1=expr RPAR LBRACE e2=expr RBRACE ELSE LBRACE e3=expr RBRACE { IfThenElse(e1, e2, e3) } + | e=expr DOT mthd=LOWERIDENT LPAR args_=args RPAR + { Invoke(e, mthd, args_) } + | NEW t =TYPE + { New(t)} + +args: + | { [] } + | e=expr { [e] } + | e=expr COMMA res=args { e :: res} %inline uop: | NOT {Unot} @@ -177,4 +179,5 @@ expr: | OR { Bor } | AND { Band } + %% From b7ab09cc7834a1d4724ba397c6c8df457254d954 Mon Sep 17 00:00:00 2001 From: KEYU PU Date: Sat, 13 Jan 2018 22:36:59 +0100 Subject: [PATCH 12/14] class : Debug for method, add unit test of method --- Evaluator/Class.java | 6 +++--- Main/Compile.ml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Evaluator/Class.java b/Evaluator/Class.java index cdd2622..39b3a9d 100644 --- a/Evaluator/Class.java +++ b/Evaluator/Class.java @@ -1,6 +1,6 @@ -class point{ +class Point{ i; - String t(String str){ - "ceshi" + Int ceshi(){ + 5; } } diff --git a/Main/Compile.ml b/Main/Compile.ml index 6ad3b33..0ba9596 100644 --- a/Main/Compile.ml +++ b/Main/Compile.ml @@ -11,9 +11,9 @@ let execute lexbuf verbose = try (* to enable LexicalAnalyser, plase un comment this block *) - (* print_endline ("Doing LexicalAnalyser...."); + print_endline ("Doing LexicalAnalyser...."); LexicalAnalyser.examine_all lexbuf; - print_string "\n"; *) + print_string "\n"; print_endline ("Doing SyntaxAnalyser...."); let exp_list = SyntaxAnalyser.content nexttoken lexbuf in print_endline("Start printing AST..."); From 8406e942a5442278ef6821208adf7ddad117b51e Mon Sep 17 00:00:00 2001 From: JinhaiZ Date: Sat, 13 Jan 2018 22:55:14 +0100 Subject: [PATCH 13/14] Expression: add print function for STRING --- Parsing/LexicalAnalyser.mll | 1 + 1 file changed, 1 insertion(+) diff --git a/Parsing/LexicalAnalyser.mll b/Parsing/LexicalAnalyser.mll index 3ef949f..589c572 100644 --- a/Parsing/LexicalAnalyser.mll +++ b/Parsing/LexicalAnalyser.mll @@ -35,6 +35,7 @@ | BOOLEAN b -> print_string "BOOL("; print_string(string_of_bool b); print_string ")" | NULL -> print_string "NULL" | COMMA -> print_string "COMMA" + | STRING s-> print_string "STRING("; print_string s; print_string ")"; (*Class print*) | CLASS -> print_string "CLASS" From 79394b1d110ca47647fc3da13c4215c6eb90a652 Mon Sep 17 00:00:00 2001 From: KEYU PU Date: Sun, 14 Jan 2018 10:44:51 +0100 Subject: [PATCH 14/14] class: add comment in class and fix bug of keyword this --- Evaluator/Class.java | 15 ++++++++++++--- Main/Compile.ml | 4 ++-- Parsing/Expression.ml | 4 ++-- Parsing/LexicalAnalyser.mll | 2 +- Parsing/SyntaxAnalyser.mly | 30 ++++++++++++++++++------------ 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/Evaluator/Class.java b/Evaluator/Class.java index 39b3a9d..80acb4e 100644 --- a/Evaluator/Class.java +++ b/Evaluator/Class.java @@ -1,6 +1,15 @@ class Point{ - i; - Int ceshi(){ - 5; + // Point class + i=10; + s="tt"; + Int test(){ + i=5; + } + static String test(){ + s="test"; + // comment + this.test(); + /* multi line comment + */ } } diff --git a/Main/Compile.ml b/Main/Compile.ml index 0ba9596..6ad3b33 100644 --- a/Main/Compile.ml +++ b/Main/Compile.ml @@ -11,9 +11,9 @@ let execute lexbuf verbose = try (* to enable LexicalAnalyser, plase un comment this block *) - print_endline ("Doing LexicalAnalyser...."); + (* print_endline ("Doing LexicalAnalyser...."); LexicalAnalyser.examine_all lexbuf; - print_string "\n"; + print_string "\n"; *) print_endline ("Doing SyntaxAnalyser...."); let exp_list = SyntaxAnalyser.content nexttoken lexbuf in print_endline("Start printing AST..."); diff --git a/Parsing/Expression.ml b/Parsing/Expression.ml index 4b0137d..412b297 100644 --- a/Parsing/Expression.ml +++ b/Parsing/Expression.ml @@ -30,7 +30,7 @@ type param = | Param of string * string type mthd = - | Method of bool * string * string * (param list) * expression + | Method of bool * string * string * (param list) * expression list type attr = | Attr of string @@ -123,7 +123,7 @@ let string_of_static_bool = function | false -> "non-static" let string_of_method = function - | Method(static,s1,s2,p,e) -> "Method("^string_of_static_bool static^ " " ^s1^" "^s2^"(Params("^(string_of_params p)^")){"^(string_of_expr e)^"})" + | Method(static,s1,s2,p,e) -> "Method("^string_of_static_bool static^ " " ^s1^" "^s2^"(Params("^(string_of_params p)^")){"^(string_of_exprs e)^"})" let string_of_attr = function | Attr a -> "Attr" ^a diff --git a/Parsing/LexicalAnalyser.mll b/Parsing/LexicalAnalyser.mll index 589c572..617fb50 100644 --- a/Parsing/LexicalAnalyser.mll +++ b/Parsing/LexicalAnalyser.mll @@ -42,7 +42,7 @@ | NEW -> print_string "NEW" | THIS -> print_string "THIS" | STATIC -> print_string "STATIC" - | TYPE s -> print_string "TYPE("; print_string s; print_string ")" + | DOT -> print_string "." open Lexing diff --git a/Parsing/SyntaxAnalyser.mly b/Parsing/SyntaxAnalyser.mly index 924bf81..8824646 100644 --- a/Parsing/SyntaxAnalyser.mly +++ b/Parsing/SyntaxAnalyser.mly @@ -34,9 +34,6 @@ /* Keywords */ %token IF ELSE STATIC -/* Declarations of variables */ -%token TYPE - /* Priority and associativity */ %right SEMICOLON @@ -80,7 +77,7 @@ comment_or_class: | comment* c=class_ { Class(c) } class_: - | CLASS id=LOWERIDENT LBRACE a=attributes_or_methods RBRACE + | CLASS id=UPPERIDENT LBRACE a=attributes_or_methods RBRACE { Class_(id,a) } attributes_or_methods: @@ -96,27 +93,35 @@ attribute: | id=LOWERIDENT ASSIGN expr {AttrWithAssign(id)} method1: - | STATIC t=TYPE id=LOWERIDENT LPAR p=params RPAR LBRACE e=expr RBRACE + | STATIC t=UPPERIDENT id=LOWERIDENT LPAR p=params RPAR LBRACE e=exprs RBRACE { Method(true, t, id, p, e) } - | STATIC t=TYPE id=LOWERIDENT LPAR RPAR LBRACE e=expr RBRACE + | STATIC t=UPPERIDENT id=LOWERIDENT LPAR RPAR LBRACE e=exprs RBRACE { Method(true, t, id, [], e) } - | t=TYPE id=LOWERIDENT LPAR p=params RPAR LBRACE e=expr RBRACE + | t=UPPERIDENT id=LOWERIDENT LPAR p=params RPAR LBRACE e=exprs RBRACE { Method(false, t, id, p, e) } - | t=TYPE id=LOWERIDENT LPAR RPAR LBRACE e=expr RBRACE + | t=UPPERIDENT id=LOWERIDENT LPAR RPAR LBRACE e=exprs RBRACE { Method(false, t, id, [], e) } +exprs: + | {[]} + | e = expr r=exprs {e::r} + param: - | t=TYPE id=LOWERIDENT + | t=UPPERIDENT id=LOWERIDENT { Param(t,id) } params: | { [] } - | t=TYPE id=LOWERIDENT + | t=UPPERIDENT id=LOWERIDENT { [Param(t,id)] } - | t=TYPE id=LOWERIDENT COMMA r=param+ + | t=UPPERIDENT id=LOWERIDENT COMMA r=param+ { Param(t,id) :: r} expr: + | comment+ e=expr + { e } + | e=expr comment+ + { e } | e1=expr SEMICOLON { Semi(e1)} | LPAR e=expr RPAR @@ -149,9 +154,10 @@ expr: { IfThen(e1, e2) } | IF LPAR e1=expr RPAR LBRACE e2=expr RBRACE ELSE LBRACE e3=expr RBRACE { IfThenElse(e1, e2, e3) } + | THIS { This } | e=expr DOT mthd=LOWERIDENT LPAR args_=args RPAR { Invoke(e, mthd, args_) } - | NEW t =TYPE + | NEW t =UPPERIDENT { New(t)} args: