-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoc.y
202 lines (185 loc) · 4.54 KB
/
hoc.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
%{
package main
import(
"fmt"
"bufio"
"os"
"log"
"io"
"strconv"
"regexp"
"unicode"
)
%}
%union{
inst *Inst
sym *Symbol
}
%type <inst> expr asgn stmt stmtlist cond while if end
%token <sym> NUMBER VAR BLTIN UNDEF PRINT WHILE IF ELSE
%right '='
%left OR
%left AND
%left GT GE LT LE EQ NE
%left '%'
%left '+' '-'
%left '*' '/'
%left UNARYMINUS NOT
%right '^'
%%
list: /* empty */
| list '\n'
| list asgn '\n' {Opr(Print); STOP.Code(); return 1;}
| list stmt '\n' {STOP.Code(); return 1;}
| list expr '\n' {Opr(Print); STOP.Code(); return 1;}
| list error '\n' {fmt.Println("error occurred")}
;
asgn: VAR '=' expr {$$ = $3; Opr(Varpush); Val($1); Opr(Assign)}
;
stmt: expr {Opr(Pop)}
| PRINT expr {Opr(PrExpr); $$=$2;}
| while cond stmt end {
WhileBodyCounter = len(Prog)
($3).Code()
WhileNextCounter = len(Prog)
if $4 == nil {
STOP.Code()
} else {
($4).Code()
}
}
| if cond stmt end {
($3).Code()
IfNextCounter = len(Prog)
if $4 == nil {
STOP.Code()
} else {
($4).Code()
}
}
| if cond stmt end else stmt end {
($3).Code()
($6).Code()
IfNextCounter = len(Prog)
if $7 == nil {
STOP.Code()
} else {
($7).Code()
}
}
| '{'stmtlist'}' {$$ = $2}
;
cond: '('expr')' {STOP.Code(); $$ = $2; IfBodyCounter = len(Prog)}
;
while: WHILE {$$=Opr(Whilecode); STOP.Code(); STOP.Code(); CondCounter = len(Prog);}
;
else: ELSE {IfElseCounter = len(Prog)}
;
if: IF {$$=Opr(Ifcode); STOP.Code(); STOP.Code(); STOP.Code(); CondCounter = len(Prog);}
;
end: /* empty */ {STOP.Code()} //$$ = len(Prog)
;
stmtlist: /* empty */ {} //{$$ = len(Prog)}
| stmtlist '\n'
| stmtlist stmt
;
expr: '('expr')' {$$ = $2}
| expr '%' expr {Opr(Mod)}
| expr '+' expr {Opr(Add)}
| expr '-' expr {Opr(Sub)}
| expr '*' expr {Opr(Mul)}
| expr '/' expr {Opr(Div)}
| expr '^' expr {Opr(Power)}
| NUMBER {$$=Opr(Constpush); Val($1)}
| '-' expr %prec UNARYMINUS {$$=$2; Opr(Negate)}
| VAR {$$=Opr(Varpush); Val($1); Opr(Eval)}
| asgn
| BLTIN '('expr')' {$$=$3; Opr(Bltin); Val($1)}
| expr GT expr {Opr(Gt)}
| expr GE expr {Opr(Ge)}
| expr LT expr {Opr(Lt)}
| expr LE expr {Opr(Le)}
| expr EQ expr {Opr(Eq)}
| expr NE expr {Opr(Ne)}
| expr AND expr {Opr(And)}
| expr OR expr {Opr(Or)}
| NOT expr {$$=$2; Opr(Not)}
;
%%
type Lexer struct {
s string
pos int
}
func (l *Lexer) Lex(lval *yySymType) int {
var c rune = ' '
for c == ' ' || c == '\t' {
if l.pos == len(l.s) {
return 0
}
c = rune(l.s[l.pos])
l.pos += 1
}
_, err := strconv.ParseFloat(string(c), 32)
if string(c) == "." || err == nil {
re := regexp.MustCompile("[0-9]+")
locations := re.FindStringIndex(l.s[l.pos-1:])
str := re.FindString(l.s[l.pos-1:])
l.pos += locations[1] - 1
f, _ := strconv.ParseFloat(str, 64)
s := &Symbol{Type: NUMBER, Val: f}
lval.sym = s
return NUMBER
}
if unicode.IsLetter(c) {
name := string(c)
for unicode.IsLetter(rune(l.s[l.pos])) && l.pos != len(l.s) {
name += string(l.s[l.pos])
l.pos += 1
}
s := Lookup(name)
if s == nil {
s = &Symbol{Name: name, Type: UNDEF, Val: 0.0}
}
lval.sym = s
if s.Type == UNDEF {
return VAR
}
return s.Type
}
switch string(c) {
case ">": return l.follow("=", GE, GT);
case "<": return l.follow("=", LE, LT);
case "=": return l.follow("=", EQ, int(c));
case "!": return l.follow("=", NE, NOT);
case "|": return l.follow("|", OR, int(c));
case "&": return l.follow("&", AND, int(c));
case "\n": return int(c);
default: return int(c);
}
return int(c)
}
func (l *Lexer) follow(expect string, ifyes, ifno int) int {
nextChar := rune(l.s[l.pos])
if string(nextChar) == expect {
l.pos += 1
return ifyes
}
return ifno
}
func (l *Lexer) Error(s string) {
fmt.Fprintf(os.Stderr, "%s: %s", "HOC", s)
}
func main() {
Init()
reader := bufio.NewReader(os.Stdin)
for {
str, err := reader.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
yyParse(&Lexer{s: str, pos: 0})
Execute()
}
}