-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
612 lines (535 loc) · 11.9 KB
/
main.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
)
// These type definitions make it possible to
// end every type with "boi"
type IntyBoi int
type FloatyBoi float64
type StringyBoi string
func boiError(boiInputs ...interface{}) {
fmt.Print("\033[31;1mBoi! ")
fmt.Print(boiInputs...)
fmt.Println(", boi\033[0m")
}
func main() {
boiArgs := os.Args[1:] // boi
var reader io.Reader
if len(boiArgs) < 1 {
boiInteractive()
} else if boiArgs[0] == "/slack" {
hostname := ""
if len(boiArgs) > 1 {
hostname = boiArgs[1]
}
boiSlackServer(hostname)
} else if boiArgs[0] == "-" {
reader = os.Stdout
} else {
//
boiFilename := boiArgs[0]
if boiFilename[len(boiFilename)-3:] != "boi" {
boiError(fmt.Errorf(
"boi %s: MUST end with 'boi'", boiFilename,
))
}
var err error
reader, err = os.Open(boiFilename)
if err != nil {
boiError(err)
}
}
err := boiBoi(reader) // boi
if err != nil {
boiError(err)
}
}
func boiBoi(reader io.Reader) error {
code, err := ioutil.ReadAll(reader)
if err != nil {
return err
}
lex := NewBoiInterpreter(code)
if err := lex.Run(); err != nil {
return err
}
return nil
}
type BoiVar struct {
data []byte
}
const (
BoiTokenValue = 1 // A string
BoiTokenVar = 2
BoiTokenBoi = 3 // End of statement
BoiTokenCall = 4
)
const (
// BoiStateStatement means we're expecting a statement
BoiStateStatement IntyBoi = 0 // boi
)
// Enumerated list of "source types"
const (
BoiSourceLocal = 1
BoiSourceReturn = 2
)
type Token struct {
BoiType IntyBoi
// For strings or variable names
BoiValue []byte
// Source context (for variables)
BoiSource int
Children []Token
}
type BoiFunc interface {
Do(args []BoiVar) error
}
type BoiContext struct {
functions map[string]BoiFunc
variables map[string]BoiVar
parentCtx *BoiContext
returnCtx *BoiContext
}
func (ctx *BoiContext) Call(fname string, args []BoiVar) error {
f, exists := ctx.functions[fname]
if !exists {
if ctx.parentCtx == nil {
return fmt.Errorf("call to undefined function %s", fname)
} else {
return ctx.parentCtx.Call(fname, args)
}
}
return f.Do(args)
}
func (ctx *BoiContext) Set(vname string, value BoiVar) error {
tryContext := ctx
_, exists := tryContext.variables[vname]
for !exists {
if tryContext.parentCtx == nil {
break
} else {
tryContext = tryContext.parentCtx
}
_, exists = tryContext.variables[vname]
}
if exists {
tryContext.variables[vname] = value
} else {
ctx.variables[vname] = value
}
return nil
}
func (ctx *BoiContext) Get(vname string) (BoiVar, bool) {
v, exists := ctx.variables[vname]
if !exists {
if ctx.parentCtx == nil {
// TODO: Raise error if boi.context is strict context
return BoiVar{}, false
} else {
return ctx.parentCtx.Get(vname)
}
}
return v, true
}
type BoiInterpreter struct {
input []byte
pos IntyBoi
state IntyBoi
rSyntaxToken *regexp.Regexp
rIsBoiVar *regexp.Regexp
rIsRetVar *regexp.Regexp
rIsBoi *regexp.Regexp
context *BoiContext
}
func NewBoiInterpreter(input []byte) *BoiInterpreter {
rootContext := &BoiContext{
map[string]BoiFunc{},
map[string]BoiVar{},
nil, nil,
}
boi := &BoiInterpreter{
input, 0, BoiStateStatement,
nil, nil, nil, nil,
rootContext,
}
boi.rIsBoiVar = regexp.MustCompile("^boi:[A-Za-z][A-Za-z0-9]*")
boi.rIsRetVar = regexp.MustCompile("^ret:[A-Za-z][A-Za-z0-9]*")
boi.rIsBoi = regexp.MustCompile("^boi[\\s\\n]")
boi.rSyntaxToken = regexp.MustCompile(
`^([A-Za-z]+[!,:\?]?|[\[\];]|--)`,
)
// Add internal functions
boi.RegisterGoFunction("say", BoiFuncSay)
boi.RegisterGoFunction("set", BoiFuncSet)
boi.RegisterGoFunction("icanhas", BoiFuncGet)
boi.RegisterGoFunction("nyan", BoiFuncCat)
boi.context.functions["int"] = BoiFuncInt{boi}
boi.context.functions["+"] = BoiFuncAdd{boi}
boi.context.functions["-"] = BoiFuncSub{boi}
boi.context.functions["/"] = BoiFuncDiv{boi}
boi.context.functions["*"] = BoiFuncMul{boi}
boi.context.functions["dec"] = BoiFuncDec{boi}
boi.RegisterGoFunction("<", BoiFuncLess)
// Grey area (memes, also practical)
boi.RegisterGoFunction("declare", BoiFuncDeclare)
// Memes
boi.RegisterGoFunction("IsEven", BoiFuncIsEven)
return boi
}
func (boi *BoiInterpreter) RegisterGoFunction(fname string, f BoiGoFunc) {
adapter := BoiGoFunctionAdapter{
BoiGoFuncAsFuncStruct{f}, boi,
}
boi.context.functions[fname] = adapter
}
func (boi *BoiInterpreter) RegisterGoFunctionStruct(
fname string, f BoiGoFuncStruct,
) {
adapter := BoiGoFunctionAdapter{
f, boi,
}
boi.context.functions[fname] = adapter
}
func (boi *BoiInterpreter) subContext() *BoiContext {
ctx := &BoiContext{
map[string]BoiFunc{},
map[string]BoiVar{},
boi.context, nil,
}
boi.context = ctx
return ctx
}
func (boi *BoiInterpreter) returnContext() error {
returnCtx := boi.context
boi.context = boi.context.parentCtx
if boi.context == nil {
return errors.New("returned to nil context")
}
boi.context.returnCtx = returnCtx
return nil
}
func (boi *BoiInterpreter) Run() error {
for {
if boi.whitespace() {
return nil
}
if err := boi.doStatement(); err != nil {
return err
}
}
return nil
}
func (boi *BoiInterpreter) whitespace() bool {
if !(boi.pos < IntyBoi(len(boi.input)-1)) {
return true // reached EOF
}
for ; boi.pos < IntyBoi(len(boi.input)); boi.pos++ {
//
if !(boi.input[boi.pos] == ' ' ||
boi.input[boi.pos] == '\n' ||
boi.input[boi.pos] == '\t') {
return false
}
}
return true
}
func (boi *BoiInterpreter) noeof(hasEof bool) error {
if hasEof {
return errors.New("unexpected EOF")
}
return nil
}
func (boi *BoiInterpreter) doStatement() error {
stmt, err := boi.getStatement()
if err != nil {
return err
}
if stmt == nil {
return nil
}
return boi.ExecStmt(stmt)
}
func (boi *BoiInterpreter) getStatement() (*BoiStatement, error) {
op := string(boi.rSyntaxToken.Find(boi.input[boi.pos:]))
for op == "--" {
boi.pos += 2
for {
boi.pos++
if boi.pos >= IntyBoi(len(boi.input)) {
break
}
if boi.input[boi.pos] == '\n' {
break
}
}
if boi.whitespace() {
return nil, nil
}
op = string(boi.rSyntaxToken.Find(boi.input[boi.pos:]))
}
switch op {
case "boi!":
boi.pos += 4
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}
return &BoiStatement{
BoiOpCall, tokens, nil,
}, nil
case "boi,":
boi.pos += 4
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}
return NewCallStatement("say", tokens), nil
case "boi:":
boi.pos += 4
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}
return NewCallStatement("set", tokens), nil
case "one":
fallthrough
case "ONE":
boi.pos += 4
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}
return NewCallStatement("declare", tokens), nil
case "boi?":
boi.pos += 4
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}
statements, err := boi.GetStatements()
if err != nil {
return nil, err
}
return &BoiStatement{
BoiOpIf, tokens, statements,
}, nil
case "bloop":
boi.pos += 5
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}
statements, err := boi.GetStatements()
if err != nil {
return nil, err
}
return &BoiStatement{
BoiOpLoop, tokens, statements,
}, nil
case "oh":
fallthrough
case "OH":
boi.pos += 2
boi.noeof(boi.whitespace())
tokens, err := boi.GetTokens()
if err != nil {
return nil, err
}
statements, err := boi.GetStatements()
if err != nil {
return nil, err
}
return &BoiStatement{
BoiOpFuncDef, tokens, statements,
}, nil
case "BOI":
boi.pos += 3
return nil, nil
default:
return nil, fmt.Errorf("unrecognized keyword '%s'", op)
}
}
func (boi *BoiInterpreter) GetTokens() ([]Token, error) {
tokens := []Token{}
for {
boi.noeof(boi.whitespace())
if token, err := boi.eatToken(); err == nil {
if token.BoiType != BoiTokenBoi {
tokens = append(tokens, token)
} else {
break
}
} else {
return tokens, err
}
}
return tokens, nil
}
func (boi *BoiInterpreter) GetStatements() ([]*BoiStatement, error) {
// Aggregate statements until we hit a nil statement ("BOI")
statements := []*BoiStatement{}
for {
if boi.whitespace() {
return nil, fmt.Errorf("end of file before BOI")
}
stmt, err := boi.getStatement()
if err != nil {
return nil, err
}
if stmt == nil {
break
}
statements = append(statements, stmt)
}
return statements, nil
}
func (boi *BoiInterpreter) Call(identifier string, args []BoiVar) error {
return boi.context.Call(identifier, args)
/*
if f, exists := boi.context.functions[identifier]; exists {
err := f.Do(args)
if err != nil {
return err
}
} else {
return fmt.Errorf("function %s: not found", identifier)
}
return nil
*/
}
func (boi *BoiInterpreter) getValueOf(tok Token) (BoiVar, bool) {
switch tok.BoiType {
case BoiTokenValue:
return BoiVar{tok.BoiValue}, true
case BoiTokenVar:
context := boi.context
if tok.BoiSource == BoiSourceReturn {
context = boi.context.returnCtx
}
if context == nil {
// TODO: Raise error if boi.context is strict context
return BoiVar{}, false
}
identifier := string(tok.BoiValue)
value, exists := context.Get(identifier)
if !exists {
// TODO: Raise error if strict context
}
return value, exists
case BoiTokenCall:
identifier := string(tok.Children[0].BoiValue)
args := []BoiVar{}
for _, tok := range tok.Children[1:] {
value, _ := boi.getValueOf(tok)
args = append(args, value)
}
// Call statement
err := boi.Call(identifier, args)
if err != nil {
return BoiVar{}, false // TODO: Raise error
}
output := boi.context.returnCtx.variables["exit"]
return BoiVar(output), true
}
return BoiVar{}, false
}
func (boi *BoiInterpreter) eatToken() (Token, error) {
if !(boi.pos < IntyBoi(len(boi.input))) {
return Token{}, errors.New("unexpected EOF")
}
keyword := string(boi.rSyntaxToken.Find(boi.input[boi.pos:]))
isBoi := keyword == "boi" || keyword == "]" || keyword == ";" ||
keyword == "BOI"
if isBoi {
boi.pos += IntyBoi(len(keyword))
t := Token{
BoiType: BoiTokenBoi,
BoiValue: []byte{},
}
return t, nil
}
token := Token{
BoiType: BoiTokenValue,
BoiValue: []byte{},
BoiSource: BoiSourceLocal,
}
isBoiVar := boi.rIsBoiVar.Match(boi.input[boi.pos:])
isRetVar := boi.rIsRetVar.Match(boi.input[boi.pos:])
if isBoiVar || isRetVar {
boi.pos += 4
token.BoiType = BoiTokenVar
}
if isRetVar {
token.BoiSource = BoiSourceReturn
}
if boi.input[boi.pos] == '[' || boi.input[boi.pos] == '!' {
boi.pos++
if boi.whitespace() {
return token, fmt.Errorf("end of file before BOI")
}
toks, err := boi.GetTokens()
if err != nil {
return token, err
}
token.BoiType = BoiTokenCall
token.Children = toks
return token, nil
}
if boi.input[boi.pos] == '"' {
boi.pos++ // otherwise we'll stop at the first quote
value := []byte{}
literal := false
for ; boi.pos < IntyBoi(len(boi.input)); boi.pos++ {
c := boi.input[boi.pos]
if literal {
value = append(value, c)
} else {
if c == '\\' {
literal = true
} else if c == '"' {
boi.pos++ // don't forget to go past this quote
break
} else {
value = append(value, c)
}
}
}
token.BoiValue = value
return token, nil
}
if true {
value := []byte{}
literal := false
for ; boi.pos < IntyBoi(len(boi.input)); boi.pos++ {
c := boi.input[boi.pos]
if literal {
value = append(value, c)
} else {
if c == '\\' {
literal = true
} else if c == ' ' {
boi.pos++ // don't forget to go past this space
break
} else if c == ']' || c == ';' {
break
} else {
value = append(value, c)
}
}
}
token.BoiValue = value
return token, nil
}
return Token{}, errors.New("unexpected token")
}