2024-09-13 20:37:20 +00:00
|
|
|
{
|
|
|
|
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 }
|
2024-09-20 18:20:09 +00:00
|
|
|
| comment { read lexbuf }
|
2024-09-13 20:37:20 +00:00
|
|
|
| "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) }
|