-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.js
92 lines (85 loc) · 2.19 KB
/
grammar.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: 'asa',
rules: {
source_file: $ => repeat($.instruction),
instruction: $ => choice(
$.function_def,
$.goto,
$.label,
$.ifgoto,
$.import,
$.single_line_comment, // get ignored
$.raise,
$.show,
$.str,
$.print,
$.println,
$.push,
$.pop,
$.add,
$.sub,
$.mul,
$.div,
$.lshift,
$.rshift,
$.cmp,
$.halt,
$.increment,
$.decrement,
$.call,
$.gettype,
$.var_def
),
function_def: $ => seq(
'def', $.identifier, ':',
repeat($.instruction),
'end'
),
goto: $ => seq("goto", $.identifier, ";"),
label: $ => seq("label", $.identifier, ";"),
ifgoto: $ => seq("ifgoto", $.integer, $.identifier, ";"),
import: $ => seq("import", $.string, ";"),
raise: $ => seq("raise", ";"),
show: $ => seq("show", ";"),
str: $ => seq("str", ";"),
print: $ => seq("print", ";"),
println: $ => seq("println", ";"),
push: $ => seq("push", choice($.identifier, $.val), ";"),
pop: $ => seq("pop", $.identifier, ";"),
add: $ => seq("add", ";"),
sub: $ => seq("sub", ";"),
mul: $ => seq("mul", ";"),
div: $ => seq("div", ";"),
lshift: $ => seq("lshift", ";"),
rshift: $ => seq("rshift", ";"),
cmp: $ => seq("cmp", ";"),
halt: $ => seq("halt", ";"),
increment: $ => seq("incr", $.identifier, ";"),
decrement: $ => seq("decr", $.identifier, ";"),
call: $ => seq("call", $.identifier, ";"),
gettype: $ => seq("type", ";"),
var_def: $ => seq("var", $.identifier, $.val, ";"),
val: $ => choice(
$.integer,
$.float,
$.double,
$.bigdouble,
$.biginteger,
$.string,
$.char,
$.boolean
),
identifier: $ => /[a-zA-Z_]\w*(\/\w+)*\??/,
integer: $ => /\d+/,
float: $ => /\d+\.\d+f/,
double: $ => /\d+\.\d+/,
bigdouble: $ => /\d+\.\d+bd/,
biginteger: $ => /\d+bi/,
string: $ => /"[^"]*"/,
char: $ => /'[^']'/,
boolean: $ => choice('true', 'false'),
single_line_comment: $ => seq('//', /[^\n]*/)
}
});