This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
throfflib.go
1114 lines (1005 loc) · 29.8 KB
/
throfflib.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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package throfflib
// Copyright Jeremy Price - Praeceptamachinae.com
//
//Released under the artistic license 2.0
// #cgo CFLAGS: -IC:/Users/user/Desktop/Dropbox/goProjects/src/github.com/thinxer/go-tcc
/*
#include <stdlib.h>
typedef float (*jitfunc)(float a, float b);
static float call(jitfunc f,float a, float b) {
return f(a, b);
}
*/
import "C"
//import "unsafe"
//import "github.com/thinxer/go-tcc"
import (
"fmt"
"io"
"io/ioutil"
"log"
"strconv"
"strings"
"github.com/abadojack/whatlanggo"
"github.com/chzyer/readline"
_ "github.com/mattn/go-sqlite3"
)
var PrintWarnings = false
var precision uint = 256
var interpreter_debug = false
var interpreter_trace = false
var traceProg = false
var debug = false
var USE_FUNCTION_CACHE = false
var seqID = 0 //Every instruction in the program gets a unique number, used by the optimiser and similar tasks
var BraceMode = "throff"
type StepFunc func(*Engine, *Thingy) *Engine
// The fundamental unit of data. In this engine, arrays, hashes, strings and numbers are all "Thingy"s
type Thingy struct {
annotations map[string]Thingy
_stub StepFunc //The native code function that will perform the next command in the program
tiipe string //The tiipe is the user-visible type. Tiipes are very flexible especially string<->token and code<->array
subType string //The interpreter visible type. Almost always "NATIVE" or "INTERPRETED"
userType *Thingy //A purely user defined type. Can be anything
_source string //A string that, in theory, could be eval-ed to recreate the data structure. Also used for if statement comparisons
environment *Thingy //Functions carry a reference to their parent environment, which allows us to create a new environment for the function to run in each time it is called
errorChain Stack //Error handlers are functions that are kept on a stack
//_parentEnvironment *Thingy
lock *Thingy
_structVal interface{}
_stringVal string
_arrayVal Stack
_engineVal *Engine
_hashVal map[string]*Thingy
_llVal *ll_t
_bytesVal []byte
_note string
arity int //The number of arguments this function pops off the stack, minus the number of return results`
arityIn int //The number of arguments this function pops off the stack`
arityOut int //The number of arguments this function pushes onto the stack (returned values)`
_intVal int //Currently used for booleans, will also be used to cache string->int conversions
_id int //Every token gets a unique id number, this allows us to do JIT on the code, among other tricks
_line int //Line number of instruction
_filename string //The source code file this instruction was written in
codeStackConsume int //When the functions are cached, we need to know how long they are, and skip the build phase by popping this many Thingys off the code stack
immutable bool
share_parent_environment bool //Instead of creating a new lexical pad (environment to store variables), use the surrounding lexical environment. This allows e.g. if statements to affect variables in the surrounding function
no_environment bool //AKA macro. Instead of using its own lexical environment, this function will use the lexical environment that it is INVOKED in. Required for doing complicated bind operations, probably a bad idea for anything else
}
type Stack []*Thingy
type Engine struct {
//previousEngine *Engine
environment *Thingy //The current lexical environment
dataStack Stack //The argument stack
dyn Stack //The current dynamic environment
codeStack Stack //The future of the program
lexStack Stack
_buildingFunc bool //do we run functions or just shift them to the data stack?
_funcLevel int
_prevLevel int
_functionCache map[int]*Thingy //Cache built functions here to avoid expensive rebuilds
_line int
_heatMap map[string][]int
_safeMode bool
LastFunction *Thingy
}
func (t *Thingy) setString(val string) {
t._stringVal = val
t._source = val
}
func (t *Thingy) setStub(val StepFunc) {
t._stub = val
t._source = "A function added by setStub"
t._note = "A function added by setStub"
}
func cloneMap(m map[string]*Thingy) map[string]*Thingy {
//fmt.Printf("Cloning map %v\n\n", m )
var nm = make(map[string]*Thingy, 1000)
for k, v := range m {
nm[k] = v
}
return nm
}
//Builds a string that, when EVALed, will recreate the data structure
//The string might not be an exact representation of the data, but should recreate it perfectly
func (t *Thingy) getSource() string {
if t.subType == "NATIVE" {
return t._source
}
if t.tiipe == "ARRAY" {
var accum string = "A[ "
for _, el := range t._arrayVal {
accum = fmt.Sprintf("%v STRING [ %v ]", accum, el._source)
}
return fmt.Sprintf("%v ]A", accum)
}
if t.tiipe == "CODE" || t.tiipe == "LAMBDA" {
var accum string = "[ "
for _, el := range t._arrayVal {
accum = fmt.Sprintf("%v %v", accum, el._source)
}
return fmt.Sprintf("%v ]", accum)
}
if t.tiipe == "HASH" {
var accum string = "H[ "
for k, v := range t._hashVal {
accum = fmt.Sprintf("%s %s %s", accum, k, v.getSource())
}
return fmt.Sprintf("%v ]H", accum)
}
// if t.tiipe == "STRING" {
// return fmt.Sprintf("STRING [ %v ]", t._stringVal)
// }
return t._source
}
//Builds a string for display to the user. Probably can't be used to re-create the original data structure
func (t *Thingy) GetString() string {
if t.tiipe == "ARRAY" {
var accum string = ""
for i, el := range t._arrayVal {
if i == 0 {
accum = fmt.Sprintf("%v", el.GetString())
} else {
accum = fmt.Sprintf("%v %v", accum, el.GetString())
}
}
return accum
}
if t.tiipe == "CODE" || t.tiipe == "LAMBDA" {
var accum string = ""
for i, el := range t._arrayVal {
if i == 0 {
accum = fmt.Sprintf("%v", el.GetString())
} else {
accum = fmt.Sprintf("%v %v", accum, el.GetString())
}
}
return accum
}
if t.tiipe == "HASH" {
var accum string = "{ "
for k, v := range t._hashVal {
accum = fmt.Sprintf("%s, %s -> %s", accum, k, v.GetString())
}
return fmt.Sprintf("%v }", accum)
}
if t.tiipe == "BOOLEAN" {
if t._intVal == 0 {
return "FALSE"
} else {
return "TRUE"
}
}
return t._stringVal
}
func actualClone(t Thingy) *Thingy {
return &t
}
func clone(t *Thingy) *Thingy {
//fmt.Printf("Cloning thingy %v\n\n", t )
return actualClone(*t)
}
//The engine is cloned at each step
func actualCloneEngine(t Engine) *Engine { return &t }
func cloneEngine(t *Engine, immutable bool) *Engine {
//fmt.Printf("Cloning engine %v\n\n", t )
//engineDump(t)
newt := actualCloneEngine(*t)
if immutable {
newt.environment = cloneEnv(t.environment)
} else {
newt.environment = t.environment
}
//newt.previousEngine=t //This is a memory leak
return newt
}
func cloneEnv(env *Thingy) *Thingy {
newEnv := clone(env)
return newEnv
}
func add(e *Engine, s string, t *Thingy) *Engine {
ne := cloneEngine(e, false)
t._note = s
t._source = s
ne.environment._llVal = ll_add(ne.environment._llVal, s, t)
//t.environment = ne.environment
t.share_parent_environment = true
t.no_environment = true
return ne
}
func Add(e *Engine, s string, t *Thingy) *Engine {
return add(e, s, t)
}
func getEnvironmentKeys(e *Engine) []string {
env := e.environment
h := map[string]*Thingy{}
ll_to_hash(env._llVal, h)
out := []string{}
for k, _ := range h {
out = append(out, k)
}
return out
}
func newThingy() *Thingy {
t := new(Thingy)
//The default action is to push the thing on to the data stack
t._stub = func(e *Engine, c *Thingy) *Engine {
ne := cloneEngine(e, false)
ne.dataStack = pushStack(ne.dataStack, c)
return ne
}
return t
}
//This is the native code called when the engine encounters a token. It looks up the token and replaces it with the correct value.
//It also builds functions when it spots the function brackets [ ]
//This function got out of hand, and should be broken up
func tokenStepper(e *Engine, c *Thingy) *Engine {
ne := cloneEngine(e, false) //FIXME e should already be a clone?
//Are we in function-building mode?
ne._prevLevel = ne._funcLevel
if e.isEndBrace(c.getSource()) {
ne._funcLevel -= 1
} //Finish a (possibly nested) function
if e.isStartBrace(c.getSource()) {
//fmt.Printf("Cached func: %v - %v, %v\n", c._id, ne._functionCache[c._id], c.getSource())
if USE_FUNCTION_CACHE && ne._functionCache[c._id] != nil {
f := ne._functionCache[c._id]
//fmt.Printf("Cached func: %v - %v\n", c._id, f.getSource())
//We have previously seen this function, and have it cached
//fmt.Printf("Found cached function, skipping build...\n")
//engineDump(ne)
f.environment = ne.environment
ne.dataStack = pushStack(ne.dataStack, f)
var i int
for i = 0; i <= f.codeStackConsume; i++ {
_, ne.codeStack = popStack(ne.codeStack)
_, ne.lexStack = popStack(ne.lexStack)
}
//fmt.Printf("Skip complete\n")
//engineDump(ne)
return ne
} else {
ne._funcLevel += 1
}
} //Start a (possibly nested) function
//fmt.Printf("TOKEN: in function level: %v\n", ne._funcLevel)
if ne._funcLevel < 0 {
emit(fmt.Sprintf("Unmatched [ or 」 at line %v\n", c._line))
engineDump(ne)
panic(fmt.Sprintf("Unmatched [ or 」at line %v\n", c._line))
} //Too many close functions, not enough opens
if ne._funcLevel == 0 { //Either we are not building a function, or we just finished
if c.getSource() == "BuildFuncFromStack" { //We move to phase 2, assembling the function from pieces on the data stack
//fmt.Printf("debug: %v\n", c._source)
ne._buildingFunc = false //Switch out of phase 1 build mode
ne._funcLevel += 1 //Start counting function brackets
ne.dataStack = pushStack(ne.dataStack, NewString(e.CurrentEndBrace(), e.environment))
return c._stub(ne, c)
} else {
val, ok := nameSpaceLookup(ne, c)
if ok {
if val.tiipe == "CODE" {
ne.codeStack = pushStack(ne.codeStack, val)
ne.lexStack = pushStack(ne.lexStack, e.environment)
} else {
ne.dataStack = pushStack(ne.dataStack, val)
}
} else {
var _, ok = strconv.ParseFloat(c.getSource(), 32) //Numbers don't need to be defined in the namespace
if ok != nil {
if PrintWarnings {
fmt.Printf("Warning: %v not defined at %v:%v\n", c.GetString(), c._filename, c._line)
}
}
ne.dataStack = pushStack(ne.dataStack, c)
}
}
} else {
ne.dataStack = pushStack(ne.dataStack, c)
}
//fmt.Printf("TokenStep: %v\n", c._source)
return ne
}
//Tokens cause a namespace lookup on their string value
//Whatever gets returned is pushed onto the code stack
//Then on the next step, that Thing gets activated, usually moving itself to the data stack, or running some code
//This can cause infinite loops if the token resolves to itself
//So we should probably detect that and move it to the data stack, since that is probably what the user wanted?
func NewToken(aString string, env *Thingy) *Thingy {
t := newThingy()
t.tiipe = "TOKEN"
t.subType = "NATIVE"
t.setString(aString)
t.environment = env
t._stub = tokenStepper
t.arity = 0
seqID = seqID + 1
t._id = seqID
return t
}
//Raw byte representated.
func NewBytes(bytes []byte, env *Thingy) *Thingy {
t := newThingy()
t.tiipe = "BYTES"
t.subType = "NATIVE"
t._bytesVal = bytes
t.environment = nil
t._stub = func(e *Engine, c *Thingy) *Engine {
ne := cloneEngine(e, false)
ne.dataStack = pushStack(ne.dataStack, c)
//fmt.Printf("StringStep: %v\n", c._source)
return ne
}
t.arity = -1
seqID = seqID + 1
t._id = seqID
return t
}
//Unicode strings. Length etc might not be the same as its byte representation
func NewString(aString string, env *Thingy) *Thingy {
t := newThingy()
t.tiipe = "STRING"
t.subType = "NATIVE"
t.setString(aString)
t.environment = nil
t._stub = func(e *Engine, c *Thingy) *Engine {
ne := cloneEngine(e, false)
ne.dataStack = pushStack(ne.dataStack, c)
//fmt.Printf("StringStep: %v\n", c._source)
return ne
}
t.arity = -1
seqID = seqID + 1
t._id = seqID
return t
}
//Stores a reference to the engine at the point where it was called. When activated, execution continues at that point
func NewContinuation(e *Engine) *Thingy {
t := newThingy()
t.tiipe = "CONTINUATION"
t.subType = "NATIVE"
t.setString("Continuation")
t._engineVal = e
seqID = seqID + 1
t._id = seqID
return t
}
//Holds any go structure, like a filehandle, network socket or database handle
func NewWrapper(s interface{}) *Thingy {
t := newThingy()
t._structVal = s
t.tiipe = "WRAPPER"
t.subType = "NATIVE"
t.setString("Native structure wrapper")
t.arity = -1
seqID = seqID + 1
t._id = seqID
return t
}
//Wraps a native go function
func NewCode(aName string, arity, arityIn, arityOut int, aFunc StepFunc) *Thingy {
t := newThingy()
t.tiipe = "CODE"
t.subType = "NATIVE"
t.setStub(aFunc)
t.setString(aName)
t.arity = arity
t.arityIn = arityIn
t.arityOut = arityOut
seqID = seqID + 1
t._id = seqID
return t
}
func NewArray(a Stack) *Thingy {
t := newThingy()
t.tiipe = "ARRAY"
t.subType = "INTERPRETED"
t.setString("Array - add code to fill this in properly")
t.arity = -1
t._arrayVal = a
seqID = seqID + 1
t._id = seqID
return t
}
func NewHash() *Thingy {
t := newThingy()
t.tiipe = "HASH"
t.subType = "NATIVE"
t.setString("hash - add code to fill this in properly")
t.arity = -1
t._hashVal = make(map[string]*Thingy, 1000)
return t
}
func NewBool(a int) *Thingy {
t := newThingy()
t.tiipe = "BOOLEAN"
t.subType = "NATIVE"
t.setString("BOOLEAN")
t._intVal = a
t.arity = -1
seqID = seqID + 1
t._id = seqID
return t
}
func NewEngine() *Engine {
e := new(Engine)
e.environment = NewHash()
e._functionCache = map[int]*Thingy{}
e._heatMap = map[string][]int{}
e._funcLevel = 0
e._safeMode = false
return e
}
func (e *Engine) isStartBrace(s string) bool {
//fmt.Printf("Comparing %v and %v\n", s, "{「{")
if BraceMode == "throff" {
return s == "]"
} else {
return strings.ContainsAny(s, "]【")
}
return false
}
func (e *Engine) isEndBrace(s string) bool {
if BraceMode == "throff" {
return s == "["
} else {
return strings.ContainsAny(s, "[】")
}
return false
}
func (e *Engine) CurrentEndBrace() string {
if BraceMode == "throff" {
return "["
} else {
return "】"
}
return "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
}
//Sometimes, we just don't want to do anything
func NullStep(e *Engine) *Engine {
return e
}
//Note, it works on immutable stacks, giving us the ability to save old engine states
func pushStack(s Stack, v *Thingy) Stack {
return append(s, v)
}
//Note, it works on immutable stacks, giving us the ability to save old engine states
func popStack(s Stack) (*Thingy, Stack) {
if len(s) > 0 {
v, sret := s[len(s)-1], s[:len(s)-1]
return v, sret
} else {
panic("Attempted read past end of stack!")
}
}
//neatly print out all the variables in scope
func dumpEnv(ll *ll_t) {
if ll != nil {
emit(fmt.Sprintf("===Env=== %p\n", ll))
dumpEnvRec(ll.cdr)
emit(fmt.Sprintf("===End Env=== %p\n", ll))
}
}
func dumpEnvRec(ll *ll_t) {
if ll != nil {
emit(ll.key + ": " + ll.val.getSource() + "\n")
dumpEnvRec(ll.cdr)
}
}
//The core of the interpreter. Each step advances the program by one command
func doStep(e *Engine) (*Engine, bool) {
if len(e.codeStack) > 0 { //If there are any instructions left
var v, lex *Thingy
var dyn Stack
ne := cloneEngine(e, false) //Clone the current engine state. The false means "do not clone the lexical environment" i.e. it
//will be common to this step and the previous step. Otherwise we would be running in fully
//immutable mode (to come)
v, ne.codeStack = popStack(ne.codeStack) //Pop an instruction off the instruction stack. Usually a token, but could be data or native code
lex, ne.lexStack = popStack(ne.lexStack)
dyn = lex.errorChain
//Move to own routine?
if false && v._line != 0 && len(v._filename) > 1 {
m := ne._heatMap[v._filename]
if m == nil {
m = make([]int, 1000000, 1000000)
}
m[v._line] = m[v._line] + 1
ne._heatMap[v._filename] = m
}
if interpreter_debug && v.environment != nil {
dumpEnv(v.environment._llVal)
}
if v.tiipe == "CODE" && v.no_environment == true { //Macros do not carry their own environment, they use the environment from the previous instruction
if interpreter_debug {
emit(fmt.Sprintf("Macro using invoked environment"))
}
lex = ne.environment //Set the macro environment to the current environment i.e. of the previous instruction, which will usually be the token's environment
} else {
ne._line = v._line
if lex != nil {
ne.environment = lex //Set the current environment to that carried by the current instruction
ne.dyn = dyn
}
}
//ne.environment = lex
if traceProg {
emit(fmt.Sprintf("%v:Step: %v(%v) - (%p) \n", v._line, v.GetString(), v.tiipe, lex))
}
//fmt.Printf("Calling: '%v'\n", v.getString())
if interpreter_debug {
emit(fmt.Sprintf("Choosing environment %p for command %v(%v)\n", ne.environment, v.GetString(), ne.environment))
}
oldlen := len(ne.dataStack) //Note the size of the data stack
if interpreter_debug {
emit(fmt.Sprintf("Using environment: %p for command : %v\n", ne.environment, v.GetString()))
}
ne = v._stub(ne, v) //Call the handler function for this instruction
newlen := len(ne.dataStack) //Note the new data stack size
if (v.arity > 9000) || (v.tiipe == "TOKEN") { //Tokens and some other instructions can have variable arity
return ne, true
} else {
if v.arity == (oldlen - newlen) { //Check the number of arguments to an instruction against the change in the stack size
return ne, true
} else {
if v.tiipe == "CODE" {
emit(fmt.Sprintln(fmt.Sprintf("Arity mismatch in native function! %v claimed %v, but actually took %v\n", v.getSource(), v.arity, (oldlen - newlen)))) //FIXME name not printing
}
return ne, true
}
}
} else {
//fmt.Printf("No code left to run!\n")
return e, false
}
}
func SlurpFile(fname string) string {
content, _ := ioutil.ReadFile(fname)
return string(content)
}
func tokenise(s string, filename string) Stack {
var line int = 0
s = strings.Replace(s, "\n", " LINEBREAKHERE ", -1)
s = strings.Replace(s, "\r", " ", -1)
s = strings.Replace(s, "\t", " ", -1)
stringBits := strings.Split(s, " ")
var tokens Stack
ts := []string{}
for _, v := range stringBits {
if len(v) > 0 && (v[0] == '\'' || v[0] == '^') {
v = v[1:]
ts = append(ts, "[")
ts = append(ts, v)
ts = append(ts, "]")
} else {
ts = append(ts, v)
}
}
for _, v := range ts {
seqID = seqID + 1
if len(v) > 0 {
if v == "LINEBREAKHERE" {
line = line + 1
} else {
t := NewToken(v, NewHash())
t._id = seqID
t._line = line
t._filename = filename
//fmt.Printf("Token id: %v\n", i)
tokens = pushStack(tokens, t)
}
}
}
return tokens
}
func reverseStringArray(ss []string) {
last := len(ss) - 1
for i := 0; i < len(ss)/2; i++ {
ss[i], ss[last-i] = ss[last-i], ss[i]
}
}
func StringsToTokens(stringBits []string) Stack {
var tokens Stack
if BraceMode != "throff" {
reverseStringArray(stringBits)
}
for i, v := range stringBits {
if len(v) > 0 {
t := NewToken(v, NewHash())
t._id = i
tokens = pushStack(tokens, t)
}
}
return tokens
}
func StringsToArray(stringBits []string) *Thingy {
var tokens Stack
if BraceMode != "throff" {
reverseStringArray(stringBits)
}
for i, v := range stringBits {
if len(v) > 0 {
t := NewString(v, NewHash())
t._id = i
tokens = pushStack(tokens, t)
}
}
return NewArray(tokens)
}
func engineDump(e *Engine) {
emit(fmt.Sprintf("Stack: %v, Code: %v, Environment: %v items\n", len(e.dataStack), len(e.codeStack), len(e.environment._hashVal)))
emit(fmt.Sprintf("---------------------------"))
emit(fmt.Sprintf("|| code: "))
stackDump(e.codeStack)
emit(fmt.Sprintf("\n"))
emit(fmt.Sprintf("|| data: "))
stackDump(e.dataStack)
emit(fmt.Sprintf("\n"))
emit(fmt.Sprintf("----------------------------"))
}
func run(e *Engine) (*Engine, bool) {
ok := true
for ok {
e, ok = doStep(e)
}
return e, ok
}
func (e *Engine) Run() *Engine {
e, _ = run(e)
return e
}
func (e *Engine) LoadTokens(s Stack) {
for _, elem := range s {
elem.environment = e.environment
e.lexStack = pushStack(e.lexStack, e.environment)
} //All tokens start off sharing the root environment
e.codeStack = append(e.codeStack, s...)
}
func (e *Engine) DataStackTop() *Thingy {
return e.dataStack[len(e.dataStack)-1]
}
// Function constructor - constructs new function for listing given directory
func listFiles(path string) func(string) []string {
return func(line string) []string {
names := make([]string, 0)
files, _ := ioutil.ReadDir(path)
for _, f := range files {
names = append(names, f.Name())
}
return names
}
}
var completer = readline.NewPrefixCompleter(
readline.PcItem("mode",
readline.PcItem("vi"),
readline.PcItem("emacs"),
),
readline.PcItem("login"),
readline.PcItem("say",
readline.PcItemDynamic(listFiles("./"),
readline.PcItem("with",
readline.PcItem("following"),
readline.PcItem("items"),
),
),
readline.PcItem("hello"),
readline.PcItem("bye"),
),
readline.PcItem("setprompt"),
readline.PcItem("setpassword"),
readline.PcItem("bye"),
readline.PcItem("help"),
readline.PcItem("go",
readline.PcItem("build", readline.PcItem("-o"), readline.PcItem("-v")),
readline.PcItem("install",
readline.PcItem("-v"),
readline.PcItem("-vv"),
readline.PcItem("-vvv"),
),
readline.PcItem("test"),
),
readline.PcItem("sleep"),
)
func Repl(e *Engine, japanese bool) *Engine {
prompt := ""
if japanese {
prompt = "\033[32m小日本語 » "
} else {
prompt = "\033[32mThroff » "
}
l, err := readline.NewEx(&readline.Config{
Prompt: prompt,
HistoryFile: "transcript.txt",
AutoComplete: completer,
InterruptPrompt: "^C",
EOFPrompt: "exit",
})
if err != nil {
panic(err)
}
defer l.Close()
log.SetOutput(l.Stderr())
return realRepl(e, l)
}
func DetectJapanese(text string) bool {
return strings.ContainsAny(text, " 【】「」わをそ") || whatlanggo.Jpn == whatlanggo.Detect(text).Lang
}
func realRepl(e *Engine, rl *readline.Instance) *Engine {
//engineDump(e)
emit(fmt.Sprintf("Ready> "))
//reader := bufio.NewReader(os.Stdin)
//text, _ := reader.ReadString('\n')
rl.Config.AutoComplete = readline.NewPrefixCompleter(readline.PcItemDynamic(
func(string) []string {
return getEnvironmentKeys(e)
},
))
line, err := rl.Readline()
rl.Write([]byte("\033[33m"))
emit("\n")
if err == readline.ErrInterrupt {
if len(line) == 0 {
return e
} else {
//continue
}
} else if err == io.EOF {
return e
}
text := line
var strs []string
if len(text) > 0 {
if DetectJapanese(text) {
//lang = "jp"
BraceMode = "forth"
strs = strings.Split(text, " ")
//log.Printf("Evaluating %v\n", strs)
//reverseStringArray(strs)
e.LoadTokens(StringsToTokens(strs))
//log.Println("Japanese detected")
} else {
BraceMode = "throff"
e.LoadTokens(tokenise(text, "repl"))
}
//stackDump(e.codeStack)
e, _ = run(e)
emit("\n\n")
//emit(fmt.Sprintln(e.dataStack[len(e.dataStack)-1].GetString()))
prettyStackDump(e.dataStack, BraceMode == "forth")
realRepl(e, rl)
return e
} else {
return e
}
}
func Shell(e *Engine, japanese bool) *Engine {
prompt := ""
prompt = "\033[32mShell » "
l, err := readline.NewEx(&readline.Config{
Prompt: prompt,
HistoryFile: "transcript.txt",
AutoComplete: completer,
InterruptPrompt: "^C",
EOFPrompt: "exit",
})
if err != nil {
panic(err)
}
defer l.Close()
log.SetOutput(l.Stderr())
return realShell(e, l)
}
func realShell(e *Engine, rl *readline.Instance) *Engine {
//engineDump(e)
emit(fmt.Sprintf("Ready> "))
//reader := bufio.NewReader(os.Stdin)
//text, _ := reader.ReadString('\n')
rl.Config.AutoComplete = readline.NewPrefixCompleter(readline.PcItemDynamic(
func(string) []string {
return getEnvironmentKeys(e)
},
))
line, err := rl.Readline()
rl.Write([]byte("\033[33m"))
emit("\n")
if err == readline.ErrInterrupt {
if len(line) == 0 {
return e
} else {
//continue
}
} else if err == io.EOF {
return e
}
if len(line) > 0 {
text := "CMDINTER A[ " + line + " ]A "
//fmt.Println(text)
BraceMode = "throff"
e.LoadTokens(tokenise(text, "repl"))
//stackDump(e.codeStack)
fmt.Println("runnng command", text)
e, _ = run(e)
//emit("\n\n")
fmt.Println("Dumping remaining stack")
//emit(fmt.Sprintln(e.dataStack[len(e.dataStack)-1].GetString()))
prettyStackDump(e.dataStack, BraceMode == "forth")
fmt.Println("looping")
realShell(e, rl)
return e
} else {
return e
}
}
var outputBuff string = ""
//All output to STDOUT should go through this function, so we can resend it to network ports, logfiles etc
func emit(s string) {
outputBuff = fmt.Sprintf("%s%s", outputBuff, s)
fmt.Printf("%s", s)
}
func clearOutput() string {
var temp = outputBuff
outputBuff = ""
return temp
}
func (e *Engine) RunString(s string, source string) *Engine {
e.LoadTokens(tokenise(s, source))
e, _ = run(e)
return e
}
func (e *Engine) CallArray(s string, args []string) ([]string, *Engine) {
e.LoadTokens(tokenise(s, "Call from go"))
tarray := []*Thingy{}
for _, v := range args {
tarray = append(tarray, tokenise(v, "Call args loader")...)
}
t := NewArray(tarray)
e.dataStack = append(e.dataStack, t)
e, _ = run(e)
var out []string
throffArr := e.DataStackTop()
for _, v := range throffArr._arrayVal {
out = append(out, v.GetString())
}
return out, e
}
func reverse(ss []string) {
last := len(ss) - 1
for i := 0; i < len(ss)/2; i++ {
ss[i], ss[last-i] = ss[last-i], ss[i]
}
}
func (e *Engine) CallArgs(s string, args ...string) (string, *Engine) {
e.LoadTokens(tokenise(s, "CallArgs from go"))
for _, v := range args {
t := NewToken(v, NewHash())
t._id = seqID
t._line = -1
t._filename = "CallArgs args loader"
e = PushData(e, t)
}
e, _ = run(e)
return "", e
}
func (e *Engine) CallArgs1(s string, args ...interface{}) (string, *Engine) {
e.LoadTokens(tokenise(s, "CallArgs from go"))
for _, v := range args {
str := fmt.Sprintf("%v", v)
e.LoadTokens(tokenise(str, "CallArgs args loader"))
}
e, _ = run(e)
return e.DataStackTop().GetString(), e
}
func (e *Engine) RunFile(s string) *Engine {
codeString := SlurpFile(s)
//println(codeString)
return e.RunString(codeString, s)
}
func stackDump(s Stack) {
emit(fmt.Sprintf("\nStack: "))
for i, _ := range s {
if i < 20 {
emit(fmt.Sprintf(":%v(%v):", s[len(s)-1-i].getSource(), s[len(s)-1-i].tiipe))
}
}