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
|
|
|
|
)
|
|
|
|
|
|
|
|
pub fn (p mut Parser) parse_array_ti(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-03 07:02:54 +01:00
|
|
|
elem_ti := p.parse_type()
|
2020-02-04 12:50:58 +01:00
|
|
|
p.check(.rsbr)
|
|
|
|
p.check_name()
|
2020-01-06 16:13:12 +01:00
|
|
|
idx,name := p.table.find_or_register_array_fixed(&elem_ti, size, 1)
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.array_fixed, name, idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
// array
|
2020-01-07 13:10:05 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-03 07:02:54 +01:00
|
|
|
elem_ti := 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,name := p.table.find_or_register_array(&elem_ti, nr_dims)
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.array, name, idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-04 12:50:58 +01:00
|
|
|
pub fn (p mut Parser) parse_map_type(nr_muls int) table.Type {
|
|
|
|
if p.tok.kind != .lsbr {
|
|
|
|
return table.map_type
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
p.next()
|
|
|
|
p.check(.lsbr)
|
2020-02-03 07:02:54 +01:00
|
|
|
key_ti := p.parse_type()
|
2020-01-06 16:13:12 +01:00
|
|
|
p.check(.rsbr)
|
2020-02-03 07:02:54 +01:00
|
|
|
value_ti := p.parse_type()
|
2020-01-06 16:13:12 +01:00
|
|
|
idx,name := p.table.find_or_register_map(&key_ti, &value_ti)
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.map, name, idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:34:38 +01:00
|
|
|
pub fn (p mut Parser) parse_multi_return_ti() table.Type {
|
2020-01-06 16:13:12 +01:00
|
|
|
p.check(.lpar)
|
2020-01-22 21:34:38 +01:00
|
|
|
mut mr_tis := []table.Type
|
2020-01-06 16:13:12 +01:00
|
|
|
for {
|
2020-02-03 07:02:54 +01:00
|
|
|
mr_ti := p.parse_type()
|
2020-01-07 12:10:07 +01:00
|
|
|
mr_tis << mr_ti
|
2020-01-06 16:13:12 +01:00
|
|
|
if p.tok.kind == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.check(.rpar)
|
|
|
|
idx,name := p.table.find_or_register_multi_return(mr_tis)
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.multi_return, name, idx, 0)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:34:38 +01:00
|
|
|
pub fn (p mut Parser) parse_variadic_ti() table.Type {
|
2020-01-06 16:13:12 +01:00
|
|
|
p.check(.ellipsis)
|
2020-02-03 07:02:54 +01:00
|
|
|
variadic_ti := p.parse_type()
|
2020-01-06 16:13:12 +01:00
|
|
|
idx,name := p.table.find_or_register_variadic(&variadic_ti)
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.variadic, name, idx, 0)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
pub fn (p mut Parser) parse_fn_type() table.Type {
|
2020-02-04 09:54:15 +01:00
|
|
|
// p.check(.key_fn)
|
2020-02-03 07:02:54 +01:00
|
|
|
p.fn_decl()
|
|
|
|
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()
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
name := p.tok.lit
|
|
|
|
match p.tok.kind {
|
2020-02-03 07:02:54 +01:00
|
|
|
// func
|
|
|
|
.key_fn {
|
|
|
|
return p.parse_fn_type()
|
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
// array
|
|
|
|
.lsbr {
|
|
|
|
return p.parse_array_ti(nr_muls)
|
|
|
|
}
|
|
|
|
// multiple return
|
|
|
|
.lpar {
|
|
|
|
if nr_muls > 0 {
|
|
|
|
p.error('parse_ti: unexpected `&` before multiple returns')
|
|
|
|
}
|
|
|
|
return p.parse_multi_return_ti()
|
|
|
|
}
|
|
|
|
// variadic
|
|
|
|
.ellipsis {
|
|
|
|
if nr_muls > 0 {
|
|
|
|
p.error('parse_ti: unexpected `&` before variadic')
|
|
|
|
}
|
|
|
|
return p.parse_variadic_ti()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
defer {
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
match name {
|
|
|
|
'map' {
|
2020-02-04 12:50:58 +01:00
|
|
|
return p.parse_map_type(nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'voidptr' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.voidptr, 'voidptr', table.voidptr_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'byteptr' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.byteptr, 'byteptr', table.byteptr_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'charptr' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.charptr, 'charptr', table.charptr_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'i8' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.i8, 'i8', table.i8_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'i16' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.i16, 'i16', table.i16_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'int' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.int, 'int', table.int_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'i64' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.i64, 'i64', table.i64_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'byte' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.byte, 'byte', table.byte_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'u16' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.u16, 'u16', table.u16_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'u32' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.u32, 'u32', table.u32_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'u64' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.u64, 'u64', table.u64_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'f32' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.f32, 'f32', table.f32_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'f64' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.f64, 'f64', table.f64_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'string' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.string, 'string', table.string_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'char' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.char, 'char', table.charptr_type_idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
'bool' {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.bool, 'bool', 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)
|
2020-01-18 23:26:14 +01:00
|
|
|
if idx > 0 {
|
2020-01-22 21:34:38 +01:00
|
|
|
return table.new_type(p.table.types[idx].kind, name, idx, nr_muls)
|
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-01-22 21:34:38 +01:00
|
|
|
return table.new_type(.placeholder, name, idx, nr_muls)
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
2020-01-07 12:10:07 +01:00
|
|
|
}
|
2020-01-06 16:13:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|