v/vlib/v/parser/parse_type.v

193 lines
4.3 KiB
V
Raw Normal View History

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.
import (
v.table
)
pub fn (p mut Parser) parse_array_type(nr_muls int) 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()
idx := p.table.find_or_register_array_fixed(elem_type, size, 1)
return table.new_type_ptr(idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
// array
2020-01-07 13:10:05 +01:00
p.check(.rsbr)
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++
}
idx := p.table.find_or_register_array(elem_type, nr_dims)
return table.new_type_ptr(idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
pub fn (p mut Parser) parse_map_type(nr_muls int) 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 {
return table.new_type(table.map_type_idx)
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()
// 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)
return table.new_type_ptr(idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
pub fn (p mut Parser) parse_multi_return_type() table.Type {
2020-01-06 16:13:12 +01:00
p.check(.lpar)
mut mr_types := []table.Type
2020-01-06 16:13:12 +01:00
for {
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)
idx := p.table.find_or_register_multi_return(mr_types)
return table.new_type(idx)
2020-01-06 16:13:12 +01:00
}
pub fn (p mut Parser) parse_fn_type() table.Type {
2020-02-11 13:21:41 +01:00
p.warn('parrse fn')
p.check(.key_fn)
// p.fn_decl()
p.fn_args()
if p.tok.kind.is_start_of_type() {
p.parse_type()
}
return table.int_type
}
pub fn (p mut Parser) parse_type() table.Type {
2020-01-06 16:13:12 +01:00
mut nr_muls := 0
for p.tok.kind == .amp {
p.check(.amp)
nr_muls++
}
2020-02-04 09:54:15 +01:00
if p.tok.lit == 'C' {
p.next()
p.check(.dot)
}
2020-02-04 20:22:00 +01:00
if p.tok.kind == .question {
p.next()
}
// `module.Type`
2020-02-12 01:16:38 +01:00
if p.peek_tok.kind == .dot {
// /if !(p.tok.lit in p.table.imports) {
2020-02-12 01:16:38 +01:00
if !p.table.known_import(p.tok.lit) {
println(p.table.imports)
p.error('unknown module `$p.tok.lit`')
}
p.next()
p.check(.dot)
}
2020-01-06 16:13:12 +01:00
name := p.tok.lit
match p.tok.kind {
// func
.key_fn {
return p.parse_fn_type()
}
2020-01-06 16:13:12 +01:00
// array
.lsbr {
return p.parse_array_type(nr_muls)
2020-01-06 16:13:12 +01:00
}
// multiple return
.lpar {
if nr_muls > 0 {
p.error('parse_type: unexpected `&` before multiple returns')
2020-01-06 16:13:12 +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' {
return p.parse_map_type(nr_muls)
}
2020-01-06 16:13:12 +01:00
defer {
p.next()
}
match name {
'voidptr' {
return table.new_type_ptr(table.voidptr_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'byteptr' {
return table.new_type_ptr(table.byteptr_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'charptr' {
return table.new_type_ptr(table.charptr_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'i8' {
return table.new_type_ptr(table.i8_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'i16' {
return table.new_type_ptr(table.i16_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'int' {
return table.new_type_ptr(table.int_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'i64' {
return table.new_type_ptr(table.i64_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'byte' {
return table.new_type_ptr(table.byte_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'u16' {
return table.new_type_ptr(table.u16_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'u32' {
return table.new_type_ptr(table.u32_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'u64' {
return table.new_type_ptr(table.u64_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'f32' {
return table.new_type_ptr(table.f32_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'f64' {
return table.new_type_ptr(table.f64_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'string' {
return table.new_type_ptr(table.string_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'char' {
return table.new_type_ptr(table.char_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
'bool' {
return table.new_type_ptr(table.bool_type_idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
// struct / enum / placeholder
else {
// struct / enum
mut idx := p.table.find_type_idx(name)
if idx > 0 {
return table.new_type_ptr(idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
// not found - add placeholder
idx = p.table.add_placeholder_type(name)
println('NOT FOUND: $name - adding placeholder - $idx')
return table.new_type_ptr(idx, nr_muls)
2020-01-06 16:13:12 +01:00
}
}
2020-01-06 16:13:12 +01:00
}
}
}