module scheme import lexer import os import reader // SourceSyntax selects only the frontend. Both frontends execute one dialect. pub enum SourceSyntax as u8 { auto scheme vile } pub fn (mut vm VM) eval_file(path string) !Value { return vm.eval_file_as(path, .auto) } // eval_file_as is a host entry, like eval. Relative host paths use the process // directory. Scheme load resolves relative to the calling source file instead. pub fn (mut vm VM) eval_file_as(path string, syntax SourceSyntax) !Value { vm.ready()! vm.check_entry_control()! function := vm.file_program(path, syntax, '', unsafe { nil }, false)! return vm.run_program(function, vm.next_context()) } // eval_file_in auto-detects the frontend and retains the file's diagnostic path. pub fn (mut vm VM) eval_file_in(path string, environment Value) !Value { vm.ready()! vm.check_entry_control()! space := vm.environment_space(environment)! function := vm.file_program(path, .auto, '', space, false)! return vm.run_program(function, vm.next_context()) } fn source_path(path string, caller string) !string { if path.contains('\x00') { return failure('file name contains NUL') } if path == '' { return failure('file name is empty') } // Preserve parent components until the filesystem follows symlinks. A // lexical abs_path normalization can select a different source file. if os.is_abs_path(path) { return path.clone() } directory := if caller != '' { os.dir(caller) } else { os.getwd() } base := if os.is_abs_path(directory) { directory } else { os.join_path(os.getwd(), directory) } return os.join_path(base, path) } fn source_error(path string, err IError) IError { if is_control_transfer(err) { return err } if err is reader.ReadError { return EvalError{ source_path: path, message: err.message, offset: err.offset, line: err.line, column: err.column } } if err is lexer.LexError { return EvalError{ source_path: path, message: err.message, offset: err.offset, line: err.line, column: err.column } } return EvalError{ source_path: path, message: err.msg() } } @[params] struct SourceInput { text string path string syntax SourceSyntax fold_case bool = true } fn read_source_input(vm &VM, input SourceInput) !reader.Document { if !isnil(vm) && !isnil(vm.owner.bounded_files) { return vm.owner.bounded_files.read_source(vm, input) } return input.read(vm) } // File buffers already have the VM's allocator. Host strings can be reused // when evaluation returns, so completed documents retain their own spelling. fn (input SourceInput) read(vm &VM) !reader.Document { source := if input.path == '' { input.text } else if isnil(vm) { os.read_file(input.path)! } else { vm.read_source_file(input.path)! } options := reader.ReadOptions{ fold_case: input.fold_case, copy_source: input.path == '' } if input.syntax == .vile || (input.syntax == .auto && input.path != '' && os.file_ext(input.path) == '.vile') { return reader.read_alt(source, options) } return reader.read_all(source, options) } fn (mut vm VM) file_program(filename string, syntax SourceSyntax, caller string, space &Namespace, discard bool) !&Function { path := source_path(filename, caller)! doc := read_source_input(vm, path: path, syntax: syntax) or { return source_error(path, err) } return vm.new_source_program(doc, vm.next_context(), space, path, discard) } fn (mut vm VM) install_load_primitive() { function := &Function{ code: unsafe { nil } params: 1 rest: true slot_count: 2 instructions: [bridge_instruction(.load_enter, 0, 0)] } vm.bind_global('load', procedure_value(&Procedure{ mode: .closure, name: 'load', owner: vm.owner, function: function })) } fn (m &Machine) source_path() string { site := m.error_site() return if isnil(site.function) || isnil(site.function.code) { '' } else { site.function.code.source_file(site.origin) } } @[noinline] fn (mut m Machine) enter_load(mut vm VM) ! { path := string_data(m.env.slots[0])! rest := m.env.slots[1] mut space := unsafe { &Namespace(nil) } if rest.tag != .nil { pair := rest.pair_pointer() if pair.cdr.tag != .nil { return failure('load expects one or two arguments') } space = vm.environment_space(pair.car)! } function := vm.file_program(path, .auto, m.source_path(), space, true)! m.truncate(m.base) m.function = function m.env = unsafe { nil } m.ip = 0 }