mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
"none" keyword for optionals + more memory fixes
This commit is contained in:
parent
e40ab547ba
commit
d1500511e6
@ -315,8 +315,9 @@ fn (v &V) type_definitions() string {
|
||||
// sort structs
|
||||
types_sorted := sort_structs(types)
|
||||
// Generate C code
|
||||
return types_to_c(builtin_types,v.table) + '\n//----\n' +
|
||||
res := types_to_c(builtin_types,v.table) + '\n//----\n' +
|
||||
types_to_c(types_sorted, v.table)
|
||||
return res
|
||||
}
|
||||
|
||||
// sort structs by dependant fields
|
||||
|
@ -116,13 +116,10 @@ typedef map map_string;
|
||||
#endif
|
||||
|
||||
//============================== HELPER C MACROS =============================*/
|
||||
|
||||
#define _PUSH(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array__push(arr, &tmp);}
|
||||
#define _PUSH_MANY(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array__push_many(arr, tmp.data, tmp.len);}
|
||||
#define _IN(typ, val, arr) array_##typ##_contains(arr, val)
|
||||
#define _IN_MAP(val, m) map__exists(m, val)
|
||||
//#define ALLOC_INIT(type, ...) (type *)memdup((type[]){ __VA_ARGS__ }, sizeof(type))
|
||||
|
||||
//================================== GLOBALS =================================*/
|
||||
byteptr g_str_buf;
|
||||
int load_so(byteptr);
|
||||
|
@ -33,7 +33,34 @@ mut:
|
||||
//gen_types []string
|
||||
}
|
||||
|
||||
fn (f &Fn) find_var(name string) Var {
|
||||
fn (f &Fn) find_var(name string) ?Var {
|
||||
for i in 0 .. f.var_idx {
|
||||
if f.local_vars[i].name == name {
|
||||
return f.local_vars[i]
|
||||
}
|
||||
}
|
||||
return none
|
||||
}
|
||||
|
||||
fn (p &Parser) find_var_check_new_var(name string) ?Var {
|
||||
for i in 0 .. p.cur_fn.var_idx {
|
||||
if p.cur_fn.local_vars[i].name == name {
|
||||
return p.cur_fn.local_vars[i]
|
||||
}
|
||||
}
|
||||
// A hack to allow `newvar := Foo{ field: newvar }`
|
||||
// Declare the variable so that it can be used in the initialization
|
||||
if name == 'main__' + p.var_decl_name {
|
||||
return Var{
|
||||
name : p.var_decl_name
|
||||
typ : 'voidptr'
|
||||
is_mut : true
|
||||
}
|
||||
}
|
||||
return none
|
||||
}
|
||||
|
||||
fn (f &Fn) find_var2(name string) Var {
|
||||
for i in 0 .. f.var_idx {
|
||||
if f.local_vars[i].name == name {
|
||||
return f.local_vars[i]
|
||||
@ -73,8 +100,10 @@ fn (p mut Parser) mark_var_changed(v Var) {
|
||||
}
|
||||
|
||||
fn (f mut Fn) known_var(name string) bool {
|
||||
v := f.find_var(name)
|
||||
return v.name.len > 0
|
||||
_ := f.find_var(name) or {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fn (f mut Fn) register_var(v Var) {
|
||||
@ -819,9 +848,9 @@ fn (p mut Parser) fn_call_args(f mut Fn) &Fn {
|
||||
}
|
||||
p.check(.key_mut)
|
||||
var_name := p.lit
|
||||
v := p.cur_fn.find_var(var_name)
|
||||
if v.name == '' {
|
||||
v := p.cur_fn.find_var(var_name) or {
|
||||
p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`')
|
||||
exit(1)
|
||||
}
|
||||
if !v.is_changed {
|
||||
p.mark_var_changed(v)
|
||||
|
@ -184,6 +184,17 @@ fn main() {
|
||||
v.run_compiled_executable_and_exit()
|
||||
}
|
||||
|
||||
// TODO remove
|
||||
if v.pref.autofree {
|
||||
println('started freeing v struct')
|
||||
v.table.fns.free()
|
||||
v.table.typesmap.free()
|
||||
v.table.obf_ids.free()
|
||||
v.cgen.lines.free()
|
||||
free(v.cgen)
|
||||
free(v.table)
|
||||
println('done!')
|
||||
}
|
||||
}
|
||||
|
||||
fn (v mut V) compile() {
|
||||
@ -833,7 +844,7 @@ fn new_v(args[]string) &V {
|
||||
show_c_cmd: '-show_c_cmd' in args
|
||||
translated: 'translated' in args
|
||||
is_run: 'run' in args
|
||||
autofree: 'autofree' in args
|
||||
autofree: '-autofree' in args
|
||||
is_repl: is_repl
|
||||
build_mode: build_mode
|
||||
cflags: cflags
|
||||
|
@ -1267,12 +1267,11 @@ fn ($v.name mut $v.typ) $p.cur_fn.name (...) {
|
||||
//p.warn('expecting array got $expr_type')
|
||||
//}
|
||||
// Allow `num = 4` where `num` is an `?int`
|
||||
if p.assigned_type.starts_with('Option_') && expr_type == p.assigned_type.right('Option_'.len) {
|
||||
println('allowing option asss')
|
||||
if p.assigned_type.starts_with('Option_') &&
|
||||
expr_type == p.assigned_type.right('Option_'.len) {
|
||||
expr := p.cgen.cur_line.right(pos)
|
||||
left := p.cgen.cur_line.left(pos)
|
||||
typ := expr_type.replace('Option_', '')
|
||||
//p.cgen.cur_line = left + 'opt_ok($expr)'
|
||||
p.cgen.resetln(left + 'opt_ok($expr, sizeof($typ))')
|
||||
}
|
||||
else if !p.builtin_mod && !p.check_types_no_throw(expr_type, p.assigned_type) {
|
||||
@ -1319,7 +1318,7 @@ fn (p mut Parser) var_decl() {
|
||||
name: name
|
||||
typ: typ
|
||||
is_mut: is_mut
|
||||
is_alloc: p.is_alloc
|
||||
is_alloc: p.is_alloc || typ.starts_with('array_')
|
||||
})
|
||||
//if p.is_alloc { println('REG VAR IS ALLOC $name') }
|
||||
p.var_decl_name = ''
|
||||
@ -1486,15 +1485,8 @@ fn (p mut Parser) name_expr() string {
|
||||
name = p.prepend_mod(name)
|
||||
}
|
||||
// Variable
|
||||
mut v := p.cur_fn.find_var(name)
|
||||
// A hack to allow `newvar := Foo{ field: newvar }`
|
||||
// Declare the variable so that it can be used in the initialization
|
||||
if name == 'main__' + p.var_decl_name {
|
||||
v.name = p.var_decl_name
|
||||
v.typ = 'voidptr'
|
||||
v.is_mut = true
|
||||
}
|
||||
if v.name.len != 0 {
|
||||
for { // TODO remove
|
||||
mut v := p.find_var_check_new_var(name) or { break }
|
||||
if ptr {
|
||||
p.gen('& /*vvar*/ ')
|
||||
}
|
||||
@ -1523,7 +1515,7 @@ fn (p mut Parser) name_expr() string {
|
||||
// error
|
||||
}
|
||||
return typ
|
||||
}
|
||||
} // TODO REMOVE for{}
|
||||
// if known_type || is_c_struct_init || (p.first_pass() && p.peek() == .lcbr) {
|
||||
// known type? int(4.5) or Color.green (enum)
|
||||
if p.table.known_type(name) {
|
||||
@ -1881,7 +1873,7 @@ struct $f.parent_fn {
|
||||
}
|
||||
|
||||
enum IndexType {
|
||||
none
|
||||
noindex
|
||||
str
|
||||
map
|
||||
array
|
||||
@ -1898,7 +1890,7 @@ fn get_index_type(typ string) IndexType {
|
||||
return IndexType.ptr
|
||||
}
|
||||
if typ[0] == `[` { return IndexType.fixed_array }
|
||||
return IndexType.none
|
||||
return IndexType.noindex
|
||||
}
|
||||
|
||||
fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||
@ -2239,7 +2231,14 @@ fn (p mut Parser) factor() string {
|
||||
mut typ := ''
|
||||
tok := p.tok
|
||||
switch tok {
|
||||
case .number:
|
||||
case .key_none:
|
||||
if !p.expected_type.starts_with('Option_') {
|
||||
p.error('need "$p.expected_type" got none')
|
||||
}
|
||||
p.gen('opt_none()')
|
||||
p.check(.key_none)
|
||||
return p.expected_type
|
||||
case Token.number:
|
||||
typ = 'int'
|
||||
// Check if float (`1.0`, `1e+3`) but not if is hexa
|
||||
if (p.lit.contains('.') || (p.lit.contains('e') || p.lit.contains('E'))) &&
|
||||
@ -2367,10 +2366,10 @@ fn (p mut Parser) assoc() string {
|
||||
// println('assoc()')
|
||||
p.next()
|
||||
name := p.check_name()
|
||||
if !p.cur_fn.known_var(name) {
|
||||
var := p.cur_fn.find_var(name) or {
|
||||
p.error('unknown variable `$name`')
|
||||
exit(1)
|
||||
}
|
||||
var := p.cur_fn.find_var(name)
|
||||
p.check(.pipe)
|
||||
p.gen('($var.typ){')
|
||||
mut fields := []string// track the fields user is setting, the rest will be copied from the old object
|
||||
@ -3463,10 +3462,14 @@ fn (p mut Parser) return_st() {
|
||||
else {
|
||||
ph := p.cgen.add_placeholder()
|
||||
p.inside_return_expr = true
|
||||
is_none := p.tok == .key_none
|
||||
p.expected_type = p.cur_fn.typ
|
||||
expr_type := p.bool_expression()
|
||||
p.inside_return_expr = false
|
||||
// Automatically wrap an object inside an option if the function returns an option
|
||||
if p.cur_fn.typ.ends_with(expr_type) && p.cur_fn.typ.starts_with('Option_') {
|
||||
// Automatically wrap an object inside an option if the function
|
||||
// returns an option
|
||||
if p.cur_fn.typ.ends_with(expr_type) && !is_none &&
|
||||
p.cur_fn.typ.starts_with('Option_') {
|
||||
tmp := p.get_tmp()
|
||||
ret := p.cgen.cur_line.right(ph)
|
||||
typ := expr_type.replace('Option_', '')
|
||||
@ -3537,7 +3540,7 @@ fn (p mut Parser) go_statement() {
|
||||
// Method
|
||||
if p.peek() == .dot {
|
||||
var_name := p.lit
|
||||
v := p.cur_fn.find_var(var_name)
|
||||
v := p.cur_fn.find_var(var_name) or { return }
|
||||
p.mark_var_used(v)
|
||||
p.next()
|
||||
p.check(.dot)
|
||||
|
@ -194,7 +194,7 @@ fn (p mut Parser) insert_query(fn_ph int) {
|
||||
p.check(.lpar)
|
||||
var_name := p.check_name()
|
||||
p.check(.rpar)
|
||||
var := p.cur_fn.find_var(var_name)
|
||||
var := p.cur_fn.find_var(var_name) or { return }
|
||||
typ := p.table.find_type(var.typ)
|
||||
mut fields := []Var
|
||||
for i, field in typ.fields {
|
||||
|
@ -78,6 +78,7 @@ mut:
|
||||
is_changed bool
|
||||
scope_level int
|
||||
is_c bool // todo remove once `typ` is `Type`, not string
|
||||
moved bool
|
||||
}
|
||||
|
||||
struct Type {
|
||||
@ -322,6 +323,7 @@ fn (t &Table) find_fn(name string) Fn {
|
||||
if !isnil(f.name.str) {
|
||||
return f
|
||||
}
|
||||
//println('ret find fn')
|
||||
return Fn{}
|
||||
}
|
||||
|
||||
@ -412,6 +414,7 @@ fn (t &Type) find_field(name string) Var {
|
||||
return field
|
||||
}
|
||||
}
|
||||
//println('ret Var{}')
|
||||
return Var{}
|
||||
}
|
||||
|
||||
@ -455,11 +458,16 @@ fn (table &Table) find_method(typ &Type, name string) Fn {
|
||||
// method := typ.find_method(name)
|
||||
t := table.typesmap[typ.name]
|
||||
method := t.find_method(name)
|
||||
if method.name.len == 0 && typ.parent.len > 0 {
|
||||
|
||||
for method in t.methods {
|
||||
if method.name == name {
|
||||
return method
|
||||
}
|
||||
}
|
||||
|
||||
if typ.parent != '' {
|
||||
parent := table.find_type(typ.parent)
|
||||
return parent.find_method(name)
|
||||
// println('parent = $parent.name $res')
|
||||
// return res
|
||||
}
|
||||
return method
|
||||
}
|
||||
@ -472,7 +480,9 @@ fn (t &Type) find_method(name string) Fn {
|
||||
return method
|
||||
}
|
||||
}
|
||||
//println('ret Fn{}')
|
||||
return Fn{}
|
||||
//return none
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -16,9 +16,21 @@ fn err_call(ok bool) ?int {
|
||||
return 42
|
||||
}
|
||||
|
||||
fn ret_none() ?int {
|
||||
//return error('wtf') //none
|
||||
return none
|
||||
}
|
||||
|
||||
fn test_option_for_base_type_without_variable() {
|
||||
val := err_call(true) or {
|
||||
panic(err)
|
||||
}
|
||||
assert val == 42
|
||||
println('hm')
|
||||
val2 := ret_none() or {
|
||||
println('yep')
|
||||
return
|
||||
}
|
||||
println('nice')
|
||||
println(val2)
|
||||
}
|
||||
|
@ -96,6 +96,7 @@ enum Token {
|
||||
key_match
|
||||
key_module
|
||||
key_mut
|
||||
key_none
|
||||
key_return
|
||||
key_select
|
||||
key_sizeof
|
||||
@ -222,6 +223,7 @@ fn build_token_str() []string {
|
||||
s[Token.key_defer] = 'defer'
|
||||
s[Token.key_match] = 'match'
|
||||
s[Token.key_select] = 'select'
|
||||
s[Token.key_none] = 'none'
|
||||
return s
|
||||
}
|
||||
|
||||
|
@ -4,10 +4,13 @@
|
||||
- wrap up orm
|
||||
- fix vorum, migrate to orm
|
||||
- wrap up memory management
|
||||
- remove all compiler memory leaks
|
||||
- fix child <T> function calls
|
||||
+ fix non-ascii rendering in gg (ä, å, etc)
|
||||
- cache all tokens once
|
||||
- enable vfmt
|
||||
- optimize the parser (reduce map lookups)
|
||||
- cache vlib (right now it's re-compiled every time)
|
||||
- fix openssl on older linux distros
|
||||
- chat.vlang.io
|
||||
- rewrite objective c code in v (ui_mac.m)
|
||||
@ -26,9 +29,6 @@
|
||||
+ javascript backend
|
||||
- new playground with a v compiler running in the browser
|
||||
+ o(log n) type lookup
|
||||
- remove all compiler memory leaks
|
||||
- optimize the parser (reduce map lookups)
|
||||
- prebuilt binaries for all platforms
|
||||
- cache vlib (right now it's re-compiled every time)
|
||||
- fix interfaces
|
||||
|
||||
|
@ -155,8 +155,7 @@ fn (m map) bs(query string, start, end int, out voidptr) {
|
||||
fn preorder_keys(node &mapnode, keys mut []string, key_i int) int {
|
||||
mut i := key_i
|
||||
if !node.is_empty {
|
||||
mut a := *keys
|
||||
a[i] = node.key
|
||||
keys[i] = node.key
|
||||
i++
|
||||
}
|
||||
if !isnil(node.left) {
|
||||
|
@ -8,6 +8,7 @@ struct Option {
|
||||
data [255]byte
|
||||
error string
|
||||
ok bool
|
||||
is_none bool
|
||||
}
|
||||
|
||||
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
|
||||
@ -22,6 +23,10 @@ fn opt_ok(data voidptr, size int) Option {
|
||||
return res
|
||||
}
|
||||
|
||||
fn opt_none() Option {
|
||||
return Option{ is_none: true }
|
||||
}
|
||||
|
||||
pub fn error(s string) Option {
|
||||
return Option {
|
||||
error: s
|
||||
|
Loading…
Reference in New Issue
Block a user