1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: small optimizations

This commit is contained in:
Alexander Medvednikov 2019-08-26 00:08:06 +03:00
parent 794cd561cd
commit 76a89c832e
3 changed files with 15 additions and 10 deletions

View File

@ -511,7 +511,8 @@ fn (p mut Parser) struct_decl() {
if !is_c && !p.builtin_mod && p.mod != 'main' {
name = p.prepend_mod(name)
}
if p.pass == .decl && p.table.known_type(name) {
mut typ := p.table.find_type(name)
if p.pass == .decl && p.table.known_type_fast(typ) {
p.error('`$name` redeclared')
}
if !is_c {
@ -519,7 +520,6 @@ fn (p mut Parser) struct_decl() {
p.gen_typedef('typedef $kind $name $name;')
}
// Register the type
mut typ := p.table.find_type(name)
mut is_ph := false
if typ.is_placeholder {
// Update the placeholder
@ -1472,8 +1472,8 @@ fn (p mut Parser) name_expr() string {
}
// //////////////////////////
// module ?
// Allow shadowing (gg = gg.newcontext(); gg.draw_triangle())
if ((name == p.mod && p.table.known_mod(name)) || p.import_table.known_alias(name))
// (Allow shadowing `gg = gg.newcontext(); gg.draw_triangle();` )
if p.peek() == .dot && ((name == p.mod && p.table.known_mod(name)) || p.import_table.known_alias(name))
&& !p.cur_fn.known_var(name) && !is_c {
mut mod := name
// must be aliased module

View File

@ -320,6 +320,10 @@ fn (table &Table) known_type(typ_ string) bool {
return t.name.len > 0 && !t.is_placeholder
}
fn (table &Table) known_type_fast(t &Type) bool {
return t.name.len > 0 && !t.is_placeholder
}
fn (t &Table) find_fn(name string) Fn {
f := t.fns[name]
if !isnil(f.name.str) {
@ -684,6 +688,7 @@ fn (t &Table) main_exists() bool {
// TODO use `?Var`
fn (t &Table) find_const(name string) Var {
//println('find const l=$t.consts.len')
for c in t.consts {
if c.name == name {
return c

View File

@ -58,7 +58,7 @@ fn (m mut map) insert(n mut mapnode, key string, val voidptr) {
return
}
if n.key > key {
if isnil(n.left) {
if n.left == 0 {
n.left = new_node(key, val, m.element_size)
m.size++
} else {
@ -66,7 +66,7 @@ fn (m mut map) insert(n mut mapnode, key string, val voidptr) {
}
return
}
if isnil(n.right) {
if n.right == 0 {
n.right = new_node(key, val, m.element_size)
m.size++
} else {
@ -80,14 +80,14 @@ fn (n & mapnode) find(key string, out voidptr, element_size int) bool{
return true
}
else if n.key > key {
if isnil(n.left) {
if n.left == 0 {
return false
} else {
return n.left.find(key, out, element_size)
}
}
else {
if isnil(n.right) {
if n.right == 0 {
return false
} else {
return n.right.find(key, out, element_size)
@ -178,7 +178,7 @@ pub fn (m mut map) keys() []string {
}
fn (m map) get(key string, out voidptr) bool {
if isnil(m.root) {
if m.root == 0 {
return false
}
return m.root.find(key, out, m.element_size)