support of identifier and integers literal

This commit is contained in:
Mylloon 2023-10-08 18:24:28 +02:00
parent e94cca2df9
commit 0240ac6d4c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 53 additions and 10 deletions

View file

@ -15,18 +15,35 @@
}
let newline = ('\010' | '\013' | "\013\010")
let blank = [' ' '\009' '\012']
let digit = ['0'-'9']
let hexa = "0x" ['0'-'9' 'a'-'f' 'A'-'F']
let bina = "0b" ['0'-'1']
let octa = "0o" ['0'-'7']
let integers = '-'? (digit+
| hexa+
| bina+
| octa+)
let ident = ['a'-'z']['A'-'Z' 'a'-'z' '0'-'9' '_']*
rule token = parse
(** Layout *)
| newline { next_line_and token lexbuf }
| blank+ { token lexbuf }
| eof { EOF }
| eof { EOF }
(** Lexing error. *)
(** Keywords *)
| "let" { LET }
(** Operators *)
| '=' { EQUAL }
(** Values *)
| integers as i { INT (Mint.of_string i) }
| ident as s { ID s }
(** Lexing error *)
| _ { error lexbuf "unexpected character." }

View file

@ -3,20 +3,46 @@
open HopixAST
open Position
%}
%token EOF
%token EOF LET EQUAL
%token<Mint.t> INT
%token<string> ID
%start<HopixAST.t> program
%%
program: EOF
{
[]
}
program:
| definition=located(definition)* EOF {
definition
}
definition:
| v=vdefinition {
DefineValue v
}
vdefinition: // manque le type ici, on met None en attendant
| LET i=located(identifier) EQUAL e=located(expression) {
SimpleValue(i, None, e)
}
expression:
| l=located(literal) {
Literal l
}
literal:
| i=INT {
LInt i
}
identifier:
| i=ID {
Id i
}
%inline located(X): x=X {
Position.with_poss $startpos $endpos x