mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: print: support all the same functionality as println
This commit is contained in:
parent
3eff266eb9
commit
dac304195e
@ -323,7 +323,7 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
|
|||||||
return f.return_type
|
return f.return_type
|
||||||
}
|
}
|
||||||
// println can print anything
|
// println can print anything
|
||||||
if fn_name == 'println' {
|
if fn_name == 'println' || fn_name == 'print' {
|
||||||
c.expected_type = table.string_type
|
c.expected_type = table.string_type
|
||||||
call_expr.args[0].typ = c.expr(call_expr.args[0].expr)
|
call_expr.args[0].typ = c.expr(call_expr.args[0].expr)
|
||||||
return f.return_type
|
return f.return_type
|
||||||
|
@ -2362,7 +2362,13 @@ fn (g mut Gen) method_call(node ast.CallExpr) {
|
|||||||
|
|
||||||
fn (g mut Gen) fn_call(node ast.CallExpr) {
|
fn (g mut Gen) fn_call(node ast.CallExpr) {
|
||||||
mut name := node.name
|
mut name := node.name
|
||||||
is_print := name == 'println'
|
is_print := name == 'println' || name == 'print'
|
||||||
|
print_method := if name == 'println' {
|
||||||
|
'println'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
'print'
|
||||||
|
}
|
||||||
if node.is_c {
|
if node.is_c {
|
||||||
// Skip "C."
|
// Skip "C."
|
||||||
g.is_c_call = true
|
g.is_c_call = true
|
||||||
@ -2384,7 +2390,6 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if is_print && node.args[0].typ != table.string_type_idx {
|
if is_print && node.args[0].typ != table.string_type_idx {
|
||||||
typ := node.args[0].typ
|
typ := node.args[0].typ
|
||||||
mut styp := g.typ(typ)
|
mut styp := g.typ(typ)
|
||||||
@ -2403,17 +2408,17 @@ fn (g mut Gen) fn_call(node ast.CallExpr) {
|
|||||||
// tmps << tmp
|
// tmps << tmp
|
||||||
g.write('string $tmp = ${styp}_str(')
|
g.write('string $tmp = ${styp}_str(')
|
||||||
g.expr(node.args[0].expr)
|
g.expr(node.args[0].expr)
|
||||||
g.writeln('); println($tmp); string_free($tmp); //MEM2 $styp')
|
g.writeln('); ${print_method}($tmp); string_free($tmp); //MEM2 $styp')
|
||||||
}
|
}
|
||||||
else if sym.kind == .enum_ {
|
else if sym.kind == .enum_ {
|
||||||
g.write('println(tos3("')
|
g.write('${print_method}(tos3("')
|
||||||
g.enum_expr(node.args[0].expr)
|
g.enum_expr(node.args[0].expr)
|
||||||
g.write('"))')
|
g.write('"))')
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// `println(int_str(10))`
|
// `println(int_str(10))`
|
||||||
// sym := g.table.get_type_symbol(node.args[0].typ)
|
// sym := g.table.get_type_symbol(node.args[0].typ)
|
||||||
g.write('println(${styp}_str(')
|
g.write('${print_method}(${styp}_str(')
|
||||||
if table.type_is_ptr(typ) {
|
if table.type_is_ptr(typ) {
|
||||||
// dereference
|
// dereference
|
||||||
g.write('*')
|
g.write('*')
|
||||||
|
@ -236,10 +236,15 @@ pub fn (g mut Gen) save_main_fn_addr() {
|
|||||||
g.main_fn_addr = g.buf.len
|
g.main_fn_addr = g.buf.len
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (g mut Gen) gen_print_from_expr(expr ast.Expr) {
|
pub fn (g mut Gen) gen_print_from_expr(expr ast.Expr, newline bool) {
|
||||||
match expr {
|
match expr {
|
||||||
ast.StringLiteral {
|
ast.StringLiteral {
|
||||||
g.gen_print(it.val)
|
if newline {
|
||||||
|
g.gen_print(it.val+'\n')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
g.gen_print(it.val)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
@ -362,7 +367,7 @@ fn (g mut Gen) expr(node ast.Expr) {
|
|||||||
ast.CallExpr {
|
ast.CallExpr {
|
||||||
if it.name == 'println' || it.name == 'print' {
|
if it.name == 'println' || it.name == 'print' {
|
||||||
expr := it.args[0].expr
|
expr := it.args[0].expr
|
||||||
g.gen_print_from_expr(expr)
|
g.gen_print_from_expr(expr, it.name == 'println')
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
g.write('${it.name}(')
|
g.write('${it.name}(')
|
||||||
|
Loading…
Reference in New Issue
Block a user