-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.mll
executable file
·60 lines (57 loc) · 2.13 KB
/
lexer.mll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
open Lexing
open Parser
exception Syntax_error of string
let instructionPointer = ref 1;;
let syntax_error lexbuf = raise (Syntax_error ("Couldn't identify the token on line " ^ (string_of_int !instructionPointer) ^ " with token \"" ^ (Lexing.lexeme lexbuf) ^ "\""));;
}
let literal = '-'? ['0'-'9']+
let alpha = ['a'-'z' 'A'-'Z']
let str = alpha+
let register = alpha literal | alpha '[' alpha literal ']' | alpha '[' str ']'
let comment = ";"([^'\n']+)
rule lexer_main = parse
| ['\n' '\r'] { incr instructionPointer; EOL }
| [' '] { lexer_main lexbuf }
| ['\t'] { TAB }
| ['0' '1'] as b { BINARY (Char.escaped b) }
| literal as d { LITERAL (d) }
| register as r { REGISTER (r) }
| comment { lexer_main lexbuf }
| alpha as a { IDENTIFIER (Char.escaped a) }
| "," { COMMA }
| ":" { COLON }
| "ADD" { INSTR_ADD }
| "SUB" { INSTR_SUB }
| "MUL" { INSTR_MUL }
| "DIV" { INSTR_DIV }
| "TSTZ" { INSTR_TSTZ }
| "TSTE" { INSTR_TSTE }
| "TSTG" { INSTR_TSTG }
| "TSTGE" { INSTR_TSTGE }
| "TSTL" { INSTR_TSTL }
| "TSTLE" { INSTR_TSTLE }
| "TSTB" { INSTR_TSTB }
| "AND" { INSTR_AND }
| "OR" { INSTR_OR }
| "NOR" { INSTR_NOR }
| "XOR" { INSTR_XOR }
| "NAND" { INSTR_NAND }
| "COM" { INSTR_COM }
| "JMP" { INSTR_JMP }
| "CALL" { INSTR_CALL }
| "RET" { INSTR_RET }
| "MOV" { INSTR_MOV }
| "CLR" { INSTR_CLR }
| "BS" { INSTR_BS }
| "NXT" { INSTR_NXT }
| "INCR" { INSTR_INCR }
| "DECR" { INSTR_DECR }
| "stdin" as i { STDIN (i) }
| "stdout" as o { STDOUT (o) }
| "@END" as a { LABEL_END(a) }
| "@NEXT" as a { LABEL_NEXT(a) }
| "#DEF" { DEF }
| str as a { STRING (a) }
| _ { syntax_error lexbuf }
| eof { EOF }