v icon indicating copy to clipboard operation
v copied to clipboard

Compilation error

Open bobbae opened this issue 4 years ago • 1 comments

V version: 0.2.2 4c22370 OS: linux, Ubuntu 18.04.2 LTS (WSL 2)

What did you do?

v run main.v

What did you expect to see?

successful complation

What did you see instead?

==================
/tmp/v/main.6377621260036634008.tmp.c:9094: error: struct or union expected
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

If you were not working with C interop, this is a compiler bug, please report the bug using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

main.v

[heap]
struct Node  {
	mut:
		letter rune
		end bool
		counter int
		children []&Node
}

struct Trie {
	root &Node
}

fn init_trie() Trie {
	return Trie{
		root: &Node{}
	}
}

fn (t Trie) insert(word string) {
	mut node := t.root
	for letter in word {
		mut found_in_child := false
	
		for mut child in node.children {
			if child.letter == letter {
				child.counter++
				node = child
				found_in_child = true
				break
			}
		}
		if !found_in_child {
			mut new_node := Node{ letter: letter }
			node.children << &new_node
			println("insert $new_node to $node children")
			node = &new_node
		}
	}
	node.end = true
	println("root $t")
}

fn (t Trie) find(word string) (bool, int) {
	mut node := t.root
	for letter in word {
		mut letter_not_found := true
		for child in node.children {
			if child.letter == letter {
				letter_not_found = false
				node = child
				break
			}
		}
		if letter_not_found {
			println("letter $letter not found")
			for child in t.root.children {
				println("child $child")
			}
			return false, 0
		}
	}
	return true, node.counter
}

fn main() {
	trie := init_trie()
	words := ["cat","dog", "catalog"]
	
	for i := 0; i < words.len; i++ {
		trie.insert(words[i])
	}
	words_to_find := ["cat", "dogg", "cata"]

	for i := 0; i < words_to_find.len; i++ {
		wf := words_to_find[i]
		found, _ := trie.find(wf)
		if found {
			println("word $wf found")
		} else {
			println("word $wf not found")
		}
	}
}

bobbae avatar May 17 '21 22:05 bobbae

For me:

...
                end: false
                counter: 0
                children: [Node{
                    letter: 103
                    end: true
                    counter: 0
                    children: []
                }]
            }]
        }]
    }
}
/tmp/v/main.9893262333688967315.tmp.c:1756: at indent_Array_main__Node_ptr_str: RUNTIME ERROR: invalid memory access
/tmp/v/main.9893262333688967315.tmp.c:1776: by indent_main__Node_str
/tmp/v/main.9893262333688967315.tmp.c:1750: by main__Node_str
/tmp/v/main.9893262333688967315.tmp.c:10293: by main__Trie_insert
/tmp/v/main.9893262333688967315.tmp.c:10330: by main__main
/tmp/v/main.9893262333688967315.tmp.c:10711: by main

danieldaeschle avatar Jul 22 '21 15:07 danieldaeschle

Already fixed.

felipensp avatar May 26 '23 17:05 felipensp