From d5f90ef64be04567c5f2f07972f16ccb6029a00d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 10 Feb 2020 18:42:53 +0100 Subject: [PATCH] v2: handle unresolved consts --- vlib/v/checker/checker.v | 7 +++++++ vlib/v/gen/cgen_test.v | 2 +- vlib/v/gen/tests/1.c | 9 ++++++--- vlib/v/gen/tests/1.vv | 8 +++++--- vlib/v/parser/parser.v | 8 ++++++-- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0ef0343357..35b5c333f9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -384,6 +384,13 @@ pub fn (c &Checker) expr(node ast.Expr) table.Type { } return info.typ } + // Handle indents with unresolved types during the parsing step + // (declared after first usage) + else if it.kind == .blank_ident { + if constant := c.table.find_const(it.name) { + return constant.typ + } + } return table.void_type } ast.BoolLiteral { diff --git a/vlib/v/gen/cgen_test.v b/vlib/v/gen/cgen_test.v index 7f91f5b080..8373a8f119 100644 --- a/vlib/v/gen/cgen_test.v +++ b/vlib/v/gen/cgen_test.v @@ -33,7 +33,7 @@ fn test_c_files() { } else { eprintln('${term_fail} ${i}') - eprintln('${path}: got\n{$res}') + eprintln('${path}: got\n$res') assert false } } diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 10a8db9de2..68b237596a 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -8,7 +8,7 @@ void variadic(variadic_int a); void ensure_cap(int required, int cap); void println(string s); void matches(); - +void end(); int pi = 3; typedef struct { @@ -30,7 +30,6 @@ int main() { void foo(int a) { while (true) { - } for (int i = 0; i < 10; i++; @@ -92,6 +91,7 @@ multi_return_int_string multi_return() { } void variadic(variadic_int a) { + int x = path_sep; } void ensure_cap(int required, int cap) { @@ -115,6 +115,9 @@ void matches() { } ; - } } +int path_sep = 10; + +void end() { +} diff --git a/vlib/v/gen/tests/1.vv b/vlib/v/gen/tests/1.vv index a703bb41c1..5e56003f7c 100644 --- a/vlib/v/gen/tests/1.vv +++ b/vlib/v/gen/tests/1.vv @@ -97,7 +97,7 @@ fn multi_return() (int,string) { } fn variadic(a ...int) { - //a := path_sep + x := path_sep } fn ensure_cap(required int, cap int) { @@ -120,10 +120,12 @@ fn matches() { } } -/* const ( path_sep = 10 ) -*/ + +fn end() { + +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index ce5ab7e887..cd36ada4c1 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -437,8 +437,12 @@ pub fn (p mut Parser) parse_ident(is_c bool) (ast.Ident,table.Type) { // Function object (not a call), e.g. `onclick(my_click)` p.table.find_fn(name) or { // ident.info = ast.IdentVar - p.error('parse_ident: unknown identifier `$name`') - exit(0) + node = ast.Ident{ + kind: .blank_ident + name: name + } + return node,typ + // p.error('parse_ident: unknown identifier `$name`') } // p.next() }