v.gen.native: initial support for cast statements (#11291)

pull/11295/head
pancake 2021-08-24 14:26:49 +02:00 committed by GitHub
parent 833bf2cf15
commit 57b148032f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 2 deletions

View File

@ -983,6 +983,20 @@ fn (mut g Gen) assign_stmt(node ast.AssignStmt) {
ast.GoExpr { ast.GoExpr {
g.v_error('threads not implemented for the native backend', node.pos) g.v_error('threads not implemented for the native backend', node.pos)
} }
ast.CastExpr {
g.warning('cast expressions are work in progress', right.pos)
match right.typname {
'u64' {
g.allocate_var(name, 8, right.expr.str().int())
}
'int' {
g.allocate_var(name, 4, right.expr.str().int())
}
else {
g.v_error('unsupported cast type $right.typ', node.pos)
}
}
}
else { else {
// dump(node) // dump(node)
g.v_error('unhandled assign_stmt expression: $right.type_name()', right.position()) g.v_error('unhandled assign_stmt expression: $right.type_name()', right.position())

View File

@ -84,7 +84,7 @@ pub fn gen(files []&ast.File, table &ast.Table, out_name string, pref &pref.Pref
g.generate_header() g.generate_header()
for file in files { for file in files {
if file.warnings.len > 0 { if file.warnings.len > 0 {
eprintln('Warning: ${file.warnings[0]}') eprintln('warning: ${file.warnings[0]}')
} }
if file.errors.len > 0 { if file.errors.len > 0 {
g.n_error(file.errors[0].str()) g.n_error(file.errors[0].str())
@ -358,13 +358,21 @@ fn (mut g Gen) stmt(node ast.Stmt) {
// TODO // TODO
// verror('expr') // verror('expr')
} }
ast.CastExpr {
g.mov64(.rax, e0.expr.str().int())
// do the job
}
ast.StringLiteral { ast.StringLiteral {
s = e0.val.str() s = e0.val.str()
g.expr(node.exprs[0]) g.expr(node.exprs[0])
g.mov64(.rax, g.allocate_string(s, 2)) g.mov64(.rax, g.allocate_string(s, 2))
} }
ast.Ident {
g.expr(e0)
eprintln('ident $e0.name')
}
else { else {
g.n_error('unknown return type $e0') g.n_error('unknown return type $e0.type_name()')
} }
} }
// intel specific // intel specific
@ -450,6 +458,20 @@ pub fn (mut g Gen) n_error(s string) {
util.verror('native error', s) util.verror('native error', s)
} }
pub fn (mut g Gen) warning(s string, pos token.Position) {
if g.pref.output_mode == .stdout {
werror := util.formatted_error('warning', s, g.pref.path, pos)
eprintln(werror)
} else {
g.warnings << errors.Warning{
file_path: g.pref.path
pos: pos
reporter: .gen
message: s
}
}
}
pub fn (mut g Gen) v_error(s string, pos token.Position) { pub fn (mut g Gen) v_error(s string, pos token.Position) {
// TODO: store a file index in the Position too, // TODO: store a file index in the Position too,
// so that the file path can be retrieved from the pos, instead // so that the file path can be retrieved from the pos, instead

View File

@ -1,3 +1,10 @@
fn cast_test() {
a := int(1)
assert a == 1
b := u64(2)
assert b == 2
}
fn if_test() { fn if_test() {
mut a := 1 mut a := 1
if a == 1 { if a == 1 {
@ -81,6 +88,7 @@ struct User {
fn main() { fn main() {
if_test() if_test()
cast_test()
loop() loop()
args() args()
// expr() // expr()