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

86 lines
1.6 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:39:44 +01:00
%token Ladd Lsub Lmul Ldiv
2022-12-09 14:45:59 +01:00
2022-12-09 22:14:18 +01:00
%left Ladd Lsub
%left Lmul Ldiv
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-09 22:14:18 +01:00
/* Fin de programme */
2022-12-08 19:55:22 +01:00
| Lend { [] }
2022-12-09 22:14:18 +01:00
/* instr; */
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 22:14:18 +01:00
/* return x */
2022-12-09 14:45:59 +01:00
| Lreturn ; e = expr { [ Return { expr = e ; pos = $startpos } ] }
2022-12-09 22:14:18 +01:00
/* type v */
2022-12-09 14:45:59 +01:00
| t = Ltype ; v = Lvar {
[ Decl { name = v ; type_t = t ; pos = $startpos(t) } ]
2022-12-08 19:55:22 +01:00
}
2022-12-09 22:14:18 +01:00
/* type v = e */
2022-12-09 14:45:59 +01:00
| t = Ltype ; v = Lvar ; Lassign ; e = expr
2022-12-09 22:33:08 +01:00
{ [ Decl { name = v ; type_t = t ; pos = $startpos(t) }
2022-12-09 14:45:59 +01:00
; Assign { var = v ; expr = e ; pos = $startpos(v) } ]
2022-12-08 19:55:22 +01:00
}
2022-12-09 22:14:18 +01:00
/* v = e */
2022-12-09 17:04:33 +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-09 22:14:18 +01:00
/* int */
2022-12-08 19:55:22 +01:00
| n = Lint {
Val { value = Int (n) ; pos = $startpos(n) }
}
2022-12-09 22:14:18 +01:00
/* bool */
2022-12-08 21:30:39 +01:00
| b = Lbool {
Val { value = Bool (b) ; pos = $startpos(b) }
}
2022-12-09 22:14:18 +01:00
/* Variable */
2022-12-08 19:55:22 +01:00
| v = Lvar {
Var { name = v ; pos = $startpos(v) }
}
2022-12-09 22:14:18 +01:00
/* e + e */
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 22:14:18 +01:00
/* e - e */
2022-12-09 16:39:44 +01:00
| a = expr ; Lsub ; b = expr {
Call { func = "%sub" ; args = [ a ; b ] ; pos = $startpos($2) }
}
2022-12-09 22:14:18 +01:00
/* e * e */
2022-12-09 16:12:32 +01:00
| a = expr ; Lmul ; b = expr {
Call { func = "%mul" ; args = [ a ; b ] ; pos = $startpos($2) }
}
2022-12-09 22:14:18 +01:00
/* e / e */
2022-12-09 16:39:44 +01:00
| a = expr ; Ldiv ; b = expr {
Call { func = "%div" ; args = [ a ; b ] ; pos = $startpos($2) }
}
2022-12-06 20:39:15 +01:00
;