mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
507c71ad80
commit
47f9c02331
2
v.v
2
v.v
@ -44,7 +44,7 @@ fn main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
else if 'translate' in commands {
|
else if 'translate' in commands {
|
||||||
println('Translating C to V will be available in V 0.3 (January)')
|
println('Translating C to V will be available in V 0.3')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
else if 'search' in commands || 'install' in commands || 'update' in commands || 'remove' in commands {
|
else if 'search' in commands || 'install' in commands || 'update' in commands || 'remove' in commands {
|
||||||
|
@ -77,16 +77,6 @@ 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) {
|
fn (g mut CGen) gen(s string) {
|
||||||
if g.nogen || g.pass != .main {
|
if g.nogen || g.pass != .main {
|
||||||
return
|
return
|
||||||
@ -170,10 +160,6 @@ fn (g mut CGen) set_placeholder(pos int, val string) {
|
|||||||
if g.nogen || g.pass != .main {
|
if g.nogen || g.pass != .main {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if pos == 0 {
|
|
||||||
//g.prepend_to_statement(val)
|
|
||||||
//return
|
|
||||||
//}
|
|
||||||
// g.lines.set(pos, val)
|
// g.lines.set(pos, val)
|
||||||
if g.is_tmp {
|
if g.is_tmp {
|
||||||
left := g.tmp_line[..pos]
|
left := g.tmp_line[..pos]
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
// 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
|
module compiler
|
||||||
|
|
||||||
import strings
|
import strings
|
||||||
@ -10,18 +6,26 @@ const (
|
|||||||
dot_ptr = '->'
|
dot_ptr = '->'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
fn (p mut Parser) gen_or_else(pos int) string {
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
// returns the type of the new variable
|
// returns the type of the new variable
|
||||||
fn (p mut Parser) gen_var_decl(name string, is_static bool) string {
|
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
|
p.is_var_decl = true
|
||||||
mut typ := p.bool_expression()
|
mut typ := p.bool_expression()
|
||||||
//mut typ, expr := p.tmp_expr()
|
|
||||||
p.is_var_decl = false
|
p.is_var_decl = false
|
||||||
if typ.starts_with('...') { typ = typ[3..] }
|
if typ.starts_with('...') { typ = typ[3..] }
|
||||||
//p.gen('/*after expr*/')
|
//p.gen('/*after expr*/')
|
||||||
// Option check ? or {
|
// Option check ? or {
|
||||||
or_else := p.tok == .key_orelse
|
or_else := p.tok == .key_orelse
|
||||||
if or_else {
|
if or_else {
|
||||||
return p.gen_handle_option_or_else(typ, name, 0)
|
return p.gen_handle_option_or_else(typ, name, pos)
|
||||||
}
|
}
|
||||||
gen_name := p.table.var_cgen_name(name)
|
gen_name := p.table.var_cgen_name(name)
|
||||||
mut nt_gen := p.table.cgen_name_type_pair(gen_name, typ)
|
mut nt_gen := p.table.cgen_name_type_pair(gen_name, typ)
|
||||||
@ -31,7 +35,7 @@ fn (p mut Parser) gen_var_decl(name string, is_static bool) string {
|
|||||||
} else if typ.starts_with('[') && typ[ typ.len-1 ] != `*` {
|
} else if typ.starts_with('[') && typ[ typ.len-1 ] != `*` {
|
||||||
// a fixed_array initializer, like `v := [1.1, 2.2]!!`
|
// a fixed_array initializer, like `v := [1.1, 2.2]!!`
|
||||||
// ... should translate to the following in C `f32 v[2] = {1.1, 2.2};`
|
// ... should translate to the following in C `f32 v[2] = {1.1, 2.2};`
|
||||||
initializer := p.cgen.cur_line
|
initializer := p.cgen.cur_line[pos..]
|
||||||
if initializer.len > 0 {
|
if initializer.len > 0 {
|
||||||
p.cgen.resetln(' = {' + initializer.all_after('{') )
|
p.cgen.resetln(' = {' + initializer.all_after('{') )
|
||||||
} else if initializer.len == 0 {
|
} else if initializer.len == 0 {
|
||||||
@ -42,9 +46,7 @@ fn (p mut Parser) gen_var_decl(name string, is_static bool) string {
|
|||||||
if is_static {
|
if is_static {
|
||||||
nt_gen = 'static $nt_gen'
|
nt_gen = 'static $nt_gen'
|
||||||
}
|
}
|
||||||
// Now that we know the type, prepend it
|
p.cgen.set_placeholder(pos, nt_gen)
|
||||||
// `[typ] [name] = bool_expression();`
|
|
||||||
p.cgen.prepend_to_statement(nt_gen)
|
|
||||||
return typ
|
return typ
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user