Add "unnamed" (i.e. numbered) variables to syntax.

master
Rohan Jacob-Rao 10 years ago
parent 2dc796542a
commit 6dc20cd4e6

@ -0,0 +1,4 @@
; Definition of main function
define i32 @main() {
ret i32 %0
}

@ -9,9 +9,13 @@
(** Representation of LLVM constructs *) (** Representation of LLVM constructs *)
type variable_id =
| Name of string
| Number of int
type variable = type variable =
| Global of string | Global of variable_id
| Local of string | Local of variable_id
type constant = type constant =
| Cint of int | Cint of int
@ -42,4 +46,8 @@ type func_def = FuncDef of variable * typ option * (typ * string) list * instr l
type prog = Prog of func_def list type prog = Prog of func_def list
let string_of_variable : variable -> string = function let string_of_variable : variable -> string = function
| Global str | Local str -> str | Global var_id | Local var_id ->
begin match var_id with
| Name str -> str
| Number i -> string_of_int i
end

@ -20,6 +20,7 @@ let comment = ';' [^ '\n']*
let nonzero_digit = ['1'-'9'] let nonzero_digit = ['1'-'9']
let digit = ['0'-'9'] let digit = ['0'-'9']
let pos_int = nonzero_digit digit* let pos_int = nonzero_digit digit*
let nonneg_int = '0' | pos_int
let intlit = '-'? digit+ let intlit = '-'? digit+
let lower = ['a'-'z'] let lower = ['a'-'z']
@ -150,9 +151,11 @@ rule token = parse
(*| "va_arg" { VA_ARG }*) (*| "va_arg" { VA_ARG }*)
(*| "landingpad" { LANDINGPAD }*) (*| "landingpad" { LANDINGPAD }*)
(* identifiers *) (* IDENTIFIERS *)
| '@' (id as str) { GLOBAL str } | '@' (id as str) { NAMED_GLOBAL str }
| '%' (id as str) { LOCAL str } | '%' (id as str) { NAMED_LOCAL str }
| '@' (nonneg_int as i) { NUMBERED_GLOBAL (int_of_string i) }
| '%' (nonneg_int as i) { NUMBERED_LOCAL (int_of_string i) }
| id as str { IDENT str } | id as str { IDENT str }
| eof { EOF } | eof { EOF }

@ -125,8 +125,10 @@
(*%token VA_ARG*) (*%token VA_ARG*)
(*%token LANDINGPAD*) (*%token LANDINGPAD*)
%token <string> GLOBAL %token <string> NAMED_GLOBAL
%token <string> LOCAL %token <string> NAMED_LOCAL
%token <int> NUMBERED_GLOBAL
%token <int> NUMBERED_LOCAL
%token <string> IDENT %token <string> IDENT
%token EOF %token EOF
@ -241,8 +243,10 @@ operand:
| const = constant { Const const } | const = constant { Const const }
variable: variable:
| id=GLOBAL { Global id } | name = NAMED_GLOBAL { Global (Name name) }
| id=LOCAL { Local id } | name = NAMED_LOCAL { Local (Name name) }
| num = NUMBERED_GLOBAL { Global (Number num) }
| num = NUMBERED_LOCAL { Local (Number num) }
constant: constant:
| i = CONSTINT { Cint i } | i = CONSTINT { Cint i }

Loading…
Cancel
Save