From 47dccd56a2e98172663f299cd98843ab7c8d515c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 28 Apr 2022 13:15:09 +0300 Subject: [PATCH] eval: small cleanup in the handling of `$if platform {` --- vlib/v/eval/eval.v | 10 +++++----- vlib/v/eval/expr.v | 35 +++++++++++------------------------ 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/vlib/v/eval/eval.v b/vlib/v/eval/eval.v index 2e27e88268..e64fcdcde4 100644 --- a/vlib/v/eval/eval.v +++ b/vlib/v/eval/eval.v @@ -117,7 +117,6 @@ pub fn (mut e Eval) register_symbols(mut files []&ast.File) { for mut file in files { file.idx = e.trace_file_paths.len e.trace_file_paths << file.path - // eprintln('registering file: $file.path_base') mod := file.mod.name for mut stmt in file.stmts { if mut stmt is ast.FnDecl { @@ -205,7 +204,7 @@ pub fn (mut e Eval) register_symbol(stmt ast.Stmt, mod string, file string) { } } else { - eprintln('unsupported expression') + e.error('unsupported expression') } } } @@ -223,9 +222,10 @@ pub fn (mut e Eval) register_symbol(stmt ast.Stmt, mod string, file string) { fn (e Eval) error(msg string) { eprintln('> V interpeter backtrace:') - for idx, t in e.back_trace { - eprintln(' ${e.trace_file_paths[t.file_idx]}:${t.line + 1}:${e.trace_function_names[t.fn_idx]}') - // eprintln('${e.trace_file_paths[t.file_idx]}:${t.line + 1}:$t.fn_idx') + for t in e.back_trace { + file_path := e.trace_file_paths[t.file_idx] or { t.file_idx.str() } + fn_name := e.trace_function_names[t.fn_idx] or { t.fn_idx.str() } + eprintln(' $file_path:${t.line + 1}:$fn_name}') } util.verror('interpreter', msg) } diff --git a/vlib/v/eval/expr.v b/vlib/v/eval/expr.v index 2e78ec5f0a..f6bbf78fe6 100644 --- a/vlib/v/eval/expr.v +++ b/vlib/v/eval/expr.v @@ -1,5 +1,6 @@ module eval +import v.pref import v.ast import v.util import math @@ -134,30 +135,16 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object { do_if = true } else { if branch.cond is ast.Ident { - match branch.cond.name { - 'windows' { - do_if = e.pref.os == .windows - } - 'macos' { - do_if = e.pref.os == .macos - } - 'linux' { - do_if = e.pref.os == .linux - } - 'android' { - do_if = e.pref.os == .android - } - 'freebsd' { - do_if = e.pref.os == .freebsd - } - 'openbsd' { - do_if = e.pref.os == .openbsd - } - 'prealloc' { - do_if = e.pref.prealloc - } - else { - e.error('unknown compile time if: $branch.cond.name') + if known_os := pref.os_from_string(branch.cond.name) { + do_if = e.pref.os == known_os + } else { + match branch.cond.name { + 'prealloc' { + do_if = e.pref.prealloc + } + else { + e.error('unknown compile time if: $branch.cond.name') + } } } } else if branch.cond is ast.PostfixExpr {