mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
native: add printing support for boolean and string variables (#15868)
This commit is contained in:
parent
f338dec5c6
commit
947a1f2c65
@ -3009,6 +3009,34 @@ fn (mut g Gen) init_struct(var Var, init ast.StructInit) {
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut g Gen) convert_bool_to_string(reg Register) {
|
||||
g.cmp_zero(reg)
|
||||
false_label := g.labels.new_label()
|
||||
false_cjmp_addr := g.cjmp(.je)
|
||||
g.labels.patches << LabelPatch{
|
||||
id: false_label
|
||||
pos: false_cjmp_addr
|
||||
}
|
||||
g.println('; jump to label $false_label')
|
||||
|
||||
g.learel(reg, g.allocate_string('true', 3, .rel32))
|
||||
|
||||
end_label := g.labels.new_label()
|
||||
end_jmp_addr := g.jmp(0)
|
||||
g.labels.patches << LabelPatch{
|
||||
id: end_label
|
||||
pos: end_jmp_addr
|
||||
}
|
||||
g.println('; jump to label $end_label')
|
||||
|
||||
g.labels.addrs[false_label] = g.pos()
|
||||
g.println('; label $false_label')
|
||||
g.learel(reg, g.allocate_string('false', 3, .rel32))
|
||||
|
||||
g.labels.addrs[end_label] = g.pos()
|
||||
g.println('; label $end_label')
|
||||
}
|
||||
|
||||
fn (mut g Gen) convert_int_to_string(r1 Register, r2 Register) {
|
||||
if r1 != .rax {
|
||||
g.mov_reg(.rax, r1)
|
||||
|
@ -24,6 +24,12 @@ pub fn (mut g Gen) init_builtins() {
|
||||
}
|
||||
arg_regs: [.rcx, .rdi]
|
||||
}
|
||||
'bool_to_string': BuiltinFn{
|
||||
body: fn (builtin BuiltinFn, mut g Gen) {
|
||||
g.convert_bool_to_string(builtin.arg_regs[0])
|
||||
}
|
||||
arg_regs: [.rax]
|
||||
}
|
||||
'reverse_string': BuiltinFn{
|
||||
body: fn (builtin BuiltinFn, mut g Gen) {
|
||||
g.reverse_string(builtin.arg_regs[0])
|
||||
|
@ -567,6 +567,11 @@ fn (mut g Gen) gen_var_to_string(reg Register, var Var, config VarConfig) {
|
||||
g.lea_var_to_reg(g.get_builtin_arg_reg('int_to_string', 1), buffer)
|
||||
g.call_builtin('int_to_string')
|
||||
g.lea_var_to_reg(reg, buffer)
|
||||
} else if typ.is_bool() {
|
||||
g.mov_var_to_reg(g.get_builtin_arg_reg('bool_to_string', 0), var, config)
|
||||
g.call_builtin('bool_to_string')
|
||||
} else if typ.is_string() {
|
||||
g.mov_var_to_reg(.rax, var, config)
|
||||
} else {
|
||||
g.n_error('int-to-string conversion not implemented for type $typ')
|
||||
}
|
||||
@ -708,6 +713,13 @@ g.expr
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut g Gen) extern_fn_decl(node ast.FnDecl) {
|
||||
// declarations like: fn C.malloc()
|
||||
// TODO: implement extern function calls here
|
||||
// Must store an address where the function label is located in the RelA elf section
|
||||
panic('C. functions are not implemented yet')
|
||||
}
|
||||
|
||||
fn (mut g Gen) fn_decl(node ast.FnDecl) {
|
||||
name := if node.is_method {
|
||||
'${g.table.get_type_name(node.receiver.typ)}.$node.name'
|
||||
@ -723,6 +735,11 @@ fn (mut g Gen) fn_decl(node ast.FnDecl) {
|
||||
if node.is_builtin {
|
||||
g.warning('fn_decl: $name is builtin', node.pos)
|
||||
}
|
||||
if node.no_body {
|
||||
g.extern_fn_decl(node)
|
||||
return
|
||||
}
|
||||
|
||||
g.stack_var_pos = 0
|
||||
g.register_function_address(name)
|
||||
g.labels = &LabelTable{}
|
||||
|
@ -30,6 +30,8 @@ fn test_stderr() {
|
||||
}
|
||||
|
||||
fn test_idents() {
|
||||
// signed integer
|
||||
|
||||
x := 0
|
||||
println(x)
|
||||
|
||||
@ -44,6 +46,22 @@ fn test_idents() {
|
||||
|
||||
b := -456
|
||||
println(b)
|
||||
|
||||
// booleans
|
||||
|
||||
bool_true := true
|
||||
println(bool_true)
|
||||
|
||||
bool_false := false
|
||||
println(bool_false)
|
||||
|
||||
// strings
|
||||
|
||||
str := 'string blah blah blah'
|
||||
println(str)
|
||||
|
||||
unicode := '😀😆😎💻🌎'
|
||||
println(unicode)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -7,4 +7,8 @@ Hello World
|
||||
5
|
||||
-8
|
||||
123
|
||||
-456
|
||||
-456
|
||||
true
|
||||
false
|
||||
string blah blah blah
|
||||
😀😆😎💻🌎
|
Loading…
Reference in New Issue
Block a user