V shell scripts
							parent
							
								
									96152510e5
								
							
						
					
					
						commit
						5cd38ec91b
					
				| 
						 | 
					@ -202,7 +202,7 @@ fn (p mut Parser) chash() {
 | 
				
			||||||
	else if hash.contains('embed') {
 | 
						else if hash.contains('embed') {
 | 
				
			||||||
		pos := hash.index('embed') + 5
 | 
							pos := hash.index('embed') + 5
 | 
				
			||||||
		file := hash.right(pos)
 | 
							file := hash.right(pos)
 | 
				
			||||||
		if p.pref.build_mode != BuildMode.default_mode {
 | 
							if p.pref.build_mode != .default_mode {
 | 
				
			||||||
			p.genln('#include $file')
 | 
								p.genln('#include $file')
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -210,10 +210,6 @@ fn (p mut Parser) chash() {
 | 
				
			||||||
		// Move defines on top
 | 
							// Move defines on top
 | 
				
			||||||
		p.cgen.includes << '#$hash'
 | 
							p.cgen.includes << '#$hash'
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else if hash == 'v' {
 | 
					 | 
				
			||||||
		println('v script')
 | 
					 | 
				
			||||||
		//p.v_script = true
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	// Don't parse a non-JS V file (`#-js` flag)
 | 
						// Don't parse a non-JS V file (`#-js` flag)
 | 
				
			||||||
	else if hash == '-js'  {
 | 
						else if hash == '-js'  {
 | 
				
			||||||
		$if js {
 | 
							$if js {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -662,7 +662,12 @@ fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type strin
 | 
				
			||||||
		//println('r=$receiver.typ RT=$receiver_type')
 | 
							//println('r=$receiver.typ RT=$receiver_type')
 | 
				
			||||||
		if receiver.is_mut && !p.expr_var.is_mut {
 | 
							if receiver.is_mut && !p.expr_var.is_mut {
 | 
				
			||||||
			//println('$method_call  recv=$receiver.name recv_mut=$receiver.is_mut')
 | 
								//println('$method_call  recv=$receiver.name recv_mut=$receiver.is_mut')
 | 
				
			||||||
			p.error('`$p.expr_var.name` is immutable, declare it with `mut`')
 | 
								if p.expr_var.is_for_var {
 | 
				
			||||||
 | 
									p.error('`$p.expr_var.name` is immutable, `for` variables' +
 | 
				
			||||||
 | 
										' always are')
 | 
				
			||||||
 | 
								}	 else {
 | 
				
			||||||
 | 
									p.error('`$p.expr_var.name` is immutable, declare it with `mut`')
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if !p.expr_var.is_changed {
 | 
							if !p.expr_var.is_changed {
 | 
				
			||||||
			p.mark_var_changed(p.expr_var)
 | 
								p.mark_var_changed(p.expr_var)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -556,6 +556,15 @@ pub fn (v mut V) add_v_files_to_compile() {
 | 
				
			||||||
	for file in v.get_user_files() {
 | 
						for file in v.get_user_files() {
 | 
				
			||||||
		mut p := v.new_parser_from_file(file)
 | 
							mut p := v.new_parser_from_file(file)
 | 
				
			||||||
		p.parse(.imports)
 | 
							p.parse(.imports)
 | 
				
			||||||
 | 
							if p.v_script {
 | 
				
			||||||
 | 
								v.log('imports0:')
 | 
				
			||||||
 | 
								println(v.table.imports)
 | 
				
			||||||
 | 
								println(v.files)
 | 
				
			||||||
 | 
								p.import_table.register_alias('os', 'os', 0)
 | 
				
			||||||
 | 
								p.table.imports << 'os'
 | 
				
			||||||
 | 
								p.table.register_module('os')
 | 
				
			||||||
 | 
								println('got v script')
 | 
				
			||||||
 | 
							}	
 | 
				
			||||||
		//if p.pref.autofree {		p.scanner.text.free()		free(p.scanner)	}
 | 
							//if p.pref.autofree {		p.scanner.text.free()		free(p.scanner)	}
 | 
				
			||||||
		v.add_parser(p)
 | 
							v.add_parser(p)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -609,12 +618,13 @@ pub fn (v &V) get_builtin_files() []string {
 | 
				
			||||||
pub fn (v &V)  get_user_files() []string {
 | 
					pub fn (v &V)  get_user_files() []string {
 | 
				
			||||||
	mut dir := v.dir
 | 
						mut dir := v.dir
 | 
				
			||||||
	v.log('get_v_files($dir)')
 | 
						v.log('get_v_files($dir)')
 | 
				
			||||||
	// Need to store user files separately, because they have to be added after libs, but we dont know
 | 
						// Need to store user files separately, because they have to be added after
 | 
				
			||||||
	// which libs need to be added yet
 | 
						// libs, but we dont know	which libs need to be added yet
 | 
				
			||||||
	mut user_files := []string
 | 
						mut user_files := []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if v.pref.is_test && v.pref.is_stats {
 | 
						if v.pref.is_test && v.pref.is_stats {
 | 
				
			||||||
		user_files << [v.vroot, 'vlib', 'benchmark', 'tests', 'always_imported.v'].join( os.path_separator )
 | 
							user_files << os.join(v.vroot, 'vlib', 'benchmark', 'tests',
 | 
				
			||||||
 | 
								'always_imported.v')
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	// v volt/slack_test.v: compile all .v files to get the environment
 | 
						// v volt/slack_test.v: compile all .v files to get the environment
 | 
				
			||||||
| 
						 | 
					@ -624,9 +634,9 @@ pub fn (v &V)  get_user_files() []string {
 | 
				
			||||||
	if is_test_with_imports {
 | 
						if is_test_with_imports {
 | 
				
			||||||
		user_files << dir
 | 
							user_files << dir
 | 
				
			||||||
		pos := dir.last_index(os.path_separator)
 | 
							pos := dir.last_index(os.path_separator)
 | 
				
			||||||
		dir = dir.left(pos) + os.path_separator// TODO WHY IS THIS .neEDED?
 | 
							dir = dir.left(pos) + os.path_separator// TODO why is this needed
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if dir.ends_with('.v') {
 | 
						if dir.ends_with('.v') || dir.ends_with('.vsh') {
 | 
				
			||||||
		// Just compile one file and get parent dir
 | 
							// Just compile one file and get parent dir
 | 
				
			||||||
		user_files << dir
 | 
							user_files << dir
 | 
				
			||||||
		dir = dir.all_before(os.path_separator)
 | 
							dir = dir.all_before(os.path_separator)
 | 
				
			||||||
| 
						 | 
					@ -792,7 +802,7 @@ pub fn new_v(args[]string) &V {
 | 
				
			||||||
		*/
 | 
							*/
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	is_test := dir.ends_with('_test.v')
 | 
						is_test := dir.ends_with('_test.v')
 | 
				
			||||||
	is_script := dir.ends_with('.v')
 | 
						is_script := dir.ends_with('.v') || dir.ends_with('.vsh')
 | 
				
			||||||
	if is_script && !os.file_exists(dir) {
 | 
						if is_script && !os.file_exists(dir) {
 | 
				
			||||||
		println('`$dir` does not exist')
 | 
							println('`$dir` does not exist')
 | 
				
			||||||
		exit(1)
 | 
							exit(1)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -148,6 +148,10 @@ fn (v mut V) new_parser(scanner &Scanner, id string) Parser {
 | 
				
			||||||
		vroot: v.vroot
 | 
							vroot: v.vroot
 | 
				
			||||||
		local_vars: [Var{}].repeat(MaxLocalVars)
 | 
							local_vars: [Var{}].repeat(MaxLocalVars)
 | 
				
			||||||
		import_table: v.table.get_file_import_table(id)
 | 
							import_table: v.table.get_file_import_table(id)
 | 
				
			||||||
 | 
							v_script: id.ends_with('.vsh')
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if p.v_script {
 | 
				
			||||||
 | 
							println('new_parser: V script')
 | 
				
			||||||
	}	
 | 
						}	
 | 
				
			||||||
	$if js {
 | 
						$if js {
 | 
				
			||||||
		p.is_js = true
 | 
							p.is_js = true
 | 
				
			||||||
| 
						 | 
					@ -742,7 +746,7 @@ fn (p mut Parser) struct_decl() {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if p.tok == .key_mut {
 | 
							if p.tok == .key_mut {
 | 
				
			||||||
			if is_mut {
 | 
								if is_mut {
 | 
				
			||||||
				p.error('structs can only have one `mut:`, all private key_mut fields have to be grouped')
 | 
									p.error('structs can only have one `mut:`, all private mutable fields have to be grouped')
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			is_mut = true
 | 
								is_mut = true
 | 
				
			||||||
			p.fmt_dec()
 | 
								p.fmt_dec()
 | 
				
			||||||
| 
						 | 
					@ -1928,23 +1932,17 @@ fn (p mut Parser) name_expr() string {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return typ
 | 
							return typ
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// Function (not method btw, methods are handled in dot())
 | 
						// Function (not method btw, methods are handled in `dot()`)
 | 
				
			||||||
	mut f := p.table.find_fn(name) or {
 | 
						mut f := p.table.find_fn_is_script(name, p.v_script) or {
 | 
				
			||||||
		// We are in the second pass, that means this function was not defined, throw an error.
 | 
							// We are in the second pass, that means this function was not defined,
 | 
				
			||||||
 | 
							// throw an error.
 | 
				
			||||||
		if !p.first_pass() {
 | 
							if !p.first_pass() {
 | 
				
			||||||
			// V script? Try os module.
 | 
					 | 
				
			||||||
			// TODO
 | 
					 | 
				
			||||||
			if p.v_script {
 | 
					 | 
				
			||||||
				//name = name.replace('main__', 'os__')
 | 
					 | 
				
			||||||
				//f = p.table.find_fn(name)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			// check for misspelled function / variable / module
 | 
								// check for misspelled function / variable / module
 | 
				
			||||||
			suggested := p.identify_typo(name, p.import_table)
 | 
								suggested := p.identify_typo(name, p.import_table)
 | 
				
			||||||
			if suggested != '' {
 | 
								if suggested != '' {
 | 
				
			||||||
				p.error('undefined: `$name`. did you mean:$suggested')
 | 
									p.error('undefined: `$name`. did you mean:$suggested')
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			// If orig_name is a mod, then printing undefined: `mod` tells us nothing
 | 
								// If orig_name is a mod, then printing undefined: `mod` tells us nothing
 | 
				
			||||||
			// if p.table.known_mod(orig_name) {
 | 
					 | 
				
			||||||
			if p.table.known_mod(orig_name) || p.import_table.known_alias(orig_name) {
 | 
								if p.table.known_mod(orig_name) || p.import_table.known_alias(orig_name) {
 | 
				
			||||||
				name = name.replace('__', '.')
 | 
									name = name.replace('__', '.')
 | 
				
			||||||
				p.error('undefined: `$name`')
 | 
									p.error('undefined: `$name`')
 | 
				
			||||||
| 
						 | 
					@ -1955,6 +1953,8 @@ fn (p mut Parser) name_expr() string {
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			p.next()
 | 
								p.next()
 | 
				
			||||||
			// First pass, the function can be defined later.
 | 
								// First pass, the function can be defined later.
 | 
				
			||||||
 | 
								// Only in const definitions? (since fn bodies are skipped
 | 
				
			||||||
 | 
								// in the first pass).
 | 
				
			||||||
			return 'void'
 | 
								return 'void'
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return 'void'
 | 
							return 'void'
 | 
				
			||||||
| 
						 | 
					@ -3560,6 +3560,8 @@ fn (p mut Parser) for_st() {
 | 
				
			||||||
				typ: var_type
 | 
									typ: var_type
 | 
				
			||||||
				ptr: typ.contains('*')
 | 
									ptr: typ.contains('*')
 | 
				
			||||||
				is_changed: true
 | 
									is_changed: true
 | 
				
			||||||
 | 
									is_mut: false
 | 
				
			||||||
 | 
									is_for_var: true
 | 
				
			||||||
			})
 | 
								})
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -105,6 +105,7 @@ mut:
 | 
				
			||||||
	is_moved        bool
 | 
						is_moved        bool
 | 
				
			||||||
	line_nr         int
 | 
						line_nr         int
 | 
				
			||||||
	token_idx       int // this is a token index, which will be used by error reporting
 | 
						token_idx       int // this is a token index, which will be used by error reporting
 | 
				
			||||||
 | 
						is_for_var      bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct Type {
 | 
					struct Type {
 | 
				
			||||||
| 
						 | 
					@ -362,6 +363,22 @@ fn (t &Table) find_fn(name string) ?Fn {
 | 
				
			||||||
	return none
 | 
						return none
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn (t &Table) find_fn_is_script(name string, is_script bool) ?Fn {
 | 
				
			||||||
 | 
						mut f := t.fns[name]
 | 
				
			||||||
 | 
						if f.name.str != 0 { // TODO
 | 
				
			||||||
 | 
							return f
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						// V script? Try os module.
 | 
				
			||||||
 | 
						if is_script {
 | 
				
			||||||
 | 
							println('trying replace $name')
 | 
				
			||||||
 | 
							f = t.fns[name.replace('main__', 'os__')]
 | 
				
			||||||
 | 
							if f.name.str != 0 {
 | 
				
			||||||
 | 
								return f
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return none
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn (t &Table) known_fn(name string) bool {
 | 
					fn (t &Table) known_fn(name string) bool {
 | 
				
			||||||
	_ = t.find_fn(name) or { return false }
 | 
						_ = t.find_fn(name) or { return false }
 | 
				
			||||||
	return true
 | 
						return true
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										13
									
								
								vlib/os/os.v
								
								
								
								
							
							
						
						
									
										13
									
								
								vlib/os/os.v
								
								
								
								
							| 
						 | 
					@ -4,6 +4,8 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module os
 | 
					module os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import strings
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <sys/stat.h>
 | 
					#include <sys/stat.h>
 | 
				
			||||||
#include <signal.h>
 | 
					#include <signal.h>
 | 
				
			||||||
#include <errno.h>
 | 
					#include <errno.h>
 | 
				
			||||||
| 
						 | 
					@ -864,3 +866,14 @@ pub fn mkdir_all(path string) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO use []string.join once ...string becomes "[]string"
 | 
				
			||||||
 | 
					pub fn join(base string, dirs ...string) string {
 | 
				
			||||||
 | 
						mut path := strings.new_builder(50)
 | 
				
			||||||
 | 
						path.write(base.trim_right('\\/'))
 | 
				
			||||||
 | 
						for d in dirs {
 | 
				
			||||||
 | 
							path.write(os.path_separator)
 | 
				
			||||||
 | 
							path.write(d)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return path.str()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue