2020-01-06 16:13:12 +01:00
|
|
|
module parser
|
2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2020-01-06 16:13:12 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-01-22 21:34:38 +01:00
|
|
|
import (
|
|
|
|
v.table
|
|
|
|
)
|
|
|
|
|
2020-02-21 22:50:21 +01:00
|
|
|
pub fn (p mut Parser) parse_array_type() table.Type {
|
2020-01-06 16:13:12 +01:00
|
|
|
p.check(.lsbr)
|
|
|
|
// fixed array
|
|
|
|
if p.tok.kind == .number {
|
|
|
|
size := p.tok.lit.int()
|
2020-02-11 04:45:33 +01:00
|
|
|
p.next()
|
2020-02-04 12:50:58 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-11 04:45:33 +01:00
|
|
|
elem_type := p.parse_type()
|
2020-02-10 14:43:17 +01:00
|
|
|
idx := p.table.find_or_register_array_fixed(elem_type, size, 1)
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.new_type(idx)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
// array
|
2020-01-07 13:10:05 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-10 14:43:17 +01:00
|
|
|
elem_type := p.parse_type()
|
2020-01-06 16:13:12 +01:00
|
|
|
mut nr_dims := 1
|
|
|
|
for p.tok.kind == .lsbr {
|
|
|
|
p.check(.lsbr)
|
2020-01-07 13:10:05 +01:00
|
|
|
p.check(.rsbr)
|
2020-01-06 16:13:12 +01:00
|
|
|
nr_dims++
|
|
|
|
}
|
2020-02-10 14:43:17 +01:00
|
|
|
idx := p.table.find_or_register_array(elem_type, nr_dims)
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.new_type(idx)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 22:50:21 +01:00
|
|
|
pub fn (p mut Parser) parse_map_type() table.Type {
|
2020-02-08 16:59:57 +01:00
|
|
|
p.next()
|
2020-02-04 12:50:58 +01:00
|
|
|
if p.tok.kind != .lsbr {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.map_type
|
2020-02-04 12:50:58 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
p.check(.lsbr)
|
2020-02-08 16:59:57 +01:00
|
|
|
key_type := p.parse_type()
|
2020-02-10 08:32:08 +01:00
|
|
|
// key_type_sym := p.get_type_symbol(key_type)
|
|
|
|
// if key_type_sym.kind != .string {
|
|
|
|
if table.type_idx(key_type) != table.string_type_idx {
|
2020-02-08 16:59:57 +01:00
|
|
|
p.error('maps can only have string keys for now')
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-08 16:59:57 +01:00
|
|
|
value_type := p.parse_type()
|
|
|
|
idx := p.table.find_or_register_map(key_type, value_type)
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.new_type(idx)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:32:08 +01:00
|
|
|
pub fn (p mut Parser) parse_multi_return_type() table.Type {
|
2020-01-06 16:13:12 +01:00
|
|
|
p.check(.lpar)
|
2020-02-10 08:32:08 +01:00
|
|
|
mut mr_types := []table.Type
|
2020-01-06 16:13:12 +01:00
|
|
|
for {
|
2020-02-06 13:57:35 +01:00
|
|
|
mr_type := p.parse_type()
|
|
|
|
mr_types << mr_type
|
2020-01-06 16:13:12 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check(.rpar)
|
2020-02-06 13:57:35 +01:00
|
|
|
idx := p.table.find_or_register_multi_return(mr_types)
|
2020-02-10 08:32:08 +01:00
|
|
|
return table.new_type(idx)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 16:10:46 +01:00
|
|
|
// given anon name based off signature when `name` is blank
|
|
|
|
pub fn (p mut Parser) parse_fn_type(name string) table.Type {
|
|
|
|
// p.warn('parse fn')
|
2020-02-11 13:21:41 +01:00
|
|
|
p.check(.key_fn)
|
2020-03-14 23:21:36 +01:00
|
|
|
args, is_variadic := p.fn_args()
|
2020-03-11 16:10:46 +01:00
|
|
|
mut return_type := table.void_type
|
2020-02-11 13:21:41 +01:00
|
|
|
if p.tok.kind.is_start_of_type() {
|
2020-03-11 16:10:46 +01:00
|
|
|
return_type = p.parse_type()
|
2020-02-11 13:21:41 +01:00
|
|
|
}
|
2020-03-11 16:10:46 +01:00
|
|
|
func := table.Fn{
|
|
|
|
name: name
|
|
|
|
args: args
|
|
|
|
is_variadic: is_variadic
|
|
|
|
return_type: return_type
|
|
|
|
}
|
|
|
|
idx := p.table.find_or_register_fn_type(func)
|
|
|
|
return table.new_type(idx)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 08:32:08 +01:00
|
|
|
pub fn (p mut Parser) parse_type() table.Type {
|
2020-02-15 13:37:48 +01:00
|
|
|
// optional
|
|
|
|
mut is_optional := false
|
|
|
|
if p.tok.kind == .question {
|
|
|
|
p.next()
|
|
|
|
is_optional = true
|
|
|
|
}
|
|
|
|
// &Type
|
2020-01-06 16:13:12 +01:00
|
|
|
mut nr_muls := 0
|
|
|
|
for p.tok.kind == .amp {
|
|
|
|
p.check(.amp)
|
|
|
|
nr_muls++
|
|
|
|
}
|
2020-03-08 19:38:27 +01:00
|
|
|
if p.tok.kind == .key_mut {
|
|
|
|
nr_muls++
|
|
|
|
p.next()
|
|
|
|
}
|
2020-02-29 11:47:47 +01:00
|
|
|
is_c := p.tok.lit == 'C'
|
|
|
|
if is_c {
|
2020-02-04 09:54:15 +01:00
|
|
|
p.next()
|
|
|
|
p.check(.dot)
|
|
|
|
}
|
2020-02-29 11:47:47 +01:00
|
|
|
mut typ := p.parse_any_type(is_c, nr_muls > 0)
|
2020-02-21 11:44:06 +01:00
|
|
|
if is_optional {
|
|
|
|
typ = table.type_to_optional(typ)
|
2020-02-16 11:48:29 +01:00
|
|
|
}
|
2020-02-21 11:44:06 +01:00
|
|
|
if nr_muls > 0 {
|
|
|
|
typ = table.type_set_nr_muls(typ, nr_muls)
|
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2020-02-29 11:47:47 +01:00
|
|
|
pub fn (p mut Parser) parse_any_type(is_c, is_ptr bool) table.Type {
|
2020-02-16 11:48:29 +01:00
|
|
|
mut name := p.tok.lit
|
2020-02-29 11:47:47 +01:00
|
|
|
if is_c {
|
|
|
|
name = 'C.$name'
|
|
|
|
}
|
2020-02-12 17:39:35 +01:00
|
|
|
// `module.Type`
|
2020-02-29 11:47:47 +01:00
|
|
|
else if p.peek_tok.kind == .dot {
|
2020-02-12 17:39:35 +01:00
|
|
|
// /if !(p.tok.lit in p.table.imports) {
|
2020-02-20 11:13:18 +01:00
|
|
|
if !p.known_import(name) {
|
2020-02-12 01:16:38 +01:00
|
|
|
println(p.table.imports)
|
|
|
|
p.error('unknown module `$p.tok.lit`')
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
p.check(.dot)
|
2020-02-20 11:13:18 +01:00
|
|
|
// prefix with full module
|
|
|
|
name = '${p.imports[name]}.$p.tok.lit'
|
2020-02-16 11:48:29 +01:00
|
|
|
}
|
2020-02-27 00:12:37 +01:00
|
|
|
else if p.expr_mod != '' {
|
|
|
|
name = p.expr_mod + '.' + name
|
|
|
|
}
|
2020-02-16 11:48:29 +01:00
|
|
|
// `Foo` in module `mod` means `mod.Foo`
|
2020-02-19 03:08:10 +01:00
|
|
|
else if !(p.mod in ['builtin', 'main']) && !(name in table.builtin_type_names) {
|
2020-02-16 11:48:29 +01:00
|
|
|
name = p.mod + '.' + name
|
2020-02-12 01:16:38 +01:00
|
|
|
}
|
2020-02-16 11:48:29 +01:00
|
|
|
// p.warn('get type $name')
|
2020-01-06 16:13:12 +01:00
|
|
|
match p.tok.kind {
|
2020-02-03 07:02:54 +01:00
|
|
|
// func
|
|
|
|
.key_fn {
|
2020-03-11 16:10:46 +01:00
|
|
|
return p.parse_fn_type('')
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
// array
|
|
|
|
.lsbr {
|
2020-02-21 22:50:21 +01:00
|
|
|
return p.parse_array_type()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
// multiple return
|
|
|
|
.lpar {
|
2020-02-21 22:50:21 +01:00
|
|
|
if is_ptr {
|
2020-02-10 08:32:08 +01:00
|
|
|
p.error('parse_type: unexpected `&` before multiple returns')
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-02-10 08:32:08 +01:00
|
|
|
return p.parse_multi_return_type()
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-08 16:59:57 +01:00
|
|
|
// no defer
|
|
|
|
if name == 'map' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return p.parse_map_type()
|
2020-02-08 16:59:57 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
defer {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
match name {
|
|
|
|
'voidptr' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.voidptr_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'byteptr' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.byteptr_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'charptr' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.charptr_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'i8' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.i8_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'i16' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.i16_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'int' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.int_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'i64' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.i64_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'byte' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.byte_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'u16' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.u16_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'u32' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.u32_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'u64' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.u64_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'f32' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.f32_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'f64' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.f64_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'string' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.string_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'char' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.char_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'bool' {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.bool_type
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
// struct / enum / placeholder
|
|
|
|
else {
|
|
|
|
// struct / enum
|
|
|
|
mut idx := p.table.find_type_idx(name)
|
2020-01-18 23:26:14 +01:00
|
|
|
if idx > 0 {
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.new_type(idx)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
// not found - add placeholder
|
|
|
|
idx = p.table.add_placeholder_type(name)
|
2020-02-19 16:12:39 +01:00
|
|
|
// println('NOT FOUND: $name - adding placeholder - $idx')
|
2020-02-21 22:50:21 +01:00
|
|
|
return table.new_type(idx)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-03-08 19:38:27 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|