summaryrefslogtreecommitdiff
path: root/src/texec/eval.go
blob: 510805e2e7d0687ae628da351229651dd761cd9f (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
/*
	Copyright 2020 Kyle Gunger

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

package texec

import (
	"tparse"
	"fmt"
	"strconv"
	"strings"
)

/*
	So here's what I care to support at present:
	Type checking, basic types, writing to stdout or a file
	Variable and state contexts
	Reading from files
	Raw structs
	Appending to arrays
	Calling functions and methods
	libtnsl stub

	This subset should theoretically be enough to write a compiler.
*/

var (
	// Program to run
	prog *TModule
	// Current artifact
	cart TArtifact

	//Default null value
	null = TVariable{tNull, nil}
)

//################
//# Helper Funcs #
//################

// Error helper

func errOut(msg string) {
	fmt.Println("==== BEGIN ERROR ====")
	fmt.Println(msg)
	fmt.Println(cart)
	fmt.Println("==== END ERROR ====")
	panic(">>> PANIC FROM EVAL <<<")
}

func errOutCTX(msg string, ctx *VarMap) {
	fmt.Println("==== BEGIN ERROR ====")
	fmt.Println(msg)
	fmt.Println(cart)
	fmt.Println(*ctx)
	fmt.Println("====  END  ERROR ====")
	panic(">>> PANIC FROM EVAL <<<")
}

func errOutNode(msg string, n tparse.Node) {
	fmt.Println("==== BEGIN ERROR ====")
	fmt.Println(msg)
	fmt.Println(cart)
	fmt.Printf("Line: %v Char: %v\n", n.Data.Line, n.Data.Char)
	fmt.Println("====  END  ERROR ====")
	panic(">>> PANIC FROM EVAL <<<")
}

// Names of artifacts, finding artifacts

func getDefNames(def tparse.Node) []string {
	out := []string{}
	for i := 0; i < len(def.Sub); i++ {
		if def.Sub[i].Data.Data == "vlist" && def.Sub[i].Data.Type == 10 {
			vl := def.Sub[i]
			for i := 0; i < len(vl.Sub); i++ {
				if vl.Sub[i].Data.Type == tparse.DEFWORD {
					out = append(out, vl.Sub[i].Data.Data)
				} else if vl.Sub[i].Data.Data == "=" && vl.Sub[i].Sub[0].Data.Type == tparse.DEFWORD {
					out = append(out, vl.Sub[i].Sub[0].Data.Data)
				}
			}
		}
	}
	return out
}

func getBlockName(block tparse.Node) []string {
	out := []string{}
	for i := 0; i < len(block.Sub[0].Sub); i++ {
		if block.Sub[0].Sub[i].Data.Type == tparse.DEFWORD {
			out = append(out, block.Sub[0].Sub[i].Data.Data)
		} else if block.Sub[0].Sub[i].Data.Data == "method" {
			out = append(out, block.Sub[0].Sub[i].Sub[0].Data.Data)
		} else if block.Sub[0].Sub[i].Data.Type == tparse.KEYWORD {
			switch block.Sub[0].Sub[i].Data.Data {
			case "if", "else", "loop", "match", "case", "default":
				out = append(out, block.Sub[0].Sub[i].Data.Data)
			default:
			}
		}
	}
	return out
}

func getTypeName(t tparse.Node) []string {
	out := []string{}
	for i := 0; i < len(t.Sub); i++ {
		if t.Sub[i].Data.Type == tparse.DEFWORD {
			out = append(out, t.Sub[i].Data.Data)
		}
	}
	return out
}

// Get the list of names defined by the block or variable definition
func getNames(root tparse.Node) []string {
	switch root.Data.Data {
	case "block":
		return getBlockName(root)
	case "define":
		return getDefNames(root)
	case "raw", "struct", "enum":
		return getTypeName(root)
	}

	return []string{}
}

// Attempt to get a module from a path starting at the given module
// Returns nil if the module was not found.
func getModuleRelative(mod *TModule, a TArtifact) *TModule {
	if mod == nil {
		return nil
	}

	for i := 0; i < len(a.Path); i++ {
		for j := 0; j < len(mod.Sub); j++ {
			if mod.Sub[j].Name == a.Path[i] {
				mod = &(mod.Sub[j])
				break
			}
			if j + 1 == len(mod.Sub) {
				return nil
			}
		}
	}

	return mod
}

// Attempt to get a module from the root module using a specified path
// Returns nil if the module was not found.
func getModule(a TArtifact) *TModule {
	return getModuleRelative(prog, a)
}

// Get a module ion the current path.
// Returns nil if the index is out of range.
func getModuleInPath(p int) *TModule {
	m := len(cart.Path)

	if p < 0 || p > m {
		return nil
	}

	return getModule( TArtifact{ cart.Path[:p] , "" } )
}

// Find an artifact from a name and the module to search
// Returns nil if the node is not found in the module
func getNode(mod *TModule, n string) *tparse.Node {

	for i := 0; i < len(mod.Artifacts); i++ {
		chk := getNames(mod.Artifacts[i])
		for j := 0; j < len(chk); j++ {
			if chk[j] == n {
				return &(mod.Artifacts[i])
			}
		}
	}

	return nil
}

func getDef(mod *TModule, n string) *TVariable {
	ret, prs := (*mod).Defs[n]
	if prs {
		return ret
	}
	return nil
}

// This is a horrible way to search through nodes with this structure.  O(n^3).
// This could (and should) be made better by using a dictionary like structure for sub-modules and artifacts.
// By sacrificing this type of tree it could probably get down to O(n^2) or even O(n) if you were good enough.
// Most probably, the following is not how it will be implemented in the final version of the compiler.
// If this wasn't a bootstrap/hoby project, I would probably fire myself for the following code.

// Yes, I am aware that the following code is bad.
// No, I don't care.

// The first variable it returns represents the block if one was found
// The second node represents the absolute path to the block
func searchNode(s TArtifact) (*tparse.Node, TArtifact) {

	// i-- because we are doing a reverse lookup
	for i := len(cart.Path); i >= 0; i-- { // O(n)
		tst := getModuleInPath(i) // O(n^2) (O(n^3) total here)
		tst = getModuleRelative(tst, s) // O(n^2) (O(n^3) total here)

		if tst == nil {
			continue
		}

		ret := getNode(tst, s.Name) // O(n^2) (O(n^3) total here)

		if ret != nil {
			pth := []string{}
			pth = append(pth, cart.Path[:i]...)
			pth = append(pth, s.Path...)
			return ret, TArtifact{ pth , s.Name }
		}
	} // Block total complexity 3*O(n^2) * O(n) = 3*O(n^3)

	return nil, tNull.T
}

func searchDef(s TArtifact) *TVariable {

	// i-- because of reverse lookup
	for i := len(cart.Path); i >= 0; i-- {
		tst := getModuleInPath(i)
		tst = getModuleRelative(tst, s)

		if tst == nil {
			continue
		}

		ret := getDef(tst, s.Name)

		if ret != nil {
			return ret
		}
	}
	return nil
}

// End block of complexity horror

// Type related stuff

// Checking type equality
// Assumes a is an unknown and b is also an unknown.
func equateTypePS(a, b TType, psa, psb int) bool {
	if len(a.T.Path) != len(b.T.Path) || len(a.Pre) - psa != len(b.Pre) - psb {
		return false
	}

	for i := 0; i < len(a.Pre) - psa; i++ {
		if a.Pre[psa + i] != b.Pre[psb + i] {
			return false
		}
	}

	for i := 0; i < len(a.T.Path);i++ {
		if a.T.Path[i] != b.T.Path[i] {
			return false
		}
	}

	if a.T.Name != b.T.Name || a.Post != b.Post {
		return false
	}

	return true
}

func equateTypePSB(a, b TType, ps int) bool {
	return equateTypePS(a, b, ps, ps)
}

// Checking type equality
// Assumes a is an unknown type and b is a known good type.
func equateTypePSO(a, b TType, ps int) bool {
	return equateTypePS(a, b, ps, 0)
}

func equateType(a, b TType) bool {
	return equateTypePS(a, b, 0, 0)
}

// Generate a TType from a 'type' node
func getType(t tparse.Node) TType {
	out := TType{}
	i := 0

	// Pre
	for ; i < len(t.Sub); i++ {
		if t.Sub[i].Data.Type == tparse.DEFWORD || t.Sub[i].Data.Type == tparse.KEYTYPE {
			break
		} else {
			out.Pre = append(out.Pre, t.Sub[i].Data.Data)
		}
	}

	// T
	for ; i < len(t.Sub); i++ {
		if t.Sub[i].Data.Type == tparse.KEYTYPE {
			out.T.Name = t.Sub[i].Data.Data
			i++
			break
		} else if t.Sub[i].Data.Type == tparse.DEFWORD {
			if i < len(t.Sub) - 1 {
				if t.Sub[i + 1].Data.Type == tparse.DEFWORD {
					out.T.Path = append(out.T.Path, t.Sub[i].Data.Data)
				} else {
					out.T.Name = t.Sub[i].Data.Data
					break
				}
			} else {
				out.T.Name = t.Sub[i].Data.Data
			}
		}
	}

	// Post
	if i < len(t.Sub) {
		if t.Sub[i].Data.Data == "`" {
			out.Post = "`"
		}
	}

	return out
}

func stripType(t TType, s int) TType {
	return TType{t.Pre[s:], t.T, t.Post}
}

func prependType(t TType, p string) TType {
	return TType{append(t.Pre, p), t.T, t.Post}
}

// Value generation

func getStringLiteral(v tparse.Node) []interface{} {
	str, err := strconv.Unquote(v.Data.Data)

	if err != nil {
		errOut(fmt.Sprintf("Failed to parse string literal %v", v.Data))
	}

	dat := []byte(str)
	out := []interface{}{}

	for i := 0; i < len(dat); i++ {
		out = append(out, dat[i])
	}

	return out
}

func getCharLiteral(v tparse.Node) byte {
	val, mb, _, err := strconv.UnquoteChar(v.Data.Data[1:], byte('\''))

	if err != nil || mb == true{
		errOut(fmt.Sprintf("Failed to parse character as single byte. %v", v.Data))
	}

	return byte(val)
}

func getIntLiteral(v tparse.Node) int {
	i, err := strconv.ParseInt(v.Data.Data, 0, 64)

	if err != nil {
		errOut(fmt.Sprintf("Failed to parse integer literal. %v", v.Data))
	}

	return int(i)
}

func getFloatLiteral(v tparse.Node) float64 {
	i, err := strconv.ParseFloat(v.Data.Data, 64)

	if err != nil {
		errOut(fmt.Sprintf("Failed to parse float literal. %v", v.Data))
	}

	return float64(i)
}

func getLiteralComposite(v tparse.Node) []interface{} {
	out := []interface{}{}

	for i := 0; i < len(v.Sub); i++ {
		if v.Sub[i].Data.Data[0] == '"' {
			out = append(out, getStringLiteral(v.Sub[i]))
		} else if v.Sub[i].Data.Data[0] == '\'' {
			out = append(out, getCharLiteral(v.Sub[i]))
		} else if v.Sub[i].Data.Data == "comp" {
			out = append(out, getLiteralComposite(v.Sub[i]))
		} else if v.Sub[i].Data.Data[0] == '0' {
			out = append(out, getIntLiteral(v.Sub[i]))
		} else {
			out = append(out, getFloatLiteral(v.Sub[i]))
		}
	}

	return out
}

func getBoolLiteral(v tparse.Node) bool {
	return v.Data.Data == "true"
}

func getLiteral(v tparse.Node, t TType) interface{} {
	if equateType(t, tFloat) {
		return getFloatLiteral(v)
	} else if equateType(t, tCharp) {
		return getCharLiteral(v)
	} else if equateType(t, tString) {
		return getStringLiteral(v)
	} else if equateType(t, tBool) {
		return getBoolLiteral(v)
	} else if equateType(t, tInt) {
		return getIntLiteral(v)
	}

	return getLiteralComposite(v)
}

func getLiteralType(v tparse.Node) TType {
	if v.Data.Data[0] == '"' {
		return tString
	} else if v.Data.Data[0] == '\'' {
		return tCharp
	} else if v.Data.Data == "comp" {
		return tStruct
	} else if v.Data.Data == "true" || v.Data.Data == "false" {
		return tBool
	} else if len(v.Data.Data) > 2 && v.Data.Data[0] == '0' && v.Data.Data[1] != '.' {
		return tInt
	} else {
		return tFloat
	}
}

// Convert Value to Struct from Array (cvsa)
// USE ONLY IN THE CASE OF tStruct!
func cvsa(sct TArtifact, dat []interface{}) VarMap {
	sv := searchDef(sct)
	
	old_c := cart
	cart = sct
	
	vars := sv.Data.([]TVariable)
	if len(vars) != len(dat) {
		return nil
	}

	out := make(VarMap)

	for i:=0;i<len(vars);i++ {
		tmp := TVariable{vars[i].Type, nil}
		if isStruct(vars[i].Type, 0) {
			tmp.Data = cvsa(vars[i].Type.T, dat[i].([]interface{}))
		} else {
			tmp.Data = dat[i]
		}
		out[vars[i].Data.(string)] = &tmp
	}

	cart = old_c

	return out
}

// Copy aray to aray (cata)
func cata(st TArtifact, dat []interface{}) []interface{} {
	out := []interface{}{}

	for i := 0; i < len(dat); i++ {
		switch v := dat[i].(type) {
		case []interface{}:
			out = append(out, cata(st, v))
		case VarMap:
			out = append(out, csts(st, v))
		default:
			out = append(out, convertValPS(TType{[]string{}, st, ""}, 0, v))
		}
	}

	return out
}

// Copy struct to struct
// Makes a deep copy of a struct.
func csts(st TArtifact, dat VarMap) VarMap {
	sv := searchDef(st)
	
	old_c := cart
	cart = st
	
	vars := sv.Data.([]TVariable)

	out := make(VarMap)

	for i := 0; i < len(vars); i++ {
		var dts interface{} = nil

		switch v := dat[vars[i].Data.(string)].Data.(type) {
		case []interface{}:
			dts = cata(vars[i].Type.T, v)
		case VarMap:
			dts = csts(vars[i].Type.T, v)
		default:
			dts = v
		}
		
		out[vars[i].Data.(string)] = &TVariable{vars[i].Type, dts}
	}

	cart = old_c

	return out
}

func convertValPS(to TType, sk int, dat interface{}) interface{} {
	if isPointer(to, sk) || equateTypePSO(to, tFile, sk) {
		return dat
	}

	var numcv float64
	switch v := dat.(type) {
	case []interface{}:
		if isArray(to, sk) {
			return cata(to.T, v)
		} else if isStruct(to, sk) {
			return cvsa(to.T, v)
		}
	case VarMap:
		return csts(to.T, v)
	case int:
		numcv = float64(v)
		goto NCV
	case byte:
		numcv = float64(v)
		goto NCV
	case float64:
		numcv = v
		goto NCV
	case bool:
		numcv = 0
		if v {
			numcv = 1
		}
		goto NCV
	}

	errOut(fmt.Sprintf("Unable to convert between two types.\nTO: %v\nSK: %d\nDT: %v", to, sk, dat))
	return nil

	NCV:
	if equateTypePSO(to, tInt, sk) {
		return int(numcv)
	} else if equateTypePSO(to, tFloat, sk) {
		return float64(numcv)
	} else if equateTypePSO(to, tByte, sk) || equateTypePSO(to, tCharp, sk) {
		return byte(numcv)
	} else if equateTypePSO(to, tBool, sk) {
		return numcv != 0
	}

	errOut(fmt.Sprintf("Unable to convert between two types.\nTO: %v\nSK: %d\nDT: %v", to, sk, dat))
	return nil
}

func convertVal(dat *TVariable, to TType) *TVariable {
	return &TVariable{to, convertValPS(to, 0, dat.Data)}
}

//#####################
//# Finding Artifacts #
//#####################

func resolveArtifactCall(a TArtifact, params []TVariable) TVariable {
	tres := tnslResolve(a)
	if tres == 0 {
		if len(params) > 0 {
			return tnslEval(params[0], a.Name)
		} else {
			errOut("Need at least one arg to call tnsl.io func")
		}
	}

	blk, pth := searchNode(a)

	if blk == nil {
		errOut(fmt.Sprintf("Invalid call to %v", a))
	}

	// Store and restore the path to the block
	ocrt := cart
	cart = pth
	out := evalBlock(*blk, params, false)
	cart = ocrt

	return out
}

func resolveStructCall(a TArtifact, method string, params []TVariable) TVariable {
	if len(a.Path) > 0 && a.Path[0] == "tnsl" {
		a.Path = append(a.Path, a.Name)
		a.Name = method
		tres := tnslResolve(a)

		if a.Name == "close" || a.Name == "read" {
			params = append(params, null)
		}

		if tres == 1 {
			if len(params) > 1 {
				return tnslFileEval(TVariable{tFile, *(params[0].Data.(*interface{}))}, params[1], a.Name)
			} else {
				errOut("Not enough args recieved to call tnsl.io.File method.")
			}
		} else {
			return null
		}
	}

	blk, pth := searchNode(a)

	if blk == nil {
		errOut(fmt.Sprintf("Could not find a method block for given type %v", a))
	}

	for i := 0; i < len(blk.Sub); i++ {
		if getBlockName(blk.Sub[i])[0] == method {
			ocrt := cart
			cart = pth
			out := evalBlock(blk.Sub[i], params, true)
			cart = ocrt
			return out
		}
	}

	errOut(fmt.Sprintf("Could not find method %s in type %v", method, a))
	return null
}

func resolveArtifact(a TArtifact, ctx *VarMap) *TVariable {
	val, prs := (*ctx)[a.Name]
	if !prs || len(a.Path) != 0 {
		// Try searching the modules for it
		val = searchDef(a)
	}
	return val
}

//#################
//# Runtime funcs #
//#################

// Value statement parsing

func isStruct(t TType, skp int) bool {
	ch := false

	ch = ch || isPointer(t, skp)
	ch = ch || isArray(t, skp)
	ch = ch || equateTypePSO(t, tFile, skp)
	ch = ch || equateTypePSO(t, tInt, skp)
	ch = ch || equateTypePSO(t, tByte, skp)
	ch = ch || equateTypePSO(t, tFloat, skp)
	ch = ch || equateTypePSO(t, tCharp, skp)
	ch = ch || equateTypePSO(t, tBool, skp)
	ch = ch || equateTypePSO(t, tNull, skp)

	return !ch
}

func isPointer(t TType, skp int) bool {

	if len(t.Pre) <= skp {
		return false
	}

	return t.Pre[skp] == "~"
}

func isArray(t TType, skp int) bool {
	if len(t.Pre) <= skp {
		return false
	}

	return t.Pre[skp] == "{}"
}

// Deals with call and index nodes
func evalCIN(v tparse.Node, ctx *VarMap, wk *TVariable) *TVariable {
	if v.Sub[0].Data.Data == "call" {
		if v.Data.Data == "append" && isArray(wk.Type, 0) {
			tmp := convertVal(evalValue(v.Sub[0].Sub[0], ctx), stripType(wk.Type, 1))
			i := len((*(wk.Data.(*interface{}))).([]interface{}))
			*(wk.Data.(*interface{})) = append((*(wk.Data.(*interface{}))).([]interface{}), tmp.Data)
			return &TVariable{stripType(wk.Type, 1), &((*(wk.Data.(*interface{}))).([]interface{})[i])}
		}

		args := []TVariable{}
		
		pth := TArtifact{[]string{}, v.Data.Data}
		if wk != nil {
			pth = wk.Type.T
			if wk.Data != nil {
				args = append(args, *wk)
			}
		}

		for i := 0; i < len(v.Sub[0].Sub); i++ {
			args = append(args, *evalValue(v.Sub[0].Sub[i], ctx))
		}

		var tmp TVariable

		if wk != nil && wk.Data != nil {
			tmp = resolveStructCall(pth, v.Data.Data, args)
		} else {
			tmp = resolveArtifactCall(pth, args)
		}
		
		wk = &TVariable{tmp.Type, &(tmp.Data)}

	} else {
		if wk == nil {
			tmp, prs := (*ctx)[v.Data.Data]
			if !prs {
				errOutNode("Unable to find variable", v)
			}
			wk = &TVariable{tmp.Type, &(tmp.Data)}
		} else {
			tmp, prs := (*(wk.Data.(*interface{}))).(VarMap)[v.Data.Data]
			if !prs {
				errOutNode("Unable to find struct variable (index)", v)
			}
			wk = &TVariable{tmp.Type, &(tmp.Data)}
		}
	}

	for i := 0; i < len(v.Sub); i++ {
		switch v.Sub[i].Data.Data {
		case "index":
			ind := convertVal(evalValue(v.Sub[i].Sub[0], ctx), tInt).Data.(int)
			wk.Data = &(((*(wk.Data.(*interface{}))).([]interface{}))[ind])
			wk.Type = stripType(wk.Type, 1)
		case "`":
			// De-reference
			wk.Data = (*(wk.Data.(*interface{})))
			wk.Type = stripType(wk.Type, 1)
		}
	}

	return wk
}

func evalDotChain(v tparse.Node, ctx *VarMap) *TVariable {
	out, prs := (*ctx)[v.Sub[0].Data.Data]
	wnd := &(v.Sub[0])

	if v.Sub[0].Data.Data == "self" && !prs {
		errOutNode("Use of 'self' keyword outside of method block.", v)
	} else if !prs {
		out = nil
	} else {
		if wnd.Data.Data != "self" {
			out = &TVariable{out.Type, &(out.Data)}
		}

		if len(v.Sub[0].Sub) > 0 {
			out = evalCIN(v.Sub[0], ctx, out)
		}

		v = v.Sub[1]
		if v.Data.Data == "." {
			wnd = &(v.Sub[0])
		} else {
			wnd = &v
		}
	}

	wrk := TArtifact{[]string{}, ""}

	for ;; {
		if out == nil {
			if wrk.Name != "" {
				wrk.Path = append(wrk.Path, wrk.Name)
			}
			wrk.Name = wnd.Data.Data

			tmp := searchDef(wrk)
			if tmp != nil {
				out = &TVariable{tmp.Type, &(tmp.Data)}
			}
		}

		if len(wnd.Sub) > 0 {
				if out == nil {
					out = evalCIN(*wnd, ctx, &TVariable{TType{[]string{}, wrk, ""}, nil})
				} else {
					if wnd.Sub[len(wnd.Sub) - 1].Data.Data == "++" || v.Sub[len(wnd.Sub) - 1].Data.Data == "--" {
						tctx := (*(out.Data.(*interface{}))).(VarMap)
						return setVal(v, &(tctx), nil)
					}
					out = evalCIN(*wnd, ctx, out)
				}
		} else if out != nil {
			tmp, prs := (*(out.Data.(*interface{}))).(VarMap)[wnd.Data.Data]
			if !prs {
				errOutNode("Unable to find struct variable (dot)", v)
			}
			out = &TVariable{tmp.Type, &(tmp.Data)}
		}

		if v.Data.Data == "." {
			v = v.Sub[1]
			if v.Data.Data == "." {
				wnd = &(v.Sub[0])
			} else {
				wnd = &v
			}
		} else {
			break
		}
	}

	return out
}

func setVal(v tparse.Node, ctx *VarMap, val *TVariable) *TVariable {
	var wrk *TVariable = nil

	if v.Data.Data == "." {
		wrk = evalDotChain(v, ctx)
		
		if wrk == nil {
			errOutNode("Unable to set a variable who's type is null. (Did you make a function call somewhere?)", v)
		}
		
		for ;v.Data.Data == "."; {
			v = v.Sub[1]
		}
	} else {
		tmp, prs := (*ctx)[v.Data.Data]

		if !prs {
			errOutCTX("Unable to set a variable due to the variable not existing.", ctx)
		}

		wrk = &TVariable{tmp.Type, &(tmp.Data)}

		if len(v.Sub) > 0 {
			wrk = evalCIN(v, ctx, nil)
		}
	}

	if len(v.Sub) > 0 {
		if v.Sub[len(v.Sub) - 1].Data.Data == "++" {
			val = &TVariable{tFloat, convertValPS(tFloat, 0, *(wrk.Data.(*interface{}))).(float64) + 1}
		} else if v.Sub[len(v.Sub) - 1].Data.Data == "--" {
			val = &TVariable{tFloat, convertValPS(tFloat, 0, *(wrk.Data.(*interface{}))).(float64) - 1}
		}
	}

	var set *interface{} = wrk.Data.(*interface{})

	(*set) = convertValPS((*wrk).Type, 0, val.Data)
	
	return wrk
}

// Parse a value node
func evalValue(v tparse.Node, ctx *VarMap) *TVariable {

	// STRUCT/ARRAY DEF
	if v.Data.Data == "comp" {
		out := []interface{}{}

		for i := 0; i < len(v.Sub); i++ {
			tmp := evalValue(v.Sub[i], ctx)
			out = append(out, (*tmp).Data)
		}

		return &TVariable{tStruct, out}
	}

	switch v.Data.Type {
	case tparse.LITERAL:
		if v.Data.Data == "self" {
			s, prs := (*ctx)["self"]
			if !prs {
				errOutNode("Use of 'self' keyword when not in a method.", v)
			}
			return &TVariable{s.Type, *(s.Data.(*interface{}))}
		}
		t := getLiteralType(v)
		return &TVariable{t, getLiteral(v, t)}
	case tparse.DEFWORD:
		if len(v.Sub) > 0 {
			if v.Sub[len(v.Sub) - 1].Data.Data == "++" || v.Sub[len(v.Sub) - 1].Data.Data == "--" {
				return setVal(v, ctx, nil)
			}
			ref := evalCIN(v, ctx, nil)
			if ref == nil {
				return &null
			}
			return &TVariable{ref.Type, *(ref.Data.(*interface{}))}
		}

		return resolveArtifact(TArtifact{[]string{}, v.Data.Data}, ctx)

	case tparse.AUGMENT:
		// Special cases
		switch v.Data.Data {
		case "=":
			return setVal(v.Sub[0], ctx, evalValue(v.Sub[1], ctx))
		case ".":
			ref := evalDotChain(v, ctx)
			if ref == nil {
				return &null
			}
			return &TVariable{ref.Type, *(ref.Data.(*interface{}))}
		}

		if len(v.Sub) == 1 {
			switch v.Data.Data {
			case "!":
				a := convertVal(evalValue(v.Sub[0], ctx), tBool)
				return &TVariable{tBool, !(a.Data.(bool))}
			case "len":
				a := evalValue(v.Sub[0], ctx)
				return &TVariable{tInt, len(a.Data.([]interface{}))}
			case "~":
				a := evalValue(v.Sub[0], ctx)
				typ := a.Type
				typ.Pre = append([]string{"~"}, typ.Pre...)
				return &TVariable{typ, &(a.Data)}
			case "-":
				a := convertVal(evalValue(v.Sub[0], ctx), tFloat)
				a.Data = -(a.Data.(float64))
				return a
			}
		}

		// General case setup
		
		a := convertVal(evalValue(v.Sub[0], ctx), tFloat)
		b := convertVal(evalValue(v.Sub[1], ctx), tFloat)
		var out TVariable
		out.Type = tFloat

		// General math and bool cases
		switch v.Data.Data {
		case "+":
			out.Data = a.Data.(float64) + b.Data.(float64)
		case "-":
			out.Data = a.Data.(float64) - b.Data.(float64)
		case "*":
			out.Data = a.Data.(float64) * b.Data.(float64)
		case "/":
			out.Data = a.Data.(float64) / b.Data.(float64)
		case "%":
			out.Type = tInt
			out.Data = int(a.Data.(float64)) % int(b.Data.(float64))
		case "&&":
			out.Type = tBool
			out.Data = a.Data.(float64) == 1 && b.Data.(float64) == 1
		case "||":
			out.Type = tBool
			out.Data = a.Data.(float64) == 1 || b.Data.(float64) == 1
		case "==":
			out.Type = tBool
			out.Data = a.Data == b.Data
		case "!==":
			out.Type = tBool
			out.Data = a.Data != b.Data
		case ">":
			out.Type = tBool
			out.Data = a.Data.(float64) > b.Data.(float64)
		case "<":
			out.Type = tBool
			out.Data = a.Data.(float64) < b.Data.(float64)
		case ">=":
			out.Type = tBool
			out.Data = a.Data.(float64) >= b.Data.(float64)
		case "<=":
			out.Type = tBool
			out.Data = a.Data.(float64) <= b.Data.(float64)
		}

		return &out
	}

	return &null
}

// Eval a definition
func evalDef(v tparse.Node, ctx *VarMap) {
	t := getType(v.Sub[0])
	
	for i := 0; i < len(v.Sub[1].Sub); i++ {
		if v.Sub[1].Sub[i].Data.Data == "=" {
			(*ctx)[v.Sub[1].Sub[i].Sub[0].Data.Data] = convertVal(evalValue(v.Sub[1].Sub[i].Sub[1], ctx), t)
		} else {
			(*ctx)[v.Sub[1].Sub[i].Data.Data] = &TVariable{t, nil}
		}
	}
}

// Eval a control flow
func evalCF(v tparse.Node, ctx *VarMap) (bool, TVariable, int) {

	loop := true
	ifout := true
	cond := tparse.Node{tparse.Token{tparse.LITERAL, "true", -1, -1}, []tparse.Node{}}
	var after *tparse.Node = nil

	if v.Sub[0].Data.Data == "bdef" {
		var before *tparse.Node = nil

		for i := 0; i < len(v.Sub[0].Sub); i++ {
			switch v.Sub[0].Sub[i].Data.Data {
			case "if", "else":
				loop = false
			case "()":
				before = &(v.Sub[0].Sub[i])
			case "[]":
				after = &(v.Sub[0].Sub[i])
			}
		}

		if before != nil {
			for i := 0; i < len(before.Sub); i++ {
				switch before.Sub[i].Data.Data {
				case "define":
					evalDef(before.Sub[i], ctx)
				case "value":
					val := *evalValue(before.Sub[i].Sub[0], ctx)
					if i == len(before.Sub) - 1 && equateType(val.Type, tBool) {
						cond = before.Sub[i].Sub[0]
						ifout = val.Data.(bool)
					}
				}
			}
		}
	}

	for ; (!loop && ifout) || evalValue(cond, ctx).Data.(bool) ; {
		for i := 0; i < len(v.Sub); i++ {
			switch v.Sub[i].Data.Data {
			case "define":
				evalDef(v.Sub[i], ctx)
			case "value":
				evalValue(v.Sub[i].Sub[0], ctx)
			case "block":
				ret, val, brk := evalCF(v.Sub[i], ctx)
				if ret {
					return ret, val, 0
				} else if brk < 0 {
					if brk < -1 {
						return false, null, brk + 1
					}
					goto CONCF
				} else if brk > 0 {
					return false, null, brk - 1
				} else if equateType(val.Type, tIF) {
					if val.Data.(bool) == true {
						i++

						for ;i < len(v.Sub) && getNames(v.Sub[i])[0] == "else"; i++ {}
						
						if i < len(v.Sub) {
							i--
						}
					}
				}
			case "return":
				if len(v.Sub[i].Sub) > 0 {
					return true, *evalValue(v.Sub[i].Sub[0], ctx), 0
				}
				return true, null, 0
			case "break":
				brk := 1
				if len(v.Sub[i].Sub) > 0 {
					brk = getIntLiteral(v.Sub[i].Sub[0])
				}
				if !loop {
					return false, null, brk
				}
				return false, null, brk - 1
			case "continue":
				cont := 1
				if len(v.Sub[i].Sub) > 0 {
					cont = getIntLiteral(v.Sub[i].Sub[0])
				}
				if !loop {
					return false, null, -cont
				} else if cont == 1 {
					goto CONCF
				}
				return false, null, 1 - cont
			}
		}

		CONCF:

		if after != nil {
			for i := 0; i < len(after.Sub); i++ {
				switch after.Sub[i].Data.Data {
				case "define":
					evalDef(after.Sub[i], ctx)
				case "value":
					val := *evalValue(after.Sub[i].Sub[0], ctx)
					if i == len(after.Sub) - 1 && equateType(val.Type, tBool) {
						cond = after.Sub[i].Sub[0]
					}
				}
			}
		}

		if !loop {
			break
		}
	}

	if !loop {
		return false, TVariable{tIF, ifout}, 0
	}

	return false, null, 0
}

func evalParams(pd tparse.Node, params *[]TVariable, ctx *VarMap, method bool) {
	if len(pd.Sub) == 0 {
		return
	}
	cvt := getType(pd.Sub[0])
	pi := 0
	
	if method {
		pi = 1
	}
	
	for i := 1; i < len(pd.Sub); i++ {
		if pd.Sub[i].Data.Type == 10 && pd.Sub[i].Data.Data == "type" {
			cvt = getType(pd.Sub[i])
		} else if pd.Sub[i].Data.Type == tparse.DEFWORD {
			(*ctx)[pd.Sub[i].Data.Data] = convertVal(&(*params)[pi], cvt)
			pi++
		}
	}
}

func evalBlock(b tparse.Node, params []TVariable, method bool) TVariable {
	ctx := make(VarMap)

	var rty TType = tNull

	if method {
		ctx["self"] = &(params[0])
	}

	if b.Sub[0].Data.Data == "bdef" {
		for i := 0; i < len(b.Sub[0].Sub); i++ {
			if b.Sub[0].Sub[i].Data.Data == "[]" {
				rty = getType(b.Sub[0].Sub[i])
			} else if b.Sub[0].Sub[i].Data.Data == "()" {
				evalParams(b.Sub[0].Sub[i], &params, &ctx, method)
			}
		}
	}

	for i := 0; i < len(b.Sub); i++ {
		switch b.Sub[i].Data.Data {
		case "define":
			evalDef(b.Sub[i], &ctx)
		case "value":
			evalValue(b.Sub[i].Sub[0], &ctx)
		case "block":
			ret, val, _ := evalCF(b.Sub[i], &ctx)
			if ret {
				return *convertVal(&val, rty)
			} else if equateType(val.Type, tIF) {
				if val.Data.(bool) == true {
					i++

					for ;i < len(b.Sub) && b.Sub[i].Data.Data == "block"; {
						if getNames(b.Sub[i])[0] == "else" {
							i++
						} else {
							break
						}
					}
					
					if i < len(b.Sub) {
						i--
					}
				}
			}
		case "return":
			return *convertVal(evalValue(b.Sub[i].Sub[0], &ctx), rty)
		}
	}

	return null
}

func EvalTNSL(root *TModule, args string) TVariable {
	prog = root
	cart = TArtifact { []string{}, "main" }

	sarg := strings.Split(args, " ")

	saif := []interface{}{}

	for i := 0; i < len(sarg); i++ {
		tmp := []interface{}{}
		dat := []byte(sarg[i])
		for j := 0; j < len(dat); j++ {
			tmp = append(tmp, dat[j])
		}

		saif = append(saif, tmp)
	}

	targ := TVariable {
		TType {
			[]string{"{}", "{}"},
			TArtifact { []string{}, "charp" },
			"" },
		saif }

	mainNode := getNode(prog, "main")

	//fmt.Println(mainNode)

	return evalBlock(*mainNode, []TVariable{targ}, false)
}