This repository has been archived on 2022-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
compilateurMIPS/parser.mly

56 lines
1.1 KiB
OCaml
Raw Normal View History

2022-12-06 20:39:15 +01:00
%{
open Ast
open Ast.Syntax
%}
%token <int> Lint
2022-12-08 21:30:39 +01:00
%token <bool> Lbool
2022-12-08 19:55:22 +01:00
%token <Ast.type_t> Ltype
%token <string> Lvar
2022-12-09 14:14:33 +01:00
%token Lend Lassign Lsc Lreturn
2022-12-09 16:12:32 +01:00
%token Ladd Lmul
2022-12-09 14:45:59 +01:00
2022-12-09 16:12:32 +01:00
%left Ladd Lmul
2022-12-06 20:39:15 +01:00
%start prog
2022-12-08 19:55:22 +01:00
%type <Ast.Syntax.block> prog
2022-12-06 20:39:15 +01:00
%%
prog:
2022-12-08 19:55:22 +01:00
| Lend { [] }
2022-12-09 14:45:59 +01:00
| i = instr ; Lsc ; b = prog { i @ b }
2022-12-06 20:39:15 +01:00
;
2022-12-08 19:55:22 +01:00
instr:
2022-12-09 14:45:59 +01:00
| Lreturn ; e = expr { [ Return { expr = e ; pos = $startpos } ] }
| t = Ltype ; v = Lvar {
[ Decl { name = v ; type_t = t ; pos = $startpos(t) } ]
2022-12-08 19:55:22 +01:00
}
2022-12-09 14:45:59 +01:00
| t = Ltype ; v = Lvar ; Lassign ; e = expr
{ [ Decl { name = v ; type_t = t ; pos = $startpos(t) }
; Assign { var = v ; expr = e ; pos = $startpos(v) } ]
2022-12-08 19:55:22 +01:00
}
2022-12-09 14:45:59 +01:00
| v = Lvar ; Lassign ; e = expr
{ [ Assign { var = v ; expr = e ; pos = $startpos($2) } ]
2022-12-08 19:55:22 +01:00
}
2022-12-06 20:39:15 +01:00
expr:
2022-12-08 19:55:22 +01:00
| n = Lint {
Val { value = Int (n) ; pos = $startpos(n) }
}
2022-12-08 21:30:39 +01:00
| b = Lbool {
Val { value = Bool (b) ; pos = $startpos(b) }
}
2022-12-08 19:55:22 +01:00
| v = Lvar {
Var { name = v ; pos = $startpos(v) }
}
2022-12-09 14:45:59 +01:00
| a = expr ; Ladd ; b = expr {
Call { func = "%add" ; args = [ a ; b ] ; pos = $startpos($2) }
}
2022-12-09 16:12:32 +01:00
| a = expr ; Lmul ; b = expr {
Call { func = "%mul" ; args = [ a ; b ] ; pos = $startpos($2) }
}
2022-12-06 20:39:15 +01:00
;