From 6ffed854cc865bcc38f4439278b7cdd34690d804 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Dec 2019 20:43:13 +0300 Subject: [PATCH] cgen.prepend_to_statement() --- v.v | 6 +++--- vlib/compiler/cgen.v | 14 ++++++++++++++ vlib/compiler/gen_c.v | 23 +++++++++++------------ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/v.v b/v.v index 1d62e2c4de..f9ddf2192d 100755 --- a/v.v +++ b/v.v @@ -24,7 +24,7 @@ fn main() { // which may be surprising to v users. stuff_after_executable := os.args[1..] commands := stuff_after_executable.filter(!it.starts_with('-')) - + simple_tools := ['up', 'create', 'test', 'test-compiler', 'build-tools', 'build-examples', 'build-vbinaries'] for tool in simple_tools { if tool in commands { @@ -32,7 +32,7 @@ fn main() { return } } - + // Print the version and exit. if '-v' in options || '--version' in options || 'version' in commands { version_hash := compiler.vhash() @@ -44,7 +44,7 @@ fn main() { return } else if 'translate' in commands { - println('Translating C to V will be available in V 0.3') + println('Translating C to V will be available in V 0.3 (January)') return } else if 'search' in commands || 'install' in commands || 'update' in commands || 'remove' in commands { diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index 793f6f27b9..3be78539a2 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -77,6 +77,16 @@ fn (g mut CGen) genln(s string) { } } +// same as `set_placeholder(0, s)`, but faster +fn (g mut CGen) prepend_to_statement(s string) { + if g.is_tmp { + g.tmp_line = s + g.tmp_line + return + } + g.lines << s + g.prev_line = g.cur_line +} + fn (g mut CGen) gen(s string) { if g.nogen || g.pass != .main { return @@ -160,6 +170,10 @@ fn (g mut CGen) set_placeholder(pos int, val string) { if g.nogen || g.pass != .main { return } + //if pos == 0 { + //g.prepend_to_statement(val) + //return + //} // g.lines.set(pos, val) if g.is_tmp { left := g.tmp_line[..pos] diff --git a/vlib/compiler/gen_c.v b/vlib/compiler/gen_c.v index ceb3276b3d..fea88d4d1d 100644 --- a/vlib/compiler/gen_c.v +++ b/vlib/compiler/gen_c.v @@ -1,3 +1,7 @@ +// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. + module compiler import strings @@ -6,26 +10,18 @@ const ( dot_ptr = '->' ) -/* -fn (p mut Parser) gen_or_else(pos int) string { -} -*/ - - // returns the type of the new variable fn (p mut Parser) gen_var_decl(name string, is_static bool) string { - // Generate expression to tmp because we need its type first - // `[typ] [name] = bool_expression();` - pos := p.cgen.add_placeholder() p.is_var_decl = true mut typ := p.bool_expression() + //mut typ, expr := p.tmp_expr() p.is_var_decl = false if typ.starts_with('...') { typ = typ[3..] } //p.gen('/*after expr*/') // Option check ? or { or_else := p.tok == .key_orelse if or_else { - return p.gen_handle_option_or_else(typ, name, pos) + return p.gen_handle_option_or_else(typ, name, 0) } gen_name := p.table.var_cgen_name(name) mut nt_gen := p.table.cgen_name_type_pair(gen_name, typ) @@ -35,7 +31,7 @@ fn (p mut Parser) gen_var_decl(name string, is_static bool) string { } else if typ.starts_with('[') && typ[ typ.len-1 ] != `*` { // a fixed_array initializer, like `v := [1.1, 2.2]!!` // ... should translate to the following in C `f32 v[2] = {1.1, 2.2};` - initializer := p.cgen.cur_line[pos..] + initializer := p.cgen.cur_line if initializer.len > 0 { p.cgen.resetln(' = {' + initializer.all_after('{') ) } else if initializer.len == 0 { @@ -46,7 +42,10 @@ fn (p mut Parser) gen_var_decl(name string, is_static bool) string { if is_static { nt_gen = 'static $nt_gen' } - p.cgen.set_placeholder(pos, nt_gen) + // Now that we know the type, prepend it + // `[typ] [name] = bool_expression();` + //p.cgen.prepend_to_statement(nt_gen) + p.cgen.set_placeholder(0, nt_gen) return typ }