-
Notifications
You must be signed in to change notification settings - Fork 33
/
t3xfasm.go
299 lines (253 loc) · 5.72 KB
/
t3xfasm.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"bufio"
"fmt"
"math"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/nokia/ntt/k3/t3xf"
"github.com/nokia/ntt/k3/t3xf/opcode"
"github.com/nokia/ntt/ttcn3/syntax"
"github.com/spf13/cobra"
)
var (
T3xfasmCommand = &cobra.Command{
Use: "t3xfasm <file>",
Short: "Assemble text file with t3xf instructions to T3XF binary file",
Long: `Assemble text file with t3xf instructions to T3XF binary file.
This commands implements a simple assembler to generate T3XF binary files.
Every line in the input file represents a single instruction. Empty lines or
lines with just a comment are generated as NOP instructions. Comments start
with a semicolon (';') and continue to the end of the line. The assembler is
case-insensitive. Availabl instructions can be found in the opcodes.yml file in
the github.com/nokia/ntt/k3/t3xf/opcode package.
Instructions optionally take an argument. References as start with '@'.
When referencing an TTCN-3 entity use the line-number in decimal.
Line instructions start with '='.
Example:
nop ; the next three instructions server as a header and BOM.
natlong 2
version
; var integer x := 2 + 3 (Note: above and this line will become a nop)
integer
name x
var
natlong 2
natlong 3
add
@8
assign
; log(x)
@8
log
goto @9
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
f, err := os.Open(args[0])
if err != nil {
return err
}
e := t3xf.NewEncoder()
s := bufio.NewScanner(f)
line := 0
for s.Scan() {
line++
ls := scanner{src: s.Bytes()}
op, err := ls.parseOpcode()
if err != nil {
return fmt.Errorf("%s:%d: %w", args[0], line, err)
}
arg, err := ls.parseArgument()
if err != nil {
return fmt.Errorf("%s:%d: %w", args[0], line, err)
}
ls.scanWhitespace()
if ls.pos < len(ls.src) && ls.src[ls.pos] != ';' {
return fmt.Errorf("%s:%d: %w: unexpected trailing characters", args[0], line, ErrSyntax)
}
if err := e.Encode(op, arg); err != nil {
return fmt.Errorf("%s:%d: %w", args[0], line, err)
}
}
if err := s.Err(); err != nil {
return err
}
b, err := e.Assemble()
if err != nil {
return err
}
out := args[0][:len(args[0])-len(filepath.Ext(args[0]))] + ".t3xf"
return os.WriteFile(out, b, 0644)
},
}
ErrSyntax = fmt.Errorf("syntax error")
)
type scanner struct {
src []byte
pos int
}
func (s *scanner) parseOpcode() (opcode.Opcode, error) {
s.scanWhitespace()
if s.pos >= len(s.src) {
return opcode.NOP, nil
}
pos := s.pos
s.pos++
ch := s.src[pos]
switch {
case isAlpha(ch):
s.scanAlnum()
word := strings.ToLower(string(s.src[pos:s.pos]))
return opcode.Parse(word)
case ch == ';':
s.pos = len(s.src)
return opcode.NOP, nil
case ch == '=':
return opcode.LINE, nil
case ch == '@':
// We need to backup the position to allow the argument parser
// to convert lines to zero-based references.
s.pos--
return opcode.REF, nil
default:
return -1, fmt.Errorf("%w: unexpected character %q", ErrSyntax, ch)
}
}
func (s *scanner) parseArgument() (interface{}, error) {
s.scanWhitespace()
if s.pos >= len(s.src) {
return nil, nil
}
pos := s.pos
s.pos++
ch := s.src[pos]
switch {
case isAlpha(ch):
s.scanAlnum()
return string(s.src[pos:s.pos]), nil
case ch == '"':
s.scanString()
v, err := syntax.Unquote(string(s.src[pos:s.pos]))
if err != nil {
return nil, err
}
return v, nil
case isDigit(ch) || ch == '-' || ch == '+':
s.scanFloat()
f, err := strconv.ParseFloat(string(s.src[pos:s.pos]), 64)
if err != nil {
return nil, err
}
if f == math.Trunc(f) {
return int(f), nil
}
return f, nil
case ch == '@':
s.scanFloat()
f, err := strconv.ParseFloat(string(s.src[pos+1:s.pos]), 64)
if err != nil {
return nil, err
}
if f != math.Trunc(f) {
return nil, fmt.Errorf("%w: integer expected", ErrSyntax)
}
return int(f) - 1, nil
case ch == '\'':
s.scanBitstring()
return t3xf.NewString(0, nil), fmt.Errorf("not implemented")
case ch == ';':
s.pos = len(s.src)
return nil, nil
default:
return -1, fmt.Errorf("%w: unexpected character %q", ErrSyntax, ch)
}
}
func (s *scanner) scanWhitespace() {
for s.pos < len(s.src) {
switch ch := s.src[s.pos]; ch {
case ' ', '\t', '\r':
default:
return
}
s.pos++
}
}
func (s *scanner) scanAlnum() {
for s.pos < len(s.src) && isAlnum(s.src[s.pos]) {
s.pos++
}
}
func (s *scanner) scanString() {
s.pos-- // backup for proper quoting ("")
for {
s.pos++
if s.pos >= len(s.src) {
return
}
switch ch := s.src[s.pos]; ch {
case '\\':
s.pos++
case '"':
s.pos++
if s.pos >= len(s.src) || s.src[s.pos] != '"' {
return
}
}
}
}
func (s *scanner) scanBitstring() {
L:
for {
if s.pos >= len(s.src) {
return
}
switch ch := s.src[s.pos]; ch {
case '\'':
s.pos++
break L
}
s.pos++
}
s.scanAlnum()
}
func (s *scanner) scanFloat() {
if s.src[s.pos-1] != '0' {
s.scanDigits()
}
// scan fractional part
if s.pos < len(s.src) && s.src[s.pos] == '.' {
// check '..' token
if s.pos+1 < len(s.src) && s.src[s.pos+1] == '.' {
return
}
s.pos++
s.scanDigits()
}
// scan exponent
if s.pos < len(s.src) && (s.src[s.pos] == 'e' || s.src[s.pos] == 'E') {
s.pos++
if s.pos < len(s.src) && (s.src[s.pos] == '+' || s.src[s.pos] == '-') {
s.pos++
}
if s.pos < len(s.src) && isDigit(s.src[s.pos]) {
s.scanDigits()
}
}
}
func (s *scanner) scanDigits() {
for s.pos < len(s.src) && isDigit(s.src[s.pos]) {
s.pos++
}
}
func isAlnum(ch byte) bool {
return isAlpha(ch) || isDigit(ch)
}
func isAlpha(ch byte) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
}
func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}