string: make replace() clone for now; parser: comptime method
							parent
							
								
									1b36d7a037
								
							
						
					
					
						commit
						f41e2c0a4b
					
				| 
						 | 
					@ -156,7 +156,7 @@ pub fn (s string) replace_once(rep, with string) string {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn (s string) replace(rep, with string) string {
 | 
					pub fn (s string) replace(rep, with string) string {
 | 
				
			||||||
	if s.len == 0 || rep.len == 0 {
 | 
						if s.len == 0 || rep.len == 0 {
 | 
				
			||||||
		return s
 | 
							return s.clone()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// TODO PERF Allocating ints is expensive. Should be a stack array
 | 
						// TODO PERF Allocating ints is expensive. Should be a stack array
 | 
				
			||||||
	// Get locations of all reps within this string
 | 
						// Get locations of all reps within this string
 | 
				
			||||||
| 
						 | 
					@ -172,7 +172,7 @@ pub fn (s string) replace(rep, with string) string {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Dont change the string if there's nothing to replace
 | 
						// Dont change the string if there's nothing to replace
 | 
				
			||||||
	if idxs.len == 0 {
 | 
						if idxs.len == 0 {
 | 
				
			||||||
		return s
 | 
							return s.clone()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Now we know the number of replacements we need to do and we can calc the len of the new string
 | 
						// Now we know the number of replacements we need to do and we can calc the len of the new string
 | 
				
			||||||
	new_len := s.len + idxs.len * (with.len - rep.len)
 | 
						new_len := s.len + idxs.len * (with.len - rep.len)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1158,8 +1158,16 @@ fn (g &Gen) autofree_variable(v ast.Var) string {
 | 
				
			||||||
			else {
 | 
								else {
 | 
				
			||||||
				// NOTE/TODO: assign_stmt multi returns variables have no expr
 | 
									// NOTE/TODO: assign_stmt multi returns variables have no expr
 | 
				
			||||||
				// since the type comes from the called fns return type
 | 
									// since the type comes from the called fns return type
 | 
				
			||||||
				t := typeof(v.expr)
 | 
									/*
 | 
				
			||||||
 | 
									f := v.name[0]
 | 
				
			||||||
 | 
									if
 | 
				
			||||||
 | 
										//!(f >= `a` && f <= `d`) {
 | 
				
			||||||
 | 
										//f != `c` {
 | 
				
			||||||
 | 
										v.name!='cvar_name' {
 | 
				
			||||||
 | 
										t := typeof(v.expr)
 | 
				
			||||||
				return '// other ' + t + '\n'
 | 
									return '// other ' + t + '\n'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									*/
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return g.autofree_var_call('string_free', v)
 | 
							return g.autofree_var_call('string_free', v)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,6 +6,7 @@ module parser
 | 
				
			||||||
import v.ast
 | 
					import v.ast
 | 
				
			||||||
import v.pref
 | 
					import v.pref
 | 
				
			||||||
import v.vmod
 | 
					import v.vmod
 | 
				
			||||||
 | 
					import v.table
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
 | 
						supported_platforms = ['windows', 'mac', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd',
 | 
				
			||||||
| 
						 | 
					@ -189,3 +190,41 @@ fn os_from_string(os string) pref.OS {
 | 
				
			||||||
	// println('bad os $os') // todo panic?
 | 
						// println('bad os $os') // todo panic?
 | 
				
			||||||
	return .linux
 | 
						return .linux
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// `user.$method()` (`method` is a string)
 | 
				
			||||||
 | 
					fn (mut p Parser) comptime_method_call(typ table.Type) {
 | 
				
			||||||
 | 
						p.check(.dollar)
 | 
				
			||||||
 | 
						method_name := p.check_name()
 | 
				
			||||||
 | 
						_ = method_name
 | 
				
			||||||
 | 
						mut j := 0
 | 
				
			||||||
 | 
						sym := p.table.get_type_symbol(typ)
 | 
				
			||||||
 | 
						if sym.kind != .struct_ {
 | 
				
			||||||
 | 
							p.error('not a struct')
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// info := sym.info as table.Struct
 | 
				
			||||||
 | 
						for method in sym.methods {
 | 
				
			||||||
 | 
							if method.return_type != table.void_type {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							/*
 | 
				
			||||||
 | 
							receiver := method.args[0]
 | 
				
			||||||
 | 
							if !p.expr_var.ptr {
 | 
				
			||||||
 | 
								p.error('`$p.expr_var.name` needs to be a reference')
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							amp := if receiver.is_mut && !p.expr_var.ptr { '&' } else { '' }
 | 
				
			||||||
 | 
							if j > 0 {
 | 
				
			||||||
 | 
								p.gen(' else ')
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							p.genln('if (string_eq($method_name, _STR("$method.name")) ) ' + '${typ.name}_$method.name ($amp $p.expr_var.name);')
 | 
				
			||||||
 | 
							*/
 | 
				
			||||||
 | 
							j++
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						p.check(.lpar)
 | 
				
			||||||
 | 
						p.check(.rpar)
 | 
				
			||||||
 | 
						if p.tok.kind == .key_orelse {
 | 
				
			||||||
 | 
							p.check(.key_orelse)
 | 
				
			||||||
 | 
							// p.genln('else {')
 | 
				
			||||||
 | 
							p.check(.lcbr)
 | 
				
			||||||
 | 
							// p.statements()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue