-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
table.v
1031 lines (947 loc) · 23.2 KB
/
table.v
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
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module compiler
import strings
struct Table {
pub mut:
typesmap map[string]Type
consts []Var
fns map[string]Fn
obf_ids map[string]int // obf_ids['myfunction'] == 23
modules []string // List of all modules registered by the application
imports []string // List of all imports
cflags []CFlag // ['-framework Cocoa', '-lglfw3']
fn_cnt int //atomic
obfuscate bool
varg_access []VargAccess
//names []Name
}
struct VargAccess {
fn_name string
tok_idx int
index int
}
enum NameCategory {
constant
mod
var
typ
}
struct Name {
cat NameCategory
idx int // e.g. typ := types[name.idx]
}
enum AccessMod {
private // private immutable
private_mut // private mutable
public // public immutable (readonly)
public_mut // public, but mutable only in this module
public_mut_mut // public and mutable both inside and outside (not recommended to use, that's why it's so verbose)
}
enum TypeCategory {
builtin
struct_
func // 2
interface_
enum_
union_ // 5
c_struct
c_typedef
objc_interface // 8 Objective C @interface
array
alias // `type myint int`
}
struct Var {
pub:
mut:
typ string
name string
idx int // index in the local_vars array
is_arg bool
is_const bool
args []Var // function args
attr string // [json] etc
is_mut bool
is_alloc bool
is_returned bool
ptr bool
ref bool
parent_fn string // Variables can only be defined in functions
mod string // module where this var is stored
access_mod AccessMod
is_global bool // __global (translated from C only)
is_used bool
is_changed bool
scope_level int
is_c bool // todo remove once `typ` is `Type`, not string
is_moved bool
line_nr int
token_idx int // this is a token index, which will be used by error reporting
is_for_var bool
is_public bool // for consts
}
struct Type {
pub:
mut:
mod string
name string
cat TypeCategory
is_public bool
fields []Var
methods []Fn
parent string
func Fn // For cat == FN (type myfn fn())
is_c bool // `C.FILE`
enum_vals []string
gen_types []string
default_vals []string // `struct Foo { bar int = 2 }`
// `is_placeholder` is used for types that are not defined yet but are known to exist.
// It allows having things like `fn (f Foo) bar()` before `Foo` is defined.
// This information is needed in the first pass.
is_placeholder bool
gen_str bool // needs `.str()` method generation
is_flag bool // enum bitfield flag
}
struct TypeNode {
mut:
next &TypeNode
typ Type
}
/*
// For debugging types
pub fn (t Type) str() string {
mut s := 'type "$t.name" {'
if t.fields.len > 0 {
// s += '\n $t.fields.len fields:\n'
for field in t.fields {
s += '\n $field.name $field.typ'
}
s += '\n'
}
if t.methods.len > 0 {
// s += '\n $t.methods.len methods:\n'
for method in t.methods {
s += '\n ${method.str()}'
}
s += '\n'
}
s += '}\n'
return s
}
*/
const (
c_reserved = [
'delete',
'exit',
'unix',
//'print',
// 'ok',
'error',
'malloc',
'calloc',
'free',
'panic',
// Full list of C reserved words, from: https://en.cppreference.com/w/c/keyword
'auto',
'char',
'default',
'do',
'double',
'extern',
'float',
'inline',
'int',
'long',
'register',
'restrict',
'short',
'signed',
'sizeof',
'static',
'switch',
'typedef',
'union',
'unsigned',
'void',
'volatile',
'while',
]
)
// This is used for debugging only
pub fn (f Fn) str() string {
t := Table{}
str_args := f.str_args(t)
return '${f.name}($str_args) $f.typ'
}
pub fn (t &Table) debug_fns() string {
mut s := strings.new_builder(1000)
for _, f in t.fns {
s.writeln(f.name)
}
return s.str()
}
// fn (types array_Type) print_to_file(f string) {
// }
const (
integer_types = ['int', 'i8', 'byte', 'i16', 'u16', 'u32', 'i64', 'u64']
float_types = ['f32', 'f64']
reserved_type_param_names = ['R', 'S', 'T', 'U', 'W']
)
fn is_number_type(typ string) bool {
return typ in integer_types || typ in float_types
}
fn is_integer_type(typ string) bool {
return typ in integer_types
}
fn is_float_type(typ string) bool {
return typ in float_types
}
fn is_primitive_type(typ string) bool {
return is_number_type(typ) || typ == 'string'
}
fn new_table(obfuscate bool) &Table {
mut t := &Table {
obfuscate: obfuscate
}
t.register_builtin('int')
t.register_builtin('size_t')
t.register_type_with_parent('i8', 'int')
t.register_type_with_parent('byte', 'int')
t.register_type_with_parent('char', 'int') // for C functions only, to avoid warnings
t.register_type_with_parent('i16', 'int')
t.register_type_with_parent('u16', 'u32')
t.register_type_with_parent('u32', 'int')
t.register_type_with_parent('i64', 'int')
t.register_type_with_parent('u64', 'u32')
t.register_builtin('byteptr')
t.register_builtin('intptr')
t.register_builtin('f32')
t.register_builtin('f64')
t.register_builtin('rune')
t.register_builtin('bool')
t.register_builtin('void')
t.register_builtin('voidptr')
t.register_builtin('charptr')
t.register_builtin('va_list')
for c in reserved_type_param_names {
t.register_builtin(c)
}
t.register_const('stdin', 'int', 'main', true)
t.register_const('stdout', 'int', 'main', true)
t.register_const('stderr', 'int', 'main', true)
t.register_const('errno', 'int', 'main', true)
t.register_type_with_parent('map_string', 'map')
t.register_type_with_parent('map_int', 'map')
return t
}
// If `name` is a reserved C keyword, returns `v_name` instead.
fn (t &Table) var_cgen_name(name string) string {
if name in c_reserved {
return 'v_$name'
}
else {
return name
}
}
fn (t mut Table) register_module(mod string) {
if mod in t.modules {
return
}
t.modules << mod
}
fn (p mut Parser) register_array(typ string) {
if typ.contains('*') {
println('bad arr $typ')
return
}
if !p.table.known_type(typ) {
p.register_type_with_parent(typ, 'array')
p.cgen.typedefs << 'typedef array $typ;'
}
}
fn (p mut Parser) register_map(typ string) {
if typ.contains('*') {
println('bad map $typ')
return
}
if !p.table.known_type(typ) {
p.register_type_with_parent(typ, 'map')
p.cgen.typedefs << 'typedef map $typ;'
}
}
fn (table &Table) known_mod(mod string) bool {
return mod in table.modules
}
fn (t mut Table) register_const(name, typ, mod string, is_pub bool) {
t.consts << Var{
name: name
typ: typ
is_const: true
mod: mod
idx: -1
is_public: is_pub
}
}
// Only for translated code
fn (p mut Parser) register_global(name, typ string) {
p.table.consts << Var {
name: name
typ: typ
is_const: true
is_global: true
mod: p.mod
is_mut: true
idx: -1
}
}
// Only for module functions, not methods.
// That's why searching by fn name works.
fn (t mut Table) register_fn(new_fn Fn) {
t.fns[new_fn.name] = new_fn
}
fn (table &Table) known_type(typ_ string) bool {
mut typ := typ_
// 'byte*' => look up 'byte', but don't mess up fns
if typ.ends_with('*') && !typ.contains(' ') {
typ = typ.replace('*', '')
}
t := table.typesmap[typ]
return t.name.len > 0 && !t.is_placeholder
}
fn (table &Table) known_type_fast(t &Type) bool {
return t.name != '' && !t.is_placeholder
}
fn (t &Table) find_fn(name string) ?Fn {
f := t.fns[name]
if f.name.str != 0 { // TODO
return f
}
return none
}
fn (t &Table) find_fn_is_script(name string, is_script bool) ?Fn {
mut f := t.fns[name]
if f.name.str != 0 { // TODO
return f
}
// V script? Try os module.
if is_script {
println('trying replace $name')
f = t.fns[name.replace('main__', 'os__')]
if f.name.str != 0 {
return f
}
}
return none
}
fn (t &Table) known_fn(name string) bool {
_ = t.find_fn(name) or { return false }
return true
}
fn (t &Table) known_const(name string) bool {
_ = t.find_const(name) or { return false }
return true
}
fn (t mut Table) register_builtin(typ string) {
if typ.len == 0 {
return
}
if typ in t.typesmap {
return
}
t.typesmap[typ] = Type{name:typ, is_public:true}
}
fn (p mut Parser) register_type_with_parent(strtyp, parent string) {
typ := Type {
name: strtyp
parent: parent
mod: p.mod
is_public: true
}
p.table.register_type(typ)
}
fn (t mut Table) register_type_with_parent(typ, parent string) {
if typ.len == 0 {
return
}
t.typesmap[typ] = Type {
name: typ
parent: parent
is_public: true
//mod: mod
}
}
fn (t mut Table) register_type(typ Type) {
if typ.name.len == 0 {
return
}
t.typesmap[typ.name] = typ
}
fn (t mut Table) rewrite_type(typ Type) {
if typ.name.len == 0 {
return
}
t.typesmap[typ.name] = typ
}
fn (table mut Table) add_field(type_name, field_name, field_type string, is_mut bool, attr string, access_mod AccessMod) {
if type_name == '' {
print_backtrace()
verror('add_field: empty type')
}
mut t := table.typesmap[type_name]
t.fields << Var {
name: field_name
typ: field_type
is_mut: is_mut
attr: attr
parent_fn: type_name // Name of the parent type
access_mod: access_mod
}
table.typesmap[type_name] = t
}
fn (table mut Table) add_default_val(idx int, type_name, val_expr string) {
mut t := table.typesmap[type_name]
if t.default_vals.len == 0 {
t.default_vals = [''].repeat(t.fields.len)
}
t.default_vals[idx] = val_expr
table.typesmap[type_name] = t
}
fn (t &Type) has_field(name string) bool {
_ = t.find_field(name) or { return false }
return true
}
fn (t &Type) has_enum_val(name string) bool {
return name in t.enum_vals
}
fn (t &Type) find_field(name string) ?Var {
for field in t.fields {
if field.name == name {
return field
}
}
return none
}
fn (table &Table) type_has_field(typ &Type, name string) bool {
_ = table.find_field(typ, name) or { return false }
return true
}
fn (table &Table) find_field(typ &Type, name string) ?Var {
for field in typ.fields {
if field.name == name {
return field
}
}
if typ.parent != '' {
parent := table.find_type(typ.parent)
for field in parent.fields {
if field.name == name {
return field
}
}
}
return none
}
fn (p mut Parser) add_method(type_name string, f Fn) {
if !p.first_pass() && f.name != 'str' {
return
}
if type_name == '' {
print_backtrace()
verror('add_method: empty type')
}
// TODO table.typesmap[type_name].methods << f
mut t := p.table.typesmap[type_name]
if f.name != 'str' && f in t.methods {
p.error('redefinition of method `${type_name}.$f.name`')
}
t.methods << f
p.table.typesmap[type_name] = t
}
fn (t &Type) has_method(name string) bool {
_ = t.find_method(name) or { return false }
return true
}
fn (table &Table) type_has_method(typ &Type, name string) bool {
_ = table.find_method(typ, name) or { return false }
return true
}
fn (table &Table) find_method(typ &Type, name string) ?Fn {
t := table.typesmap[typ.name]
for method in t.methods {
if method.name == name {
return method
}
}
if typ.parent != '' {
parent := table.find_type(typ.parent)
for method in parent.methods {
if method.name == name {
return method
}
}
return none
}
return none
}
fn (t &Type) find_method(name string) ?Fn {
// println('$t.name find_method($name) methods.len=$t.methods.len')
for method in t.methods {
// println('method=$method.name')
if method.name == name {
return method
}
}
return none
}
fn (table mut Table) add_gen_type(type_name, gen_type string) {
mut t := table.typesmap[type_name]
if gen_type in t.gen_types {
return
}
t.gen_types << gen_type
table.typesmap[type_name] = t
}
fn (p &Parser) find_type(name string) Type {
typ := p.table.find_type(name)
if typ.name == '' {
return p.table.find_type(p.prepend_mod(name))
}
return typ
}
fn (t &Table) find_type(name_ string) Type {
mut name := name_
if name.ends_with('*') && !name.contains(' ') {
name = name.replace('*', '')
}
if !(name in t.typesmap) {
//println('ret Type')
return Type{}
}
return t.typesmap[name]
}
fn (p mut Parser) check_types2(got_, expected_ string, throw bool) bool {
mut got := got_
mut expected := expected_
//p.log('check types got="$got" exp="$expected" ')
if p.pref.translated {
return true
}
if got == expected {
return true
}
// generic return type
if expected == '_ANYTYPE_' {
p.cur_fn.typ = got
return true
}
// variadic
if expected.starts_with('varg_') {
expected = expected[5..]
}
if got.starts_with('varg_') {
got = got[5..]
}
// Allow ints to be used as floats
if got == 'int' && expected == 'f32' {
return true
}
if got == 'int' && expected == 'f64' {
return true
}
if got == 'f64' && expected == 'f32' {
return true
}
if got == 'f32' && expected == 'f64' {
return true
}
// Allow ints to be used as longs
if got=='int' && expected=='i64' {
return true
}
if got == 'void*' && expected.starts_with('fn ') {
return true
}
if got.starts_with('[') && expected == 'byte*' {
return true
}
// Todo void* allows everything right now
if got=='void*' || expected=='void*' {// || got == 'cvoid' || expected == 'cvoid' {
return true
}
// TODO only allow numeric consts to be assigned to bytes, and
// throw an error if they are bigger than 255
if got=='int' && expected=='byte' {
return true
}
if got=='byteptr' && expected=='byte*' {
return true
}
if got=='byte*' && expected=='byteptr' {
return true
}
if got=='charptr' && expected=='char*' {
return true
}
if got=='char*' && expected=='charptr' {
return true
}
if got=='int' && expected=='byte*' {
return true
}
//if got=='int' && expected=='voidptr*' {
//return true
//}
// byteptr += int
if got=='int' && expected in ['byteptr', 'charptr'] {
return true
}
if got == 'Option' && expected.starts_with('Option_') {
return true
}
// lines := new_array
if got == 'array' && expected.starts_with('array_') {
return true
}
// Expected type "Option_os__File", got "os__File"
if expected.starts_with('Option_') && expected.ends_with(got) {
return true
}
// NsColor* return 0
if expected.ends_with('*') && got == 'int' {
return true
}
// if got == 'T' || got.contains('<T>') {
// return true
// }
// if expected == 'T' || expected.contains('<T>') {
// return true
// }
// TODO fn hack
if got.starts_with('fn ') && (expected.ends_with('fn') ||
expected.ends_with('Fn')) {
return true
}
// Allow pointer arithmetic
if expected=='void*' && got=='int' {
return true
}
//if p.fileis('_test') && is_number_type(got) && is_number_type(expected) {
//p.warn('got=$got exp=$expected $p.is_const_literal')
//}
// Allow `myu64 == 1`, `myfloat == 2` etc
if is_integer_type(got) && is_number_type(expected) && p.is_const_literal {
return true
}
if expected == 'integer' {
if is_integer_type(got) {
return true
} else {
p.error('expected type `$expected`, but got `$got`')
}
}
expected = expected.replace('*', '')
got = got.replace('*', '').replace('ptr','')
if got != expected {
// Interface check
if expected.ends_with('er') {
if p.satisfies_interface(expected, got, throw) {
return true
}
}
if !throw {
return false
}
else {
p.error('cannot convert `$got` to `$expected`')
}
}
return true
}
// throw by default
fn (p mut Parser) check_types(got, expected string) bool {
if p.first_pass() { return true }
return p.check_types2(got, expected, true)
}
fn (p mut Parser) check_types_no_throw(got, expected string) bool {
return p.check_types2(got, expected, false)
}
fn (p mut Parser) check_types_with_token_index(got, expected string, var_token_idx int) {
if !p.check_types2(got, expected, false) {
p.error_with_token_index('expected type `$expected`, but got `$got`', var_token_idx)
}
}
fn (p mut Parser) satisfies_interface(interface_name, _typ string, throw bool) bool {
int_typ := p.table.find_type(interface_name)
typ := p.table.find_type(_typ)
for method in int_typ.methods {
if !typ.has_method(method.name) {
// if throw {
p.error('type `$_typ` doesn\'t satisfy interface ' +
'`$interface_name` (method `$method.name` is not implemented)')
// }
return false
}
}
return true
}
fn (table &Table) is_interface(name string) bool {
if !(name in table.typesmap) {
return false
}
t := table.typesmap[name]
return t.cat == .interface_
}
// Do we have fn main()?
fn (t &Table) main_exists() bool {
for _, f in t.fns {
if f.name == 'main__main' {
return true
}
}
return false
}
fn (t &Table) has_at_least_one_test_fn() bool {
for _, f in t.fns {
if f.name.starts_with('main__test_') {
return true
}
}
return false
}
fn (t &Table) find_const(name string) ?Var {
//println('find const l=$t.consts.len')
for c in t.consts {
if c.name == name {
return c
}
}
return none
}
// ('s', 'string') => 'string s'
// ('nums', '[20]byte') => 'byte nums[20]'
// ('myfn', 'fn(int) string') => 'string (*myfn)(int)'
fn (table &Table) cgen_name_type_pair(name, typ string) string {
// Special case for [10]int
if typ.len > 0 && typ[0] == `[` {
tmp := typ.all_after(']')
size := typ.all_before(']')
return '$tmp $name $size ]'
}
// fn()
else if typ.starts_with('fn (') {
T := table.find_type(typ)
if T.name == '' {
eprintln('function type `$typ` not found')
exit(1)
}
str_args := T.func.str_args(table)
return '$T.func.typ (*$name)( $str_args /*FFF*/ )'
}
// TODO tm hack, do this for all C struct args
else if typ == 'tm' {
return 'struct /*TM*/ tm $name'
}
return '$typ $name'
}
fn is_valid_int_const(val, typ string) bool {
x := val.int()
return match typ {
'byte' { 0 <= x && x <= 255 }
'u16' { 0 <= x && x <= 65535 }
//case 'u32': return 0 <= x && x <= math.MaxU32
//case 'u64': return 0 <= x && x <= math.MaxU64
//////////////
'i8' { -128 <= x && x <= 127 }
/*
case 'i16': return math.min_i16 <= x && x <= math.max_i16
case 'int': return math.min_i32 <= x && x <= math.max_i32
*/
//case 'i64':
//x64 := val.i64()
//return i64(-(1<<63)) <= x64 && x64 <= i64((1<<63)-1)
else { true }
}
}
fn (p mut Parser) typ_to_fmt(typ string, level int) string {
t := p.table.find_type(typ)
if t.cat == .enum_ {
return '%d'
}
match typ {
'string' { return '%.*s'}
//case 'bool': return '%.*s'
'ustring' { return '%.*s'}
'byte', 'bool', 'int', 'char', 'byte', 'i16', 'i8' { return '%d'}
'u16', 'u32' { return '%u'}
'f64', 'f32' { return '%f'}
'i64' { return '%lld'}
'u64' { return '%llu'}
'byte*', 'byteptr' { return '%s'}
// case 'array_string': return '%s'
// case 'array_int': return '%s'
'void' { p.error('cannot interpolate this value')}
else {
if typ.ends_with('*') {
return '%p'
}
}
}
if t.parent != '' && level == 0 {
return p.typ_to_fmt(t.parent, level+1)
}
return ''
}
fn type_to_safe_str(typ string) string {
r := typ.replace(' ','').replace('(','_').replace(')','_')
return r
}
fn is_compile_time_const(s_ string) bool {
s := s_.trim_space()
if s == '' {
return false
}
if s.contains('\'') {
return true
}
for c in s {
if ! ((c >= `0` && c <= `9`) || c == `.`) {
return false
}
}
return true
}
fn (t &Type) contains_field_type(typ string) bool {
if !t.name[0].is_capital() {
return false
}
for field in t.fields {
if field.typ == typ {
return true
}
}
return false
}
// check for a function / variable / module typo in `name`
fn (p &Parser) identify_typo(name string) string {
// dont check if so short
if name.len < 2 { return '' }
name_dotted := mod_gen_name_rev(name.replace('__', '.'))
min_match := 0.50 // for dice coefficient between 0.0 - 1.0
mut output := ''
// check imported modules
mut n := p.table.find_misspelled_imported_mod(name_dotted, p, min_match)
if n.len > 0 {
output += '\n * module: `$n`'
}
// check consts
n = p.table.find_misspelled_const(name, p, min_match)
if n != '' {
output += '\n * const: `$n`'
}
// check types
typ, type_cat := p.table.find_misspelled_type(name, p, min_match)
if typ.len > 0 {
output += '\n * $type_cat: `$typ`'
}
// check functions
n = p.table.find_misspelled_fn(name, p, min_match)
if n.len > 0 {
output += '\n * function: `$n`'
}
// check function local variables
n = p.find_misspelled_local_var(name_dotted, min_match)
if n.len > 0 {
output += '\n * variable: `$n`'
}
return output
}
// compare just name part, some items are mod prefied
fn typo_compare_name_mod(a, b, b_mod string) f32 {
if a.len - b.len > 2 || b.len - a.len > 2 { return 0 }
auidx := a.index('__') or { return 0 } // TODO or {-1} once cgen lines bug is fixed //-1 }
buidx := b.index('__') or { return 0 } //-1 }
a_mod := if auidx != -1 { mod_gen_name_rev(a[..auidx]) } else { '' }
a_name := if auidx != -1 { a[auidx+2..] } else { a }
b_name := if buidx != -1 { b[buidx+2..] } else { b }
if a_mod.len > 0 && b_mod.len > 0 && a_mod != b_mod { return 0 }
return strings.dice_coefficient(a_name, b_name)
}
// find function with closest name to `name`
fn (table &Table) find_misspelled_fn(name string, p &Parser, min_match f32) string {
mut closest := f32(0)
mut closest_fn := ''
for _, f in table.fns {
if f.name.contains('__') && !p.is_mod_in_scope(f.mod) { continue }
c := typo_compare_name_mod(name, f.name, f.mod)
if c > closest {
closest = c
closest_fn = mod_gen_name_rev(f.name.replace('__', '.'))
}
}
return if closest >= min_match { closest_fn } else { '' }
}
// find imported module with closest name to `name`
fn (table &Table) find_misspelled_imported_mod(name string, p &Parser, min_match f32) string {
mut closest := f32(0)
mut closest_mod := ''
n1 := if name.starts_with('main.') { name[5..] } else { name }
for alias, mod in p.import_table.imports {
c := typo_compare_name_mod(n1, alias, '')
if c > closest {
closest = c
closest_mod = if alias == mod { alias } else { '$alias ($mod)' }
}
}
return if closest >= min_match { closest_mod } else { '' }
}
// find const with closest name to `name`
fn (table &Table) find_misspelled_const(name string, p &Parser, min_match f32) string {
mut closest := f32(0)
mut closest_const := ''
for cnst in table.consts {
if cnst.name.contains('__') && !p.is_mod_in_scope(cnst.mod) { continue }
c := typo_compare_name_mod(name, cnst.name, cnst.mod)
if c > closest {
closest = c
closest_const = mod_gen_name_rev(cnst.name.replace('__', '.'))
}
}
return if closest >= min_match { closest_const } else { '' }
}
// find type with closest name to `name`
fn (table &Table) find_misspelled_type(name string, p &Parser, min_match f32) (string, string) {
mut closest := f32(0)
mut closest_type := ''
mut type_cat := ''
for _, typ in table.typesmap {