compiler: force custom struct .str() methods to be defined public
parent
f286387647
commit
3486118ab7
|
@ -341,11 +341,11 @@ mut:
|
||||||
b []Test2
|
b []Test2
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (t Test2) str() string {
|
pub fn (t Test2) str() string {
|
||||||
return '{$t.one $t.two}'
|
return '{$t.one $t.two}'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (t Test) str() string {
|
pub fn (t Test) str() string {
|
||||||
return '{$t.a $t.b}'
|
return '{$t.a $t.b}'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ struct CFlag{
|
||||||
value string // eg. /path/to/include
|
value string // eg. /path/to/include
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &CFlag) str() string {
|
pub fn (c &CFlag) str() string {
|
||||||
return 'CFlag{ name: "$c.name" value: "$c.value" mod: "$c.mod" os: "$c.os" }'
|
return 'CFlag{ name: "$c.name" value: "$c.value" mod: "$c.mod" os: "$c.os" }'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -199,7 +199,7 @@ mut:
|
||||||
last_nl_pos int
|
last_nl_pos int
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (s ScannerPos) str() string {
|
pub fn (s ScannerPos) str() string {
|
||||||
return 'ScannerPos{ ${s.pos:5d} , ${s.line_nr:5d} , ${s.last_nl_pos:5d} }'
|
return 'ScannerPos{ ${s.pos:5d} , ${s.line_nr:5d} , ${s.last_nl_pos:5d} }'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -314,7 +314,7 @@ fn (p mut Parser) gen_array_str(typ Type) {
|
||||||
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
p.error('cant print ${elm_type}[], unhandled print of ${elm_type}')
|
||||||
}
|
}
|
||||||
p.v.vgen_buf.writeln('
|
p.v.vgen_buf.writeln('
|
||||||
fn (a $typ.name) str() string {
|
pub fn (a $typ.name) str() string {
|
||||||
mut sb := strings.new_builder(a.len * 3)
|
mut sb := strings.new_builder(a.len * 3)
|
||||||
sb.write("[")
|
sb.write("[")
|
||||||
for i, elm in a {
|
for i, elm in a {
|
||||||
|
@ -342,7 +342,7 @@ fn (p mut Parser) gen_struct_str(typ Type) {
|
||||||
})
|
})
|
||||||
|
|
||||||
mut sb := strings.new_builder(typ.fields.len * 20)
|
mut sb := strings.new_builder(typ.fields.len * 20)
|
||||||
sb.writeln('fn (a $typ.name) str() string {\nreturn')
|
sb.writeln('pub fn (a $typ.name) str() string {\nreturn')
|
||||||
sb.writeln("'{")
|
sb.writeln("'{")
|
||||||
for field in typ.fields {
|
for field in typ.fields {
|
||||||
sb.writeln('\t$field.name: $' + 'a.${field.name}')
|
sb.writeln('\t$field.name: $' + 'a.${field.name}')
|
||||||
|
@ -366,7 +366,7 @@ fn (p mut Parser) gen_varg_str(typ Type) {
|
||||||
p.gen_struct_str(elm_type2)
|
p.gen_struct_str(elm_type2)
|
||||||
}
|
}
|
||||||
p.v.vgen_buf.writeln('
|
p.v.vgen_buf.writeln('
|
||||||
fn (a $typ.name) str() string {
|
pub fn (a $typ.name) str() string {
|
||||||
mut sb := strings.new_builder(a.len * 3)
|
mut sb := strings.new_builder(a.len * 3)
|
||||||
sb.write("[")
|
sb.write("[")
|
||||||
for i, elm in a {
|
for i, elm in a {
|
||||||
|
|
|
@ -60,7 +60,7 @@ const (
|
||||||
MainFn = Fn{ name: 'main' }
|
MainFn = Fn{ name: 'main' }
|
||||||
)
|
)
|
||||||
|
|
||||||
fn (a []TypeInst) str() string {
|
pub fn (a []TypeInst) str() string {
|
||||||
mut r := []string
|
mut r := []string
|
||||||
for t in a {
|
for t in a {
|
||||||
mut s := ' | '
|
mut s := ' | '
|
||||||
|
@ -254,7 +254,7 @@ fn (p mut Parser) fn_decl() {
|
||||||
}
|
}
|
||||||
// Don't allow modifying types from a different module
|
// Don't allow modifying types from a different module
|
||||||
if !p.first_pass() && !p.builtin_mod && t.mod != p.mod &&
|
if !p.first_pass() && !p.builtin_mod && t.mod != p.mod &&
|
||||||
!p.is_vgen // allow .str()
|
!p.is_vgen // let vgen define methods like .str() on types defined in other modules
|
||||||
{
|
{
|
||||||
//println('T.mod=$T.mod')
|
//println('T.mod=$T.mod')
|
||||||
//println('p.mod=$p.mod')
|
//println('p.mod=$p.mod')
|
||||||
|
@ -297,6 +297,10 @@ fn (p mut Parser) fn_decl() {
|
||||||
if f.name == 'init' && !f.is_method && f.is_public && !p.is_vh {
|
if f.name == 'init' && !f.is_method && f.is_public && !p.is_vh {
|
||||||
p.error('init function cannot be public')
|
p.error('init function cannot be public')
|
||||||
}
|
}
|
||||||
|
// .str() methods
|
||||||
|
if f.is_method && f.name == 'str' && !f.is_public {
|
||||||
|
p.error('.str() methods must be declared as public')
|
||||||
|
}
|
||||||
// C function header def? (fn C.NSMakeRect(int,int,int,int))
|
// C function header def? (fn C.NSMakeRect(int,int,int,int))
|
||||||
is_c := f.name == 'C' && p.tok == .dot
|
is_c := f.name == 'C' && p.tok == .dot
|
||||||
// Just fn signature? only builtin.v + default build mode
|
// Just fn signature? only builtin.v + default build mode
|
||||||
|
|
|
@ -122,7 +122,7 @@ struct TypeNode {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// For debugging types
|
// For debugging types
|
||||||
fn (t Type) str() string {
|
pub fn (t Type) str() string {
|
||||||
mut s := 'type "$t.name" {'
|
mut s := 'type "$t.name" {'
|
||||||
if t.fields.len > 0 {
|
if t.fields.len > 0 {
|
||||||
// s += '\n $t.fields.len fields:\n'
|
// s += '\n $t.fields.len fields:\n'
|
||||||
|
@ -185,7 +185,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// This is used for debugging only
|
// This is used for debugging only
|
||||||
fn (f Fn) str() string {
|
pub fn (f Fn) str() string {
|
||||||
t := Table{}
|
t := Table{}
|
||||||
str_args := f.str_args(t)
|
str_args := f.str_args(t)
|
||||||
return '${f.name}($str_args) $f.typ'
|
return '${f.name}($str_args) $f.typ'
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
struct Foo {
|
struct Foo {
|
||||||
}
|
}
|
||||||
fn (f Foo) str() string { return 'Foo{}' }
|
pub fn (f Foo) str() string { return 'Foo{}' }
|
||||||
|
|
||||||
fn process_foo(foo &Foo) {
|
fn process_foo(foo &Foo) {
|
||||||
println('>process_foo, called for ${foo} === ${*foo}')
|
println('>process_foo, called for ${foo} === ${*foo}')
|
||||||
|
|
|
@ -257,7 +257,7 @@ fn is_key(key string) bool {
|
||||||
return int(key_to_token(key)) > 0
|
return int(key_to_token(key)) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (t TokenKind) str() string {
|
pub fn (t TokenKind) str() string {
|
||||||
return TokenStr[int(t)]
|
return TokenStr[int(t)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,7 +290,7 @@ fn (t []TokenKind) contains(val TokenKind) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (t Token) str() string {
|
pub fn (t Token) str() string {
|
||||||
if t.tok == .number {
|
if t.tok == .number {
|
||||||
return t.lit
|
return t.lit
|
||||||
|
|
||||||
|
|
|
@ -49,15 +49,15 @@ fn mat4(f &f32) Mat4 {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (v Vec3) str() string {
|
pub fn (v Vec3) str() string {
|
||||||
return 'Vec3{ $v.x, $v.y, $v.z }'
|
return 'Vec3{ $v.x, $v.y, $v.z }'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (v Vec2) str() string {
|
pub fn (v Vec2) str() string {
|
||||||
return 'Vec3{ $v.x, $v.y }'
|
return 'Vec3{ $v.x, $v.y }'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (m Mat4) str() string {
|
pub fn (m Mat4) str() string {
|
||||||
mut s := '[ '
|
mut s := '[ '
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
if i != 0 {
|
if i != 0 {
|
||||||
|
|
Loading…
Reference in New Issue