1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix methods; println

This commit is contained in:
Alexander Medvednikov 2020-03-02 18:26:55 +01:00
parent 8373ece209
commit baaa55f196
7 changed files with 43 additions and 24 deletions

View File

@ -216,6 +216,10 @@ pub fn (c mut Checker) call_expr(call_expr ast.CallExpr) table.Type {
else if !f.is_variadic && call_expr.args.len > f.args.len { else if !f.is_variadic && call_expr.args.len > f.args.len {
c.error('too many arguments in call to `$fn_name` ($call_expr.args.len instead of $f.args.len)', call_expr.pos) c.error('too many arguments in call to `$fn_name` ($call_expr.args.len instead of $f.args.len)', call_expr.pos)
} }
// println can print anything
if fn_name == 'println' {
return f.return_type
}
for i, arg_expr in call_expr.args { for i, arg_expr in call_expr.args {
arg := if f.is_variadic && i >= f.args.len - 1 { f.args[f.args.len - 1] } else { f.args[i] } arg := if f.is_variadic && i >= f.args.len - 1 { f.args[f.args.len - 1] } else { f.args[i] }
c.expected_type = arg.typ c.expected_type = arg.typ
@ -666,9 +670,7 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
typ := c.expr(match_expr) typ := c.expr(match_expr)
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
// TODO: // TODO:
if typ_sym.kind == .sum_type { if typ_sym.kind == .sum_type {}
}
} }
c.stmts(block.stmts) c.stmts(block.stmts)
// If the last statement is an expression, return its type // If the last statement is an expression, return its type
@ -766,8 +768,7 @@ pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
index_type := c.expr(node.index) index_type := c.expr(node.index)
index_type_sym := c.table.get_type_symbol(index_type) index_type_sym := c.table.get_type_symbol(index_type)
// println('index expr left=$typ_sym.name $node.pos.line_nr') // println('index expr left=$typ_sym.name $node.pos.line_nr')
if typ_sym.kind == .array && if typ_sym.kind == .array && (!(table.type_idx(index_type) in table.number_idxs) && index_type_sym.kind != .enum_) {
(!(table.type_idx(index_type) in table.number_idxs) && index_type_sym.kind != .enum_) {
c.error('non-integer index (type `$typ_sym.name`)', node.pos) c.error('non-integer index (type `$typ_sym.name`)', node.pos)
} }
else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx { else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx {

View File

@ -86,7 +86,10 @@ fn (g mut Gen) stmt(node ast.Stmt) {
} }
else { else {
type_sym := g.table.get_type_symbol(it.typ) type_sym := g.table.get_type_symbol(it.typ)
name := it.name.replace('.', '__') mut name := it.name.replace('.', '__')
if it.is_method {
name = g.table.get_type_symbol(it.receiver.typ).name + '_' + name
}
g.write('$type_sym.name ${name}(') g.write('$type_sym.name ${name}(')
g.definitions.write('$type_sym.name ${name}(') g.definitions.write('$type_sym.name ${name}(')
} }

View File

@ -1,4 +1,5 @@
void foo(int a); void foo(int a);
void User_inc_age(int n);
int get_int(string a); int get_int(string a);
bool get_bool(); bool get_bool();
int get_int2(); int get_int2();
@ -74,6 +75,10 @@ i < 10; i++;
int f = TODO_first(nums); int f = TODO_first(nums);
} }
void User_inc_age(int n) {
u.age += n;
}
int get_int(string a) { int get_int(string a) {
return 10; return 10;
} }
@ -91,13 +96,17 @@ void myuser() {
int x = 1; int x = 1;
int q = x | 4100; int q = x | 4100;
User user = (User){ User user = (User){
.age = 10, .age = 30,
}; };
int age = user.age + 1; int age = user.age + 1;
int boo = 2; int boo = 2;
int boo2 = boo + 1; int boo2 = boo + 1;
bool b = age > 0; bool b = age > 0;
bool b2 = user.age > 0; bool b2 = user.age > 0;
User user2 = (User){
.age = 20,
};
user2.age = 20 + boo;
} }
multi_return_int_string multi_return() { multi_return_int_string multi_return() {

View File

@ -83,6 +83,10 @@ fn foo(a int) {
//println(cloned1 == 1) //println(cloned1 == 1)
} }
fn (u mut User) inc_age(n int) {
u.age += n
}
fn get_int(a string) int { fn get_int(a string) int {
return 10 return 10
} }
@ -100,12 +104,14 @@ fn get_int2() int {
fn myuser() { fn myuser() {
x := 1 x := 1
q := x | 0x1004 q := x | 0x1004
user := User{age:10} user := User{age:30}
age := user.age + 1 // crash here age := user.age + 1 // crash here
boo := 2 boo := 2
boo2 := boo+1 boo2 := boo+1
b := age > 0 b := age > 0
b2 := user.age > 0 b2 := user.age > 0
mut user2 := User{age:20}
user2.age = 20 + boo
} }
fn multi_return() (int,string) { fn multi_return() (int,string) {

View File

@ -2,9 +2,9 @@ multi_return_int_string mr_test();
int testa(); int testa();
string testb(int a); string testb(int a);
int testc(int a); int testc(int a);
int testa(); int Foo_testa();
int testb(); int Foo_testb();
int testa(); int Bar_testa();
int main() { int main() {
Bar b = (Bar){ Bar b = (Bar){
@ -55,17 +55,17 @@ int testc(int a) {
return a; return a;
} }
int testa() { int Foo_testa() {
int a = TODO_testb(f); int a = TODO_testb(f);
a = 1; a = 1;
return 4; return 4;
} }
int testb() { int Foo_testb() {
return 4; return 4;
} }
int testa() { int Bar_testa() {
return 4; return 4;
} }

View File

@ -246,6 +246,9 @@ pub fn (g mut Gen) gen_print_from_expr(expr ast.Expr) {
} }
pub fn (g mut Gen) gen_print(s string) { pub fn (g mut Gen) gen_print(s string) {
//
qq := s + '\n'
//
g.strings << s + '\n' g.strings << s + '\n'
// g.string_addr[s] = str_pos // g.string_addr[s] = str_pos
g.mov(.eax, 1) g.mov(.eax, 1)

View File

@ -255,25 +255,20 @@ pub fn (t &Table) known_type(name string) bool {
[inline] [inline]
pub fn (t &Table) array_name(elem_type Type, nr_dims int) string { pub fn (t &Table) array_name(elem_type Type, nr_dims int) string {
elem_type_sym := t.get_type_symbol(elem_type) elem_type_sym := t.get_type_symbol(elem_type)
return 'array_${elem_type_sym.name}' return 'array_${elem_type_sym.name}' + if type_is_ptr(elem_type) { '_ptr' } else { '' } + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
+ if type_is_ptr(elem_type) { '_ptr' } else { '' }
+ if nr_dims > 1 { '_${nr_dims}d' } else { '' }
} }
[inline] [inline]
pub fn (t &Table) array_fixed_name(elem_type Type, size int, nr_dims int) string { pub fn (t &Table) array_fixed_name(elem_type Type, size int, nr_dims int) string {
elem_type_sym := t.get_type_symbol(elem_type) elem_type_sym := t.get_type_symbol(elem_type)
return 'array_fixed_${elem_type_sym.name}_${size}' return 'array_fixed_${elem_type_sym.name}_${size}' + if type_is_ptr(elem_type) { '_ptr' } else { '' } + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
+ if type_is_ptr(elem_type) { '_ptr' } else { '' }
+ if nr_dims > 1 { '_${nr_dims}d' } else { '' }
} }
[inline] [inline]
pub fn (t &Table) map_name(key_type Type, value_type Type) string { pub fn (t &Table) map_name(key_type Type, value_type Type) string {
key_type_sym := t.get_type_symbol(key_type) key_type_sym := t.get_type_symbol(key_type)
value_type_sym := t.get_type_symbol(value_type) value_type_sym := t.get_type_symbol(value_type)
return 'map_${key_type_sym.name}_${value_type_sym.name}' return 'map_${key_type_sym.name}_${value_type_sym.name}' + if type_is_ptr(value_type) { '_ptr' } else { '' }
+ if type_is_ptr(value_type) { '_ptr' } else { '' }
} }
pub fn (t mut Table) find_or_register_map(key_type, value_type Type) int { pub fn (t mut Table) find_or_register_map(key_type, value_type Type) int {
@ -382,6 +377,9 @@ pub fn (t &Table) check(got, expected Type) bool {
if exp_type_sym.kind == .voidptr { if exp_type_sym.kind == .voidptr {
return true return true
} }
// if got_type_sym.kind == .array_fixed {
// return true
// }
if got_type_sym.kind in [.voidptr, .byteptr, .charptr, .int] && exp_type_sym.kind in [.voidptr, .byteptr, .charptr] { if got_type_sym.kind in [.voidptr, .byteptr, .charptr, .int] && exp_type_sym.kind in [.voidptr, .byteptr, .charptr] {
return true return true
} }
@ -393,8 +391,7 @@ pub fn (t &Table) check(got, expected Type) bool {
return true return true
} }
// allow enum value to be used as int // allow enum value to be used as int
if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) || if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) || (exp_type_sym.is_int() && got_type_sym.kind == .enum_) {
(exp_type_sym.is_int() && got_type_sym.kind == .enum_) {
return true return true
} }
// TODO // TODO