From 8bb8b7d76e106df7af182b3e89e2c6c7b7e64e3b Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Fri, 21 Feb 2020 21:44:06 +1100 Subject: [PATCH] v2: add ability to store extra information with Type such as optional --- vlib/v/parser/parse_type.v | 17 ++++++++-- vlib/v/table/types.v | 65 +++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index c9a6252d0a..a9b113cfc7 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -94,9 +94,20 @@ pub fn (p mut Parser) parse_type() table.Type { p.next() p.check(.dot) } - if p.tok.kind == .question { - p.next() + //if p.tok.kind == .question { + // p.next() + //} + mut typ := p.parse_any_type(nr_muls) + if is_optional { + typ = table.type_to_optional(typ) } + if nr_muls > 0 { + typ = table.type_set_nr_muls(typ, nr_muls) + } + return typ +} + +pub fn (p mut Parser) parse_any_type(nr_muls int) table.Type { mut name := p.tok.lit // `module.Type` if p.peek_tok.kind == .dot { @@ -200,7 +211,7 @@ pub fn (p mut Parser) parse_type() table.Type { // println('NOT FOUND: $name - adding placeholder - $idx') return table.new_type_ptr(idx, nr_muls) } - } + } } } } diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index 09e9e708c6..ad5b557e8b 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -1,16 +1,29 @@ module table pub type Type int + +pub enum TypeExtra { + unset + optional +} + // return underlying TypeSymbol idx [inline] pub fn type_idx(t Type) int { - return i16(int(t)>>16) & 0xffffffff + return u16(t) & 0xffff } + // return nr_muls [inline] pub fn type_nr_muls(t Type) int { - return i16(int(t) & 0xffffffff) + return (int(t) >> 16) & 0xff +} + +// return extra +[inline] +pub fn type_extra(t Type) TypeExtra { + return ((int(t) >> 24) & 0xff) } // return true if pointer (nr_muls>0) @@ -19,42 +32,64 @@ pub fn type_is_ptr(t Type) bool { return type_nr_muls(t) > 0 } +// set nr_muls on Type and return it +[inline] +pub fn type_set_nr_muls(t Type, nr_muls int) Type { + if nr_muls < 0 || nr_muls > 255 { + panic('typ_set_nr_muls: nr_muls must be between 0 & 255') + } + return (int(type_extra(t)) << 24) | (nr_muls << 16) | u16(type_idx(t)) +} + // increments nr_nuls on Type and return it [inline] pub fn type_to_ptr(t Type) Type { - return type_idx(t)< 32767 || idx < -32767 { - panic('new_type_id: idx must be between -32767 & 32767') + if idx < 1 || idx > 65536 { + panic('new_type_id: idx must be between 1 & 65536') } - return idx< 32767 || idx < -32767 { - panic('typ_ptr: idx must be between -32767 & 32767') +pub fn new_type_ptr(idx int, nr_muls int) Type { + if idx < 1 || idx > 65536 { + panic('typ_ptr: idx must be between 1 & 65536') } - if nr_muls > 32767 || nr_muls < -0 { - panic('typ_ptr: nr_muls must be between 0 & 32767') + if nr_muls < 0 || nr_muls > 255 { + panic('typ_ptr: nr_muls must be between 0 & 255') } - return idx<