-
Notifications
You must be signed in to change notification settings - Fork 0
/
sina_parser.y
83 lines (69 loc) · 1.3 KB
/
sina_parser.y
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
%{
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include "sinavm.h"
#include "pprinter.h"
#define YYDEBUG 1
/* simple error handler - we don't need more at the moment */
void yyerror(const char *str)
{
fprintf(stderr, "error: %s\n", str);
}
int yywrap()
{
return 1;
}
/* gets set by parsing the program */
block_chunk* code;
%}
/* list of tokens for the sina grammar */
%token OPEN_PAREN CLOSE_PAREN OPEN_CURLY CLOSE_CURLY COLON
%union {
int number;
char* string;
void* pointer;
}
%token <number> INTEGER SYMBOL
%token <string> STRING
%type <pointer> program
%type <pointer> list_element list_elements list block escaped_symbol
%%
program:
list_elements
{
$$ = sinavm_new_block($1);
code = $$;
}
;
list_elements:
/* empty */ { $$ = sinavm_new_list(); }
| list_elements list_element
{
$$ = sinavm_push_back($1, $2);
}
;
list_element:
INTEGER { $$ = sinavm_new_int($1); }
| list { $$ = $1; }
| block { $$ = $1; }
| escaped_symbol { $$ = $1; }
| SYMBOL { $$ = sinavm_new_symbol($1); }
| STRING { $$ = sinavm_new_string($1); }
;
list: OPEN_PAREN list_elements CLOSE_PAREN
{
$$ = $2;
}
;
block: OPEN_CURLY list_elements CLOSE_CURLY
{
$$ = sinavm_new_block($2);
}
;
escaped_symbol: COLON SYMBOL
{
$$ = sinavm_new_escaped_symbol($2);
}
;
%%