summaryrefslogtreecommitdiff
path: root/parse.y
blob: 2f302fb8c95ac5fe464cd389db20cd17e0d5be53 (plain) (blame)
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
%{

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#include "basic.h"

extern int yylineno;
int yylex();
void yyerror(char *s, ...);

%}

%union {
    float f;
    int i;
    char *s;
    sexpr *u;
}

%token     NIL
%token <f> FLOAT
%token <i> INT
%token <s> ID
%token <s> STRING
%token     BRANCH

%type  <u> sexpr
%type  <u> lexpr

%%

cmds:
    | cmds sexpr { codegen($2); }

sexpr: '(' sexpr ';' sexpr ')' { $$ = new_node($2, $4); }
     | '(' sexpr     sexpr ')' { $$ = new_node($2, $3); }
     | '[' lexpr { $$ = $2; }
     | INT    { $$ = new_int($1); }
     | FLOAT  { $$ = new_float($1); }
     | ID     { $$ = new_id($1); }
     | NIL    { $$ = 0; }
     | STRING { $$ = new_str($1); }

lexpr: ']' { $$ = 0; }
     | sexpr ';' lexpr { $$ = new_node($1, $3); }
     | sexpr     lexpr { $$ = new_node($1, $2); }

%%

void yyerror(char *s, ...) {
    va_list ap;
    va_start(ap, s);

    fprintf(stderr, "%d: error: ", yylineno);
    vfprintf(stderr, s, ap);
    fprintf(stderr, "\n");
}