nand2tetris/compiler/backend/lib/lexer.mll
2024-09-20 14:20:09 -04:00

50 lines
1.3 KiB
OCaml

{
open Parser
}
let period = '.'
let colon = ':'
let underscore = '_'
let numbers = ['0'-'9']+
let alphabets = ['a'-'z' 'A'-'Z']+
let white = [' ' '\t']+
let comment = "//" _*
let address = (numbers)+
let id = (alphabets | numbers | colon | period | underscore)+
rule read =
parse
| white { read lexbuf }
| comment { read lexbuf }
| "add" { ADD }
| "sub" { SUB }
| "neg" { NEG }
| "eq" { EQ }
| "gt" { GT }
| "lt" { LT }
| "and" { AND }
| "or" { OR }
| "not" { NOT }
| "pop" { POP }
| "push" { PUSH }
| "argument" { ARGUMENT }
| "local" { LOCAL }
| "static" { STATIC }
| "this" { THIS }
| "that" { THAT }
| "pointer" { POINTER }
| "temp" { TEMP }
| "constant" { CONSTANT }
| "label" { LABEL }
| "goto" { GOTO }
| "if-goto" { IFGOTO }
| "function" { FUNCTION }
| "call" { CALL }
| "return" { RETURN }
| address as a { ADDRESS (int_of_string a) }
| id as i { ID (i) }
| eof { EOF }
| _ as c { failwith (Printf.sprintf "unexpected character: %C" c) }