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
2 changes: 1 addition & 1 deletion src/absyn.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type stm = CompoundStm of stm * stm
| PrintStm of exp list

and exp = IdExp of id
| NumExp of float
| NumExp of int
| OpExp of exp * binop * exp
| EseqExp of stm * exp
10 changes: 8 additions & 2 deletions src/test1.ml → src/examples.ml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
open Absyn

(*
altura := 1.73
altura := 173
*)

let prog1 = AssignStm ("altura", NumExp 1.73)
let prog1 = AssignStm ("altura", NumExp 173)


(*
print(43, 7, 0)
*)

let prog2 = PrintStm [NumExp 43; NumExp 7; NumExp 0]


(*
x := 2 + 3;
print(x)
Expand All @@ -19,6 +23,7 @@ let prog3 =
CompoundStm (AssignStm ("x", OpExp (NumExp 2, Plus, NumExp 3)),
PrintStm [IdExp "x"])


(*
x := 2 + 3 * 4;
print(x)
Expand All @@ -34,6 +39,7 @@ let prog4 =
NumExp 4))),
PrintStm [IdExp "x"])


(*
a := 5 + 3;
b := (print(a, a-1), 10*a);
Expand Down
14 changes: 7 additions & 7 deletions src/interpreter.ml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
open Absyn

type memory = (id, float) Hashtbl.t
type memory = (id, int) Hashtbl.t

let rec run m prog =
match prog with
| CompoundStm (s1, s2) -> run m s1; run m s2
| AssignStm (v, e) -> Hashtbl.replace m v (eval m e)
| PrintStm list -> List.iter
(fun e -> print_float (eval m e); print_char ' ')
(fun e -> print_int (eval m e); print_char ' ')
list;
print_newline ()
and eval m exp =
match exp with
| NumExp cte -> cte
| IdExp v -> ( try Hashtbl.find m v
with Not_found -> 0.0
with Not_found -> 0
)
| OpExp (e1, op, e2) -> let v1 = eval m e1 in
let v2 = eval m e2 in
( match op with
| Plus -> v1 +. v2
| Minus -> v1 -. v2
| Times -> v1 *. v2
| Div -> v1 /. v2
| Plus -> v1 + v2
| Minus -> v1 - v2
| Times -> v1 * v2
| Div -> v1 / v2
)
| EseqExp (s, e) -> run m s; eval m e

Expand Down
23 changes: 23 additions & 0 deletions src/maxargs.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
open Absyn

let rec maxargs statement =
match statement with
| CompoundStm (s1, s2) -> max (maxargs s1) (maxargs s2)
| AssignStm (_, e) -> maxargs_exp e
| PrintStm e_list -> max (maxargs_exp_list e_list) (List.length e_list)

and maxargs_exp expression =
match expression with
| IdExp _ -> 0
| NumExp _ -> 0
| OpExp (e1, _, e2) -> max (maxargs_exp e1) (maxargs_exp e2)
| EseqExp (s, e) -> max (maxargs s) (maxargs_exp e)

and maxargs_exp_list = function
| [] -> 0
| e :: rest -> max (maxargs_exp e) (maxargs_exp_list rest)


(*

(* Definição alternativa, usando List.fold_left *)

let rec maxargs statement =
match statement with
| PrintStm args -> List.fold_left
Expand All @@ -17,3 +39,4 @@ and maxargs_exp expression =
| EseqExp (s, e) -> max (maxargs s) (maxargs_exp e)


*)
82 changes: 82 additions & 0 deletions src/parser_.mly
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* parser.mly */

%{

module A = Absyn

(*
let pos n x = mkloc { loc_start = Parsing.rhs_start_pos n;
loc_end = Parsing.rhs_end_pos n;
}
x

let loc x = mkloc { loc_start = Parsing.symbol_start_pos ();
loc_end = Parsing.symbol_end_pos ();
}
x
*)

let loc x = x

let parse_error msg =
match ! Location.lexbuf_ref with
| Some lexbuf ->
Error.error (Location.curr_loc lexbuf) (Error.Syntax (Lexing.lexeme lexbuf))
| None ->
Error.internal "lexbuf_ref is unset"

%}

%token <float> NUM
%token <string> ID
%token PRINT
%token LPAREN RPAREN
%token COMMA SEMI
%token PLUS MINUS TIMES DIV
%token ASSIGN
%token EOF

%right SEMI
%nonassoc ASSIGN
%left PLUS MINUS
%left TIMES DIV

%start prog
%type <Absyn.stm> prog

%%

prog:
stm { $1 }
;

stm:
stm SEMI stm { loc (A.CompoundStm ($1, $3)) }
| ID ASSIGN exp { loc (A.AssignStm ($1, $3)) }
| PRINT LPAREN args RPAREN { loc (A.PrintStm $3) }
;

exp:
NUM { loc (A.NumExp $1) }
| ID { loc (A.IdExp $1) }
| exp PLUS exp { loc (A.OpExp ($1, A.Plus, $3)) }
| exp MINUS exp { loc (A.OpExp ($1, A.Minus, $3)) }
| exp TIMES exp { loc (A.OpExp ($1, A.Times, $3)) }
| exp DIV exp { loc (A.OpExp ($1, A.Div, $3)) }
| LPAREN stm COMMA exp RPAREN { loc (A.EseqExp ($2, $4)) }
;

args:
/* empty */ { [] }
| exp { $1 :: [] }
| exp COMMA args_rest { $1 :: $3 }
| error COMMA args_rest { $3 }
;

args_rest:
exp { $1 :: [] }
| exp COMMA args_rest { $1 :: $3 }
| error COMMA args_rest { $3 }
;

%%