This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
/
lexer.go
177 lines (151 loc) · 3.21 KB
/
lexer.go
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
// Copyright 2015 The PL0 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pl0
import (
"go/token"
"io"
"github.com/cznic/golex/lex"
"github.com/cznic/xc"
)
type lexer struct {
*lex.Lexer
code []Instruction
enter *Enter
example interface{}
exampleRule int
firstTok xc.Token
jmp *Jmp
lastPos token.Pos
nproc int
program *Program
report *xc.Report
scope *Bindings
}
func newLexer(nm string, sz int, r io.RuneReader, report *xc.Report) (*lexer, error) {
file := xc.FileSet.AddFile(nm, -1, sz+2)
lx, err := lex.New(
file,
r,
lex.ErrorFunc(func(pos token.Pos, msg string) {
report.Err(pos, msg)
}),
lex.RuneClass(func(r rune) int { return int(r) }),
)
if err != nil {
return nil, err
}
l := &lexer{
Lexer: lx,
report: report,
}
l.emit(&Call{Target: 2})
l.emit(&Halt{})
l.enter = &Enter{NVars: -1}
l.jmp = &Jmp{Target: -1}
l.emit(l.enter)
l.emit(l.jmp)
return l, nil
}
func (l *lexer) newScope() *Bindings {
l.scope = newBindings(l.scope)
return l.scope
}
func (l *lexer) popScope(t xc.Token) *Bindings {
if l.scope.Parent == nil {
l.report.ErrTok(t, "cannot pop scope")
return l.scope
}
l.scope = l.scope.Parent
return l.scope
}
// Implements yyLexer.
func (l *lexer) Lex(lval *yySymType) int {
r := rune(l.scan())
if r == lex.RuneEOF {
r = 0
}
pos := l.First.Pos()
l.lastPos = pos
c := lex.NewChar(pos, r)
var val int
switch r {
case IDENT, NUMBER:
val = xc.Dict.ID(l.TokenBytes(nil))
}
t := xc.Token{Char: c, Val: val}
if !l.firstTok.Pos().IsValid() {
l.firstTok = t
}
lval.Token = t
return int(r)
}
// Implements yyLexer.
func (l *lexer) Error(e string) { l.report.Err(l.lastPos, e) }
// Reduced implements yyLexerEx
func (l *lexer) Reduced(rule, state int, lval *yySymType) (stop bool) {
if n := l.exampleRule; n >= 0 && rule != n {
return false
}
switch x := lval.node.(type) {
case interface {
fragment() interface{}
}:
l.example = x.fragment()
default:
l.example = x
}
return true
}
func (l *lexer) mustIdent(t xc.Token) (Node, int) {
b, f := l.scope.lookup(t.Val)
if b == nil {
l.report.ErrTok(t, "undeclared identifier %s", t.S())
return nil, -1
}
return b.Node, f
}
func (l *lexer) mustVarOrConst(t xc.Token) (Node, int) {
n, f := l.mustIdent(t)
if n == nil {
return nil, -1
}
switch n.(type) {
case *Variable, *ConstSpec:
return n, f
default:
l.report.ErrTok(t, "%s is not a constant or a variable", t.S())
return nil, -1
}
}
func (l *lexer) mustVar(t xc.Token) (Node, int) {
n, f := l.mustIdent(t)
if n == nil {
return nil, -1
}
if _, ok := n.(*Variable); !ok {
l.report.ErrTok(t, "%s is not a variable", t.S())
return nil, -1
}
return n, f
}
func (l *lexer) mustProc(t xc.Token) (Node, int) {
n, f := l.mustIdent(t)
if n == nil {
return nil, -1
}
if _, ok := n.(*ProcSpec); !ok {
l.report.ErrTok(t, "%s is not a procedure", t.S())
return nil, -1
}
return n, f
}
func (l *lexer) bind(t xc.Token, n Node) {
l.scope.Bind(t, n, l.report)
}
func (l *lexer) emit(i Instruction) int {
addr := len(l.code)
i.setAddr(addr)
l.code = append(l.code, i)
return addr
}