-
Notifications
You must be signed in to change notification settings - Fork 0
/
Funfun.y
122 lines (107 loc) · 2.04 KB
/
Funfun.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
%{
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "ff.h"
#define YYDEBUG 1
void yyerror(char *s, ...);
extern int yylineno;
static int s_dbg_count = 0;
%}
%union {
char *str_val;
struct FfNode* node;
struct Stmts *stmt;
}
%token QUOTE ATOM EQ CAR CDR CONS COND
%token <str_val> ID
%type <node> op list elems
%type <stmt> stmts
%start progm
%%
progm
: stmts {
Interpreter *inter = ff_get_interpreter();
inter->progm = $1;
}
stmts
: list {
dbg_info("\nstmt created.\n");
$$ = ff_create_stmts($1);
}
| stmts list{
dbg_info("\nstmt linked.\n\n");
$$ = ff_link_stmts($1, $2);
}
;
list
:
'(' elems ')' {
$$ = ff_create_list_node($2);
}
|
'(' ')' {
$$ = ff_create_empty_node();
}
;
elems
: op {
dbg_info("\t[op:%s]~%d\n", $1, s_dbg_count++);
}
| ID {
dbg_info("\t[id:%s]~%d\n", $1, s_dbg_count++);
$$ = ff_create_id_node($1);
}
| list {
dbg_info("\t[list]~%d\n", s_dbg_count++);
}
| elems ID {
dbg_info("\t[elems id:%s]~%d\n",$2, s_dbg_count++);
FfNode *node = ff_create_id_node($2);
$$ = ff_link_node($1, node);
}
| elems list {
dbg_info("\t[elems list]~%d\n", s_dbg_count++);
$$ = ff_link_node($1, $2);
}
;
op
: QUOTE {
$$ = ff_create_op_node(op_quote);
dbg_info("quote ");
}
| ATOM {
$$ = ff_create_op_node(op_atom);
dbg_info("atom ");
}
| EQ {
$$ = ff_create_op_node(op_eq);
dbg_info("eq ");
}
| CAR {
$$ = ff_create_op_node(op_car);
dbg_info("car ");
}
| CDR {
$$ = ff_create_op_node(op_cdr);
dbg_info("cdr ");
}
| CONS {
$$ = ff_create_op_node(op_cons);
dbg_info("cons ");
}
| COND {
$$ = ff_create_op_node(op_cond);
dbg_info("cond ");
}
;
%%
void yyerror(char *s, ...)
{
va_list ap;
va_start(ap, s);
fprintf(stdout, "%d: error: ", yylineno);
vfprintf(stdout, s, ap);
fprintf(stdout, "\n");
}