2019-06-23 05:21:30 +03:00
|
|
|
// 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.
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
module main
|
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
import math
|
|
|
|
import strings
|
2019-07-10 17:05:39 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
struct Table {
|
|
|
|
mut:
|
2019-09-01 14:31:43 +03:00
|
|
|
typesmap map[string]Type
|
2019-08-05 10:49:52 +03:00
|
|
|
consts []Var
|
2019-08-17 22:19:37 +03:00
|
|
|
fns map[string]Fn
|
|
|
|
generic_fns []GenTable //map[string]GenTable // generic_fns['listen_and_serve'] == ['Blog', 'Forum']
|
2019-08-05 10:49:52 +03:00
|
|
|
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
|
2019-09-23 13:42:20 +03:00
|
|
|
file_imports map[string]FileImportTable // List of imports for file
|
2019-09-06 15:12:04 +03:00
|
|
|
cflags []CFlag // ['-framework Cocoa', '-lglfw3']
|
2019-08-24 14:35:05 +03:00
|
|
|
fn_cnt int //atomic
|
2019-08-05 10:49:52 +03:00
|
|
|
obfuscate bool
|
2019-09-30 13:46:50 +03:00
|
|
|
varg_access []VargAccess
|
2019-09-26 05:28:43 +03:00
|
|
|
//names []Name
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-30 13:46:50 +03:00
|
|
|
struct VargAccess {
|
|
|
|
fn_name string
|
|
|
|
tok_idx int
|
|
|
|
index int
|
|
|
|
}
|
2019-09-26 05:28:43 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
enum NameCategory {
|
|
|
|
constant
|
|
|
|
mod
|
|
|
|
var
|
|
|
|
typ
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Name {
|
|
|
|
cat NameCategory
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
struct GenTable {
|
2019-08-17 22:19:37 +03:00
|
|
|
fn_name string
|
2019-08-30 20:19:06 +03:00
|
|
|
mut:
|
2019-08-17 22:19:37 +03:00
|
|
|
types []string
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
|
2019-07-12 08:37:54 +03:00
|
|
|
// Holds import information scoped to the parsed file
|
|
|
|
struct FileImportTable {
|
|
|
|
mut:
|
2019-09-23 13:42:20 +03:00
|
|
|
module_name string
|
|
|
|
file_path string
|
|
|
|
imports map[string]string // alias => module
|
|
|
|
used_imports []string // alias
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
enum AccessMod {
|
2019-08-17 22:19:37 +03:00
|
|
|
private // private immutable
|
|
|
|
private_mut // private mutable
|
2019-07-29 19:21:36 +03:00
|
|
|
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)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-20 14:34:29 +03:00
|
|
|
enum TypeCategory {
|
2019-08-28 17:35:44 +03:00
|
|
|
builtin
|
2019-08-20 14:34:29 +03:00
|
|
|
struct_
|
2019-08-31 16:38:13 +03:00
|
|
|
func // 2
|
|
|
|
interface_
|
2019-08-20 14:34:29 +03:00
|
|
|
enum_
|
2019-08-31 16:38:13 +03:00
|
|
|
union_ // 5
|
|
|
|
c_struct
|
2019-08-20 14:34:29 +03:00
|
|
|
c_typedef
|
2019-09-21 21:38:12 +03:00
|
|
|
objc_interface // 8 Objective C @interface
|
2019-09-02 14:17:20 +03:00
|
|
|
array
|
2019-08-20 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
2019-08-29 01:52:32 +03:00
|
|
|
struct Var {
|
|
|
|
mut:
|
|
|
|
typ string
|
|
|
|
name string
|
2019-09-23 22:47:09 +03:00
|
|
|
idx int // index in the local_vars array
|
2019-08-29 01:52:32 +03:00
|
|
|
is_arg bool
|
|
|
|
is_const bool
|
|
|
|
args []Var // function args
|
|
|
|
attr string // [json] etc
|
|
|
|
is_mut bool
|
|
|
|
is_alloc bool
|
2019-09-09 03:29:24 +03:00
|
|
|
is_returned bool
|
2019-08-29 01:52:32 +03:00
|
|
|
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
|
2019-09-09 16:22:39 +03:00
|
|
|
is_c bool // todo remove once `typ` is `Type`, not string
|
2019-09-24 22:30:30 +03:00
|
|
|
is_moved bool
|
2019-09-20 13:42:37 +03:00
|
|
|
line_nr int
|
2019-09-29 20:37:39 +03:00
|
|
|
token_idx int // this is a token index, which will be used by error reporting
|
2019-08-29 01:52:32 +03:00
|
|
|
}
|
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
struct Type {
|
|
|
|
mut:
|
2019-07-07 23:30:15 +03:00
|
|
|
mod string
|
2019-06-22 21:20:28 +03:00
|
|
|
name string
|
2019-08-20 14:34:29 +03:00
|
|
|
cat TypeCategory
|
2019-06-22 21:20:28 +03:00
|
|
|
fields []Var
|
|
|
|
methods []Fn
|
|
|
|
parent string
|
2019-07-10 10:48:10 +03:00
|
|
|
func Fn // For cat == FN (type myfn fn())
|
2019-08-20 14:34:29 +03:00
|
|
|
is_c bool // `C.FILE`
|
2019-08-17 22:19:37 +03:00
|
|
|
enum_vals []string
|
|
|
|
gen_types []string
|
2019-06-22 21:20:28 +03:00
|
|
|
// This field 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
|
2019-08-17 22:19:37 +03:00
|
|
|
gen_str bool // needs `.str()` method generation
|
2019-09-02 14:17:20 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-29 01:52:32 +03:00
|
|
|
struct TypeNode {
|
|
|
|
mut:
|
|
|
|
next &TypeNode
|
|
|
|
typ Type
|
|
|
|
}
|
|
|
|
|
2019-09-29 00:21:10 +03:00
|
|
|
/*
|
2019-06-22 21:20:28 +03:00
|
|
|
// For debugging types
|
|
|
|
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
|
|
|
|
}
|
2019-09-29 00:21:10 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
CReserved = [
|
2019-09-14 23:48:30 +03:00
|
|
|
'delete',
|
2019-06-22 21:20:28 +03:00
|
|
|
'exit',
|
|
|
|
'unix',
|
2019-08-30 00:40:46 +03:00
|
|
|
//'print',
|
2019-06-22 21:20:28 +03:00
|
|
|
// 'ok',
|
|
|
|
'error',
|
|
|
|
'malloc',
|
|
|
|
'calloc',
|
|
|
|
'free',
|
|
|
|
'panic',
|
2019-08-26 20:04:57 +03:00
|
|
|
|
|
|
|
// 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',
|
2019-06-22 21:20:28 +03:00
|
|
|
]
|
2019-06-26 17:39:40 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
2019-09-18 15:06:34 +03:00
|
|
|
// This is used for debugging only
|
2019-06-22 21:20:28 +03:00
|
|
|
fn (f Fn) str() string {
|
|
|
|
t := Table{}
|
|
|
|
str_args := f.str_args(t)
|
|
|
|
return '$f.name($str_args) $f.typ'
|
|
|
|
}
|
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
fn (t &Table) debug_fns() string {
|
2019-08-17 22:19:37 +03:00
|
|
|
mut s := strings.new_builder(1000)
|
2019-07-29 19:21:36 +03:00
|
|
|
for _, f in t.fns {
|
2019-08-17 22:19:37 +03:00
|
|
|
s.writeln(f.name)
|
|
|
|
}
|
|
|
|
return s.str()
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
|
2019-06-22 21:20:28 +03:00
|
|
|
// fn (types array_Type) print_to_file(f string) {
|
|
|
|
// }
|
|
|
|
const (
|
2019-09-01 22:37:22 +03:00
|
|
|
number_types = ['number', 'int', 'i8', 'i16', 'u16', 'u32', 'byte', 'i64', 'u64', 'f32', 'f64']
|
2019-08-04 11:18:31 +03:00
|
|
|
float_types = ['f32', 'f64']
|
2019-06-22 21:20:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fn is_number_type(typ string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
return typ in number_types
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_float_type(typ string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
return typ in float_types
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 16:24:32 +03:00
|
|
|
fn is_primitive_type(typ string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
return is_number_type(typ) || typ == 'string'
|
|
|
|
}
|
2019-07-24 16:24:32 +03:00
|
|
|
|
2019-09-01 22:51:16 +03:00
|
|
|
fn new_table(obfuscate bool) &Table {
|
2019-06-22 21:20:28 +03:00
|
|
|
mut t := &Table {
|
|
|
|
obfuscate: obfuscate
|
|
|
|
}
|
|
|
|
t.register_type('int')
|
|
|
|
t.register_type('size_t')
|
|
|
|
t.register_type_with_parent('i8', 'int')
|
2019-09-01 22:37:22 +03:00
|
|
|
t.register_type_with_parent('byte', 'int')
|
2019-09-15 15:12:11 +03:00
|
|
|
t.register_type_with_parent('char', 'int') // for C functinos only, to avoid warnings
|
2019-06-22 21:20:28 +03:00
|
|
|
t.register_type_with_parent('i16', 'int')
|
2019-08-12 02:58:08 +03:00
|
|
|
t.register_type_with_parent('u16', 'u32')
|
2019-06-22 21:20:28 +03:00
|
|
|
t.register_type_with_parent('u32', 'int')
|
2019-07-07 23:30:15 +03:00
|
|
|
t.register_type_with_parent('i64', 'int')
|
2019-08-12 02:58:08 +03:00
|
|
|
t.register_type_with_parent('u64', 'u32')
|
2019-06-22 21:20:28 +03:00
|
|
|
t.register_type('byteptr')
|
|
|
|
t.register_type('intptr')
|
|
|
|
t.register_type('f32')
|
|
|
|
t.register_type('f64')
|
|
|
|
t.register_type('rune')
|
|
|
|
t.register_type('bool')
|
|
|
|
t.register_type('void')
|
|
|
|
t.register_type('voidptr')
|
2019-07-29 19:21:36 +03:00
|
|
|
t.register_type('T')
|
2019-06-22 21:20:28 +03:00
|
|
|
t.register_type('va_list')
|
2019-08-22 23:19:31 +03:00
|
|
|
t.register_const('stdin', 'int', 'main')
|
|
|
|
t.register_const('stdout', 'int', 'main')
|
|
|
|
t.register_const('stderr', 'int', 'main')
|
|
|
|
t.register_const('errno', 'int', 'main')
|
2019-06-22 21:20:28 +03:00
|
|
|
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.
|
2019-09-17 13:09:58 +03:00
|
|
|
fn (t &Table) var_cgen_name(name string) string {
|
2019-09-11 15:07:18 +03:00
|
|
|
if name in CReserved {
|
2019-06-22 21:20:28 +03:00
|
|
|
return 'v_$name'
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:49:52 +03:00
|
|
|
fn (t mut Table) register_module(mod string) {
|
2019-09-11 15:07:18 +03:00
|
|
|
if mod in t.modules {
|
2019-06-22 21:20:28 +03:00
|
|
|
return
|
|
|
|
}
|
2019-08-05 10:49:52 +03:00
|
|
|
t.modules << mod
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-29 19:21:36 +03:00
|
|
|
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;'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:49:52 +03:00
|
|
|
fn (table &Table) known_mod(mod string) bool {
|
|
|
|
return mod in table.modules
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-22 23:19:31 +03:00
|
|
|
fn (t mut Table) register_const(name, typ, mod string) {
|
2019-06-22 21:20:28 +03:00
|
|
|
t.consts << Var {
|
|
|
|
name: name
|
|
|
|
typ: typ
|
|
|
|
is_const: true
|
2019-08-17 22:19:37 +03:00
|
|
|
mod: mod
|
2019-09-23 22:47:09 +03:00
|
|
|
idx: -1
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2019-08-17 22:19:37 +03:00
|
|
|
mod: p.mod
|
|
|
|
is_mut: true
|
2019-09-23 22:47:09 +03:00
|
|
|
idx: -1
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-29 00:21:10 +03:00
|
|
|
// Only for module functions, not methods.
|
|
|
|
// That's why searching by fn name works.
|
2019-06-30 23:44:15 +03:00
|
|
|
fn (t mut Table) register_fn(new_fn Fn) {
|
2019-08-17 22:19:37 +03:00
|
|
|
t.fns[new_fn.name] = new_fn
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 09:19:27 +03:00
|
|
|
fn (table &Table) known_type(typ_ string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
mut typ := typ_
|
2019-09-30 13:46:50 +03:00
|
|
|
// vararg
|
|
|
|
if typ.starts_with('...') && typ.len > 3 {
|
|
|
|
typ = typ.right(3)
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// 'byte*' => look up 'byte', but don't mess up fns
|
|
|
|
if typ.ends_with('*') && !typ.contains(' ') {
|
|
|
|
typ = typ.left(typ.len - 1)
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
t := table.typesmap[typ]
|
|
|
|
return t.name.len > 0 && !t.is_placeholder
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-26 00:08:06 +03:00
|
|
|
fn (table &Table) known_type_fast(t &Type) bool {
|
2019-09-24 22:30:30 +03:00
|
|
|
return t.name != '' && !t.is_placeholder
|
|
|
|
|
2019-08-26 00:08:06 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 15:06:34 +03:00
|
|
|
fn (t &Table) find_fn(name string) ?Fn {
|
2019-08-17 22:19:37 +03:00
|
|
|
f := t.fns[name]
|
|
|
|
if !isnil(f.name.str) {
|
|
|
|
return f
|
|
|
|
}
|
2019-09-18 15:06:34 +03:00
|
|
|
return none
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (t &Table) known_fn(name string) bool {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = t.find_fn(name) or { return false }
|
2019-09-18 15:06:34 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (t &Table) known_const(name string) bool {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = t.find_const(name) or { return false }
|
2019-09-19 14:19:44 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (t mut Table) register_type(typ string) {
|
|
|
|
if typ.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
if typ in t.typesmap {
|
|
|
|
return
|
2019-08-26 00:08:06 +03:00
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
t.typesmap[typ] = Type{name:typ}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) register_type_with_parent(strtyp, parent string) {
|
|
|
|
typ := Type {
|
|
|
|
name: strtyp
|
|
|
|
parent: parent
|
2019-07-07 23:30:15 +03:00
|
|
|
mod: p.mod
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
p.table.register_type2(typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (t mut Table) register_type_with_parent(typ, parent string) {
|
|
|
|
if typ.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
t.typesmap[typ] = Type {
|
2019-06-22 21:20:28 +03:00
|
|
|
name: typ
|
|
|
|
parent: parent
|
2019-08-17 22:19:37 +03:00
|
|
|
//mod: mod
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (t mut Table) register_type2(typ Type) {
|
|
|
|
if typ.name.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
t.typesmap[typ.name] = typ
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-31 16:38:13 +03:00
|
|
|
fn (t mut Table) rewrite_type(typ Type) {
|
|
|
|
if typ.name.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
t.typesmap[typ.name] = typ
|
2019-08-31 16:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('add_field: empty type')
|
2019-08-31 16:38:13 +03:00
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
mut t := table.typesmap[type_name]
|
|
|
|
t.fields << Var {
|
|
|
|
name: field_name
|
|
|
|
typ: field_type
|
2019-06-22 21:20:28 +03:00
|
|
|
is_mut: is_mut
|
|
|
|
attr: attr
|
2019-09-01 14:31:43 +03:00
|
|
|
parent_fn: type_name // Name of the parent type
|
2019-06-22 21:20:28 +03:00
|
|
|
access_mod: access_mod
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
table.typesmap[type_name] = t
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (t &Type) has_field(name string) bool {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = t.find_field(name) or { return false }
|
2019-09-18 14:28:11 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-07 23:30:15 +03:00
|
|
|
fn (t &Type) has_enum_val(name string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
return name in t.enum_vals
|
2019-07-07 23:30:15 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 14:28:11 +03:00
|
|
|
fn (t &Type) find_field(name string) ?Var {
|
2019-06-22 21:20:28 +03:00
|
|
|
for field in t.fields {
|
|
|
|
if field.name == name {
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 14:28:11 +03:00
|
|
|
return none
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (table &Table) type_has_field(typ &Type, name string) bool {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = table.find_field(typ, name) or { return false }
|
2019-09-18 14:28:11 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 14:28:11 +03:00
|
|
|
fn (table &Table) find_field(typ &Type, name string) ?Var {
|
|
|
|
for field in typ.fields {
|
|
|
|
if field.name == name {
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if typ.parent != '' {
|
2019-06-22 21:20:28 +03:00
|
|
|
parent := table.find_type(typ.parent)
|
2019-09-18 14:28:11 +03:00
|
|
|
for field in parent.fields {
|
|
|
|
if field.name == name {
|
|
|
|
return field
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-18 14:28:11 +03:00
|
|
|
return none
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 15:37:34 +03:00
|
|
|
fn (p mut Parser) add_method(type_name string, f Fn) {
|
|
|
|
if !p.first_pass() && f.name != 'str' {
|
|
|
|
return
|
|
|
|
}
|
2019-08-31 16:38:13 +03:00
|
|
|
if type_name == '' {
|
|
|
|
print_backtrace()
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('add_method: empty type')
|
2019-08-31 16:38:13 +03:00
|
|
|
}
|
2019-09-09 16:22:39 +03:00
|
|
|
// TODO table.typesmap[type_name].methods << f
|
2019-09-18 15:37:34 +03:00
|
|
|
mut t := p.table.typesmap[type_name]
|
2019-09-29 00:21:10 +03:00
|
|
|
if f.name != 'str' && f in t.methods {
|
|
|
|
p.error('redefinition of method `${type_name}.$f.name`')
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
t.methods << f
|
2019-09-18 15:37:34 +03:00
|
|
|
p.table.typesmap[type_name] = t
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (t &Type) has_method(name string) bool {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = t.find_method(name) or { return false }
|
2019-09-18 15:37:34 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (table &Table) type_has_method(typ &Type, name string) bool {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = table.find_method(typ, name) or { return false }
|
2019-09-18 15:37:34 +03:00
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 15:37:34 +03:00
|
|
|
fn (table &Table) find_method(typ &Type, name string) ?Fn {
|
2019-09-03 14:57:04 +03:00
|
|
|
t := table.typesmap[typ.name]
|
2019-09-17 22:41:58 +03:00
|
|
|
for method in t.methods {
|
|
|
|
if method.name == name {
|
|
|
|
return method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if typ.parent != '' {
|
2019-06-22 21:20:28 +03:00
|
|
|
parent := table.find_type(typ.parent)
|
2019-09-18 15:37:34 +03:00
|
|
|
for method in parent.methods {
|
|
|
|
if method.name == name {
|
|
|
|
return method
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-18 15:37:34 +03:00
|
|
|
return none
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-18 15:37:34 +03:00
|
|
|
fn (t &Type) find_method(name string) ?Fn {
|
2019-06-22 21:20:28 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 15:37:34 +03:00
|
|
|
return none
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-17 22:19:37 +03:00
|
|
|
/*
|
2019-08-31 16:38:13 +03:00
|
|
|
// TODO
|
|
|
|
fn (t mutt Type) add_gen_type(type_name string) {
|
2019-06-22 21:20:28 +03:00
|
|
|
// println('add_gen_type($s)')
|
|
|
|
if t.gen_types.contains(type_name) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.gen_types << type_name
|
|
|
|
}
|
2019-08-17 22:19:37 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-08-31 16:38:13 +03:00
|
|
|
fn (p &Parser) find_type(name string) Type {
|
2019-06-22 21:20:28 +03:00
|
|
|
typ := p.table.find_type(name)
|
2019-08-31 16:38:13 +03:00
|
|
|
if typ.name == '' {
|
2019-08-05 10:49:52 +03:00
|
|
|
return p.table.find_type(p.prepend_mod(name))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2019-08-31 16:38:13 +03:00
|
|
|
fn (t &Table) find_type(name_ string) Type {
|
2019-08-17 22:19:37 +03:00
|
|
|
mut name := name_
|
2019-06-22 21:20:28 +03:00
|
|
|
if name.ends_with('*') && !name.contains(' ') {
|
|
|
|
name = name.left(name.len - 1)
|
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
if !(name in t.typesmap) {
|
2019-09-18 15:37:34 +03:00
|
|
|
//println('ret Type')
|
2019-09-01 14:31:43 +03:00
|
|
|
return Type{}
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
return t.typesmap[name]
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 09:19:27 +03:00
|
|
|
fn (p mut Parser) _check_types(got_, expected_ string, throw bool) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
mut got := got_
|
|
|
|
mut expected := expected_
|
2019-09-18 15:37:34 +03:00
|
|
|
//p.log('check types got="$got" exp="$expected" ')
|
2019-06-30 23:03:17 +03:00
|
|
|
if p.pref.translated {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-09-30 13:46:50 +03:00
|
|
|
// variadic
|
|
|
|
if expected.starts_with('...') {
|
|
|
|
expected = expected.right(3)
|
|
|
|
}
|
|
|
|
if got.starts_with('...') {
|
|
|
|
got = got.right(3)
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
// Allow ints to be used as floats
|
2019-06-25 22:36:44 +03:00
|
|
|
if got == 'int' && expected == 'f32' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-06-25 22:36:44 +03:00
|
|
|
if got == 'int' && expected == 'f64' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-06-25 22:36:44 +03:00
|
|
|
if got == 'f64' && expected == 'f32' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-06-25 22:36:44 +03:00
|
|
|
if got == 'f32' && expected == 'f64' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Allow ints to be used as longs
|
2019-06-25 23:19:17 +03:00
|
|
|
if got=='int' && expected=='i64' {
|
2019-06-22 21:20:28 +03:00
|
|
|
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
|
2019-08-20 14:34:29 +03:00
|
|
|
if got=='void*' || expected=='void*' {// || got == 'cvoid' || expected == 'cvoid' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
// TODO only allow numeric consts to be assigned to bytes, and
|
|
|
|
// throw an error if they are bigger than 255
|
2019-06-27 14:14:59 +03:00
|
|
|
if got=='int' && expected=='byte' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-06-27 14:14:59 +03:00
|
|
|
if got=='byteptr' && expected=='byte*' {
|
2019-06-26 00:43:04 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-07-04 02:56:18 +03:00
|
|
|
if got=='byte*' && expected=='byteptr' {
|
|
|
|
return true
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-06-27 14:14:59 +03:00
|
|
|
if got=='int' && expected=='byte*' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-08-03 10:44:08 +03:00
|
|
|
//if got=='int' && expected=='voidptr*' {
|
2019-08-17 22:19:37 +03:00
|
|
|
//return true
|
|
|
|
//}
|
2019-06-22 21:20:28 +03:00
|
|
|
// byteptr += int
|
2019-06-27 14:14:59 +03:00
|
|
|
if got=='int' && expected=='byteptr' {
|
2019-06-22 21:20:28 +03:00
|
|
|
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
|
2019-08-27 04:40:25 +03:00
|
|
|
if expected.ends_with('*') && got == 'int' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// if got == 'T' || got.contains('<T>') {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
// if expected == 'T' || expected.contains('<T>') {
|
|
|
|
// return true
|
|
|
|
// }
|
2019-09-21 21:38:12 +03:00
|
|
|
// TODO fn hack
|
|
|
|
if got.starts_with('fn ') && (expected.ends_with('fn') ||
|
|
|
|
expected.ends_with('Fn')) {
|
|
|
|
return true
|
|
|
|
}
|
2019-08-27 04:40:25 +03:00
|
|
|
// Allow pointer arithmetic
|
|
|
|
if expected=='void*' && got=='int' {
|
|
|
|
return true
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-23 02:14:10 +03:00
|
|
|
// Allow `myu64 == 1`
|
|
|
|
//if p.fileis('_test') && is_number_type(got) && is_number_type(expected) {
|
|
|
|
//p.warn('got=$got exp=$expected $p.is_const_literal')
|
|
|
|
//}
|
|
|
|
if is_number_type(got) && is_number_type(expected) && p.is_const_literal {
|
|
|
|
return true
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
expected = expected.replace('*', '')
|
|
|
|
got = got.replace('*', '')
|
|
|
|
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('expected type `$expected`, but got `$got`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// throw by default
|
|
|
|
fn (p mut Parser) check_types(got, expected string) bool {
|
2019-08-31 16:38:13 +03:00
|
|
|
if p.first_pass() { return true }
|
2019-06-22 21:20:28 +03:00
|
|
|
return p._check_types(got, expected, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check_types_no_throw(got, expected string) bool {
|
|
|
|
return p._check_types(got, expected, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-06-23 03:02:33 +03:00
|
|
|
p.error('Type "$_typ" doesn\'t satisfy interface "$interface_name" (method "$method.name" is not implemented)')
|
2019-06-22 21:20:28 +03:00
|
|
|
// }
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-01 14:31:43 +03:00
|
|
|
fn (table &Table) is_interface(name string) bool {
|
|
|
|
if !(name in table.typesmap) {
|
|
|
|
return false
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-09-01 14:31:43 +03:00
|
|
|
t := table.typesmap[name]
|
|
|
|
return t.cat == .interface_
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do we have fn main()?
|
|
|
|
fn (t &Table) main_exists() bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
for _, f in t.fns {
|
2019-09-28 20:42:29 +03:00
|
|
|
if f.name == 'main__main' {
|
2019-06-22 21:20:28 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-22 09:48:15 +03:00
|
|
|
fn (t &Table) has_at_least_one_test_fn() bool {
|
|
|
|
for _, f in t.fns {
|
2019-09-28 20:42:29 +03:00
|
|
|
if f.name.starts_with('main__test_') {
|
2019-09-22 09:48:15 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-19 14:19:44 +03:00
|
|
|
fn (t &Table) find_const(name string) ?Var {
|
2019-08-26 00:08:06 +03:00
|
|
|
//println('find const l=$t.consts.len')
|
2019-06-22 21:20:28 +03:00
|
|
|
for c in t.consts {
|
|
|
|
if c.name == name {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
2019-09-19 14:19:44 +03:00
|
|
|
return none
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ('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 == '' {
|
2019-06-23 11:12:09 +03:00
|
|
|
println('this should never happen')
|
|
|
|
exit(1)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
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' {
|
2019-06-27 20:02:47 +03:00
|
|
|
return 'struct /*TM*/ tm $name'
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return '$typ $name'
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:05:39 +03:00
|
|
|
fn is_valid_int_const(val, typ string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
x := val.int()
|
2019-07-10 17:05:39 +03:00
|
|
|
switch typ {
|
2019-09-02 15:02:25 +03:00
|
|
|
case 'byte': return 0 <= x && x <= math.MaxU8
|
2019-08-17 22:19:37 +03:00
|
|
|
case 'u16': return 0 <= x && x <= math.MaxU16
|
|
|
|
//case 'u32': return 0 <= x && x <= math.MaxU32
|
|
|
|
//case 'u64': return 0 <= x && x <= math.MaxU64
|
|
|
|
//////////////
|
|
|
|
case 'i8': return math.MinI8 <= x && x <= math.MaxI8
|
|
|
|
case 'i16': return math.MinI16 <= x && x <= math.MaxI16
|
2019-09-01 22:37:22 +03:00
|
|
|
case 'int': return math.MinI32 <= x && x <= math.MaxI32
|
2019-08-17 22:19:37 +03:00
|
|
|
//case 'i64':
|
|
|
|
//x64 := val.i64()
|
|
|
|
//return i64(-(1<<63)) <= x64 && x64 <= i64((1<<63)-1)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (t mut Table) register_generic_fn(fn_name string) {
|
|
|
|
t.generic_fns << GenTable{fn_name, []string}
|
|
|
|
}
|
|
|
|
|
2019-09-17 13:09:58 +03:00
|
|
|
fn (t &Table) fn_gen_types(fn_name string) []string {
|
2019-07-29 19:21:36 +03:00
|
|
|
for _, f in t.generic_fns {
|
|
|
|
if f.fn_name == fn_name {
|
|
|
|
return f.types
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 00:40:34 +03:00
|
|
|
verror('function $fn_name not found')
|
2019-08-29 03:30:17 +03:00
|
|
|
return []string
|
2019-08-17 22:19:37 +03:00
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
|
|
|
|
// `foo<Bar>()`
|
|
|
|
// fn_name == 'foo'
|
2019-08-17 22:19:37 +03:00
|
|
|
// typ == 'Bar'
|
|
|
|
fn (t mut Table) register_generic_fn_type(fn_name, typ string) {
|
2019-07-29 19:21:36 +03:00
|
|
|
for i, f in t.generic_fns {
|
|
|
|
if f.fn_name == fn_name {
|
2019-08-17 22:19:37 +03:00
|
|
|
t.generic_fns[i].types << typ
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 19:21:36 +03:00
|
|
|
|
|
|
|
fn (p mut Parser) typ_to_fmt(typ string, level int) string {
|
|
|
|
t := p.table.find_type(typ)
|
2019-08-20 14:34:29 +03:00
|
|
|
if t.cat == .enum_ {
|
2019-07-29 19:21:36 +03:00
|
|
|
return '%d'
|
|
|
|
}
|
|
|
|
switch typ {
|
|
|
|
case 'string': return '%.*s'
|
2019-08-09 19:10:59 +03:00
|
|
|
//case 'bool': return '%.*s'
|
2019-07-29 19:21:36 +03:00
|
|
|
case 'ustring': return '%.*s'
|
2019-09-01 22:37:22 +03:00
|
|
|
case 'byte', 'bool', 'int', 'char', 'byte', 'i16', 'i8': return '%d'
|
|
|
|
case 'u16', 'u32': return '%u'
|
2019-07-29 19:21:36 +03:00
|
|
|
case 'f64', 'f32': return '%f'
|
2019-08-12 02:58:08 +03:00
|
|
|
case 'i64': return '%lld'
|
|
|
|
case 'u64': return '%llu'
|
2019-07-29 19:21:36 +03:00
|
|
|
case 'byte*', 'byteptr': return '%s'
|
|
|
|
// case 'array_string': return '%s'
|
|
|
|
// case 'array_int': return '%s'
|
|
|
|
case 'void': p.error('cannot interpolate this value')
|
|
|
|
default:
|
|
|
|
if typ.ends_with('*') {
|
2019-08-17 22:19:37 +03:00
|
|
|
return '%p'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if t.parent != '' && level == 0 {
|
|
|
|
return p.typ_to_fmt(t.parent, level+1)
|
2019-07-29 19:21:36 +03:00
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2019-08-07 09:19:27 +03:00
|
|
|
fn is_compile_time_const(s_ string) bool {
|
|
|
|
s := s_.trim_space()
|
2019-07-29 19:21:36 +03:00
|
|
|
if s == '' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if s.contains('\'') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for c in s {
|
|
|
|
if ! ((c >= `0` && c <= `9`) || c == `.`) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-12 08:37:54 +03:00
|
|
|
// Once we have a module format we can read from module file instead
|
|
|
|
// this is not optimal
|
|
|
|
fn (table &Table) qualify_module(mod string, file_path string) string {
|
|
|
|
for m in table.imports {
|
|
|
|
if m.contains('.') && m.contains(mod) {
|
|
|
|
m_parts := m.split('.')
|
|
|
|
m_path := m_parts.join('/')
|
|
|
|
if mod == m_parts[m_parts.len-1] && file_path.contains(m_path) {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2019-09-27 14:02:01 +03:00
|
|
|
fn (table &Table) get_file_import_table(id string) FileImportTable {
|
2019-09-23 13:42:20 +03:00
|
|
|
// if file_path.clone() in table.file_imports {
|
|
|
|
// return table.file_imports[file_path.clone()]
|
|
|
|
// }
|
|
|
|
// just get imports. memory error when recycling import table
|
2019-09-27 14:02:01 +03:00
|
|
|
mut fit := new_file_import_table(id)
|
|
|
|
if id in table.file_imports {
|
|
|
|
fit.imports = table.file_imports[id].imports
|
2019-09-23 13:42:20 +03:00
|
|
|
}
|
|
|
|
return fit
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_file_import_table(file_path string) FileImportTable {
|
|
|
|
return FileImportTable{
|
2019-07-12 08:37:54 +03:00
|
|
|
file_path: file_path
|
2019-08-17 22:19:37 +03:00
|
|
|
imports: map[string]string
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 13:15:16 +03:00
|
|
|
fn (fit &FileImportTable) known_import(mod string) bool {
|
2019-07-23 23:57:06 +03:00
|
|
|
return mod in fit.imports || fit.is_aliased(mod)
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
2019-07-10 17:05:39 +03:00
|
|
|
|
2019-07-12 08:37:54 +03:00
|
|
|
fn (fit mut FileImportTable) register_import(mod string) {
|
|
|
|
fit.register_alias(mod, mod)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (fit mut FileImportTable) register_alias(alias string, mod string) {
|
2019-09-23 13:42:20 +03:00
|
|
|
// NOTE: come back here
|
|
|
|
// if alias in fit.imports && fit.imports[alias] == mod {}
|
|
|
|
if alias in fit.imports && fit.imports[alias] != mod {
|
2019-09-25 17:59:50 +03:00
|
|
|
verror('cannot import $mod as $alias: import name $alias already in use in "${fit.file_path}"')
|
2019-07-26 19:02:58 +03:00
|
|
|
}
|
|
|
|
if mod.contains('.internal.') {
|
|
|
|
mod_parts := mod.split('.')
|
|
|
|
mut internal_mod_parts := []string
|
|
|
|
for part in mod_parts {
|
|
|
|
if part == 'internal' { break }
|
|
|
|
internal_mod_parts << part
|
|
|
|
}
|
|
|
|
internal_parent := internal_mod_parts.join('.')
|
|
|
|
if !fit.module_name.starts_with(internal_parent) {
|
2019-09-25 17:59:50 +03:00
|
|
|
verror('module $mod can only be imported internally by libs')
|
2019-07-26 19:02:58 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-14 12:01:32 +03:00
|
|
|
fit.imports[alias] = mod
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (fit &FileImportTable) known_alias(alias string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
return alias in fit.imports
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (fit &FileImportTable) is_aliased(mod string) bool {
|
2019-08-17 22:19:37 +03:00
|
|
|
for _, val in fit.imports {
|
2019-07-14 12:01:32 +03:00
|
|
|
if val == mod {
|
2019-08-17 22:19:37 +03:00
|
|
|
return true
|
|
|
|
}
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (fit &FileImportTable) resolve_alias(alias string) string {
|
2019-07-23 23:57:06 +03:00
|
|
|
return fit.imports[alias]
|
2019-07-12 08:37:54 +03:00
|
|
|
}
|
2019-08-28 17:35:44 +03:00
|
|
|
|
2019-09-23 13:42:20 +03:00
|
|
|
fn (fit mut FileImportTable) register_used_import(alias string) {
|
|
|
|
if !(alias in fit.used_imports) {
|
|
|
|
fit.used_imports << alias
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (fit &FileImportTable) is_used_import(alias string) bool {
|
|
|
|
return alias in fit.used_imports
|
|
|
|
}
|
|
|
|
|
2019-08-28 17:35:44 +03:00
|
|
|
fn (t &Type) contains_field_type(typ string) bool {
|
2019-08-29 01:52:32 +03:00
|
|
|
if !t.name[0].is_capital() {
|
|
|
|
return false
|
|
|
|
}
|
2019-08-28 17:35:44 +03:00
|
|
|
for field in t.fields {
|
|
|
|
if field.typ == typ {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-09-13 14:10:24 +03:00
|
|
|
|
|
|
|
// check for a function / variable / module typo in `name`
|
2019-09-23 20:34:08 +03:00
|
|
|
fn (p &Parser) identify_typo(name string, fit &FileImportTable) string {
|
2019-09-13 14:10:24 +03:00
|
|
|
// dont check if so short
|
|
|
|
if name.len < 2 { return '' }
|
2019-09-15 20:07:12 +03:00
|
|
|
min_match := 0.50 // for dice coefficient between 0.0 - 1.0
|
2019-09-13 14:10:24 +03:00
|
|
|
name_orig := name.replace('__', '.').replace('_dot_', '.')
|
|
|
|
mut output := ''
|
|
|
|
// check functions
|
2019-09-23 20:34:08 +03:00
|
|
|
mut n := p.table.find_misspelled_fn(name, fit, min_match)
|
2019-09-13 14:10:24 +03:00
|
|
|
if n != '' {
|
|
|
|
output += '\n * function: `$n`'
|
|
|
|
}
|
|
|
|
// check function local variables
|
2019-09-23 20:34:08 +03:00
|
|
|
n = p.find_misspelled_local_var(name_orig, min_match)
|
2019-09-13 14:10:24 +03:00
|
|
|
if n != '' {
|
|
|
|
output += '\n * variable: `$n`'
|
|
|
|
}
|
|
|
|
// check imported modules
|
2019-09-23 20:34:08 +03:00
|
|
|
n = p.table.find_misspelled_imported_mod(name_orig, fit, min_match)
|
2019-09-13 14:10:24 +03:00
|
|
|
if n != '' {
|
|
|
|
output += '\n * module: `$n`'
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// find function with closest name to `name`
|
2019-09-15 20:07:12 +03:00
|
|
|
fn (table &Table) find_misspelled_fn(name string, fit &FileImportTable, min_match f32) string {
|
2019-09-13 16:15:30 +03:00
|
|
|
mut closest := f32(0)
|
2019-09-13 14:10:24 +03:00
|
|
|
mut closest_fn := ''
|
2019-09-15 20:39:48 +03:00
|
|
|
n1 := if name.starts_with('main__') { name.right(6) } else { name }
|
2019-09-13 14:10:24 +03:00
|
|
|
for _, f in table.fns {
|
2019-09-15 20:07:12 +03:00
|
|
|
if n1.len - f.name.len > 2 || f.name.len - n1.len > 2 { continue }
|
|
|
|
if !(f.mod in ['', 'main', 'builtin']) {
|
|
|
|
mut mod_imported := false
|
|
|
|
for _, m in fit.imports {
|
|
|
|
if f.mod == m {
|
|
|
|
mod_imported = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !mod_imported { continue }
|
|
|
|
}
|
|
|
|
p := strings.dice_coefficient(n1, f.name)
|
2019-09-13 14:10:24 +03:00
|
|
|
if p > closest {
|
|
|
|
closest = p
|
2019-09-15 20:07:12 +03:00
|
|
|
closest_fn = f.name
|
2019-09-13 14:10:24 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 16:15:30 +03:00
|
|
|
return if closest >= min_match { closest_fn } else { '' }
|
2019-09-13 14:10:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// find imported module with closest name to `name`
|
2019-09-13 16:15:30 +03:00
|
|
|
fn (table &Table) find_misspelled_imported_mod(name string, fit &FileImportTable, min_match f32) string {
|
|
|
|
mut closest := f32(0)
|
2019-09-13 14:10:24 +03:00
|
|
|
mut closest_mod := ''
|
2019-09-15 20:39:48 +03:00
|
|
|
n1 := if name.starts_with('main.') { name.right(5) } else { name }
|
2019-09-13 14:10:24 +03:00
|
|
|
for alias, mod in fit.imports {
|
2019-09-15 20:39:48 +03:00
|
|
|
if (n1.len - alias.len > 2 || alias.len - n1.len > 2) { continue }
|
2019-09-23 13:40:18 +03:00
|
|
|
mod_alias := if alias == mod { alias } else { '$alias ($mod)' }
|
2019-09-15 20:39:48 +03:00
|
|
|
p := strings.dice_coefficient(n1, alias)
|
2019-09-13 14:10:24 +03:00
|
|
|
if p > closest {
|
|
|
|
closest = p
|
2019-09-23 13:40:18 +03:00
|
|
|
closest_mod = '$mod_alias'
|
2019-09-13 14:10:24 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 16:15:30 +03:00
|
|
|
return if closest >= min_match { closest_mod } else { '' }
|
2019-09-13 14:10:24 +03:00
|
|
|
}
|