-
Notifications
You must be signed in to change notification settings - Fork 14
/
calc.hspy
70 lines (58 loc) · 1017 Bytes
/
calc.hspy
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
/*
* calculator
*/
%token NUMBER
%left '+' '-'
%left '*' '/'
%%
start: lines;
lines: /* empty */
| lines line { yylrec++ }
;
line : expr '\n' { mes $1 }
| '\n' { mes "(empty line ignored)" }
| error '\n'
;
expr : expr '+' expr { $$ = $1 + $3 }
| expr '-' expr { $$ = $1 - $3 }
| expr '*' expr { $$ = $1 * $3 }
| expr '/' expr { $$ = $1 / $3 }
| '(' expr ')' { $$ = $2 }
| NUMBER { $$ = $1 }
;
%%
#runtime "hsp3cl"
yyparse
end stat
*yylex
if start == 0 {
input expr,, 1
if peek( expr ) == 0 : return 0
expr_ptr = 0
start = 1
}
char = peek( expr, expr_ptr )
if char == 10 || char == 13 {
start = 0
return 10
}
if char == ' ' || char == '\t' {
expr_ptr ++
goto *yylex
}
if char >= '0' && char <= '9' {
yylval = char - '0'
expr_ptr ++
repeat
char = peek( expr, expr_ptr )
if char >= '0' && char <= '9' : else : break
yylval = yylval * 10 + char - '0'
expr_ptr ++
loop
return NUMBER
}
expr_ptr ++
return char
*yyerror
mes yyerrmsg
return