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:
parent
8373ece209
commit
baaa55f196
@ -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 {
|
||||
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 {
|
||||
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
|
||||
@ -666,9 +670,7 @@ pub fn (c mut Checker) match_expr(node mut ast.MatchExpr) table.Type {
|
||||
typ := c.expr(match_expr)
|
||||
typ_sym := c.table.get_type_symbol(typ)
|
||||
// TODO:
|
||||
if typ_sym.kind == .sum_type {
|
||||
|
||||
}
|
||||
if typ_sym.kind == .sum_type {}
|
||||
}
|
||||
c.stmts(block.stmts)
|
||||
// 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_sym := c.table.get_type_symbol(index_type)
|
||||
// println('index expr left=$typ_sym.name $node.pos.line_nr')
|
||||
if typ_sym.kind == .array &&
|
||||
(!(table.type_idx(index_type) in table.number_idxs) && index_type_sym.kind != .enum_) {
|
||||
if typ_sym.kind == .array && (!(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)
|
||||
}
|
||||
else if typ_sym.kind == .map && table.type_idx(index_type) != table.string_type_idx {
|
||||
|
@ -86,7 +86,10 @@ fn (g mut Gen) stmt(node ast.Stmt) {
|
||||
}
|
||||
else {
|
||||
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.definitions.write('$type_sym.name ${name}(')
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
void foo(int a);
|
||||
void User_inc_age(int n);
|
||||
int get_int(string a);
|
||||
bool get_bool();
|
||||
int get_int2();
|
||||
@ -74,6 +75,10 @@ i < 10; i++;
|
||||
int f = TODO_first(nums);
|
||||
}
|
||||
|
||||
void User_inc_age(int n) {
|
||||
u.age += n;
|
||||
}
|
||||
|
||||
int get_int(string a) {
|
||||
return 10;
|
||||
}
|
||||
@ -91,13 +96,17 @@ void myuser() {
|
||||
int x = 1;
|
||||
int q = x | 4100;
|
||||
User user = (User){
|
||||
.age = 10,
|
||||
.age = 30,
|
||||
};
|
||||
int age = user.age + 1;
|
||||
int boo = 2;
|
||||
int boo2 = boo + 1;
|
||||
bool b = age > 0;
|
||||
bool b2 = user.age > 0;
|
||||
User user2 = (User){
|
||||
.age = 20,
|
||||
};
|
||||
user2.age = 20 + boo;
|
||||
}
|
||||
|
||||
multi_return_int_string multi_return() {
|
||||
|
@ -83,6 +83,10 @@ fn foo(a int) {
|
||||
//println(cloned1 == 1)
|
||||
}
|
||||
|
||||
fn (u mut User) inc_age(n int) {
|
||||
u.age += n
|
||||
}
|
||||
|
||||
fn get_int(a string) int {
|
||||
return 10
|
||||
}
|
||||
@ -100,12 +104,14 @@ fn get_int2() int {
|
||||
fn myuser() {
|
||||
x := 1
|
||||
q := x | 0x1004
|
||||
user := User{age:10}
|
||||
user := User{age:30}
|
||||
age := user.age + 1 // crash here
|
||||
boo := 2
|
||||
boo2 := boo+1
|
||||
b := age > 0
|
||||
b2 := user.age > 0
|
||||
mut user2 := User{age:20}
|
||||
user2.age = 20 + boo
|
||||
}
|
||||
|
||||
fn multi_return() (int,string) {
|
||||
|
@ -2,9 +2,9 @@ multi_return_int_string mr_test();
|
||||
int testa();
|
||||
string testb(int a);
|
||||
int testc(int a);
|
||||
int testa();
|
||||
int testb();
|
||||
int testa();
|
||||
int Foo_testa();
|
||||
int Foo_testb();
|
||||
int Bar_testa();
|
||||
|
||||
int main() {
|
||||
Bar b = (Bar){
|
||||
@ -55,17 +55,17 @@ int testc(int a) {
|
||||
return a;
|
||||
}
|
||||
|
||||
int testa() {
|
||||
int Foo_testa() {
|
||||
int a = TODO_testb(f);
|
||||
a = 1;
|
||||
return 4;
|
||||
}
|
||||
|
||||
int testb() {
|
||||
int Foo_testb() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
int testa() {
|
||||
int Bar_testa() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
//
|
||||
qq := s + '\n'
|
||||
//
|
||||
g.strings << s + '\n'
|
||||
// g.string_addr[s] = str_pos
|
||||
g.mov(.eax, 1)
|
||||
|
@ -255,25 +255,20 @@ pub fn (t &Table) known_type(name string) bool {
|
||||
[inline]
|
||||
pub fn (t &Table) array_name(elem_type Type, nr_dims int) string {
|
||||
elem_type_sym := t.get_type_symbol(elem_type)
|
||||
return 'array_${elem_type_sym.name}'
|
||||
+ if type_is_ptr(elem_type) { '_ptr' } else { '' }
|
||||
+ if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
return 'array_${elem_type_sym.name}' + if type_is_ptr(elem_type) { '_ptr' } else { '' } + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
}
|
||||
|
||||
[inline]
|
||||
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)
|
||||
return 'array_fixed_${elem_type_sym.name}_${size}'
|
||||
+ if type_is_ptr(elem_type) { '_ptr' } else { '' }
|
||||
+ if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
return 'array_fixed_${elem_type_sym.name}_${size}' + if type_is_ptr(elem_type) { '_ptr' } else { '' } + if nr_dims > 1 { '_${nr_dims}d' } else { '' }
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (t &Table) map_name(key_type Type, value_type Type) string {
|
||||
key_type_sym := t.get_type_symbol(key_type)
|
||||
value_type_sym := t.get_type_symbol(value_type)
|
||||
return 'map_${key_type_sym.name}_${value_type_sym.name}'
|
||||
+ if type_is_ptr(value_type) { '_ptr' } else { '' }
|
||||
return 'map_${key_type_sym.name}_${value_type_sym.name}' + if type_is_ptr(value_type) { '_ptr' } else { '' }
|
||||
}
|
||||
|
||||
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 {
|
||||
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] {
|
||||
return true
|
||||
}
|
||||
@ -393,8 +391,7 @@ pub fn (t &Table) check(got, expected Type) bool {
|
||||
return true
|
||||
}
|
||||
// allow enum value to be used as int
|
||||
if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) ||
|
||||
(exp_type_sym.is_int() && got_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_) {
|
||||
return true
|
||||
}
|
||||
// TODO
|
||||
|
Loading…
Reference in New Issue
Block a user