vile / scheme / load.v
132 lines · 118 sloc · 4.29 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import lexer
4import os
5import reader
6
7// SourceSyntax selects only the frontend. Both frontends execute one dialect.
8pub enum SourceSyntax as u8 {
9 auto
10 scheme
11 vile
12}
13
14pub fn (mut vm VM) eval_file(path string) !Value {
15 return vm.eval_file_as(path, .auto)
16}
17
18// eval_file_as is a host entry, like eval. Relative host paths use the process
19// directory. Scheme load resolves relative to the calling source file instead.
20pub fn (mut vm VM) eval_file_as(path string, syntax SourceSyntax) !Value {
21 vm.ready()!
22 vm.check_entry_control()!
23 function := vm.file_program(path, syntax, '', unsafe { nil }, false)!
24 return vm.run_program(function, vm.next_context())
25}
26
27// eval_file_in auto-detects the frontend and retains the file's diagnostic path.
28pub fn (mut vm VM) eval_file_in(path string, environment Value) !Value {
29 vm.ready()!
30 vm.check_entry_control()!
31 space := vm.environment_space(environment)!
32 function := vm.file_program(path, .auto, '', space, false)!
33 return vm.run_program(function, vm.next_context())
34}
35
36fn source_path(path string, caller string) !string {
37 if path.contains('\x00') { return failure('file name contains NUL') }
38 if path == '' { return failure('file name is empty') }
39 // Preserve parent components until the filesystem follows symlinks. A
40 // lexical abs_path normalization can select a different source file.
41 if os.is_abs_path(path) { return path.clone() }
42 directory := if caller != '' { os.dir(caller) } else { os.getwd() }
43 base := if os.is_abs_path(directory) { directory } else { os.join_path(os.getwd(), directory) }
44 return os.join_path(base, path)
45}
46
47fn source_error(path string, err IError) IError {
48 if is_control_transfer(err) { return err }
49 if err is reader.ReadError {
50 return EvalError{ source_path: path, message: err.message, offset: err.offset, line: err.line, column: err.column }
51 }
52 if err is lexer.LexError {
53 return EvalError{ source_path: path, message: err.message, offset: err.offset, line: err.line, column: err.column }
54 }
55 return EvalError{ source_path: path, message: err.msg() }
56}
57
58@[params]
59struct SourceInput {
60 text string
61 path string
62 syntax SourceSyntax
63 fold_case bool = true
64}
65
66fn read_source_input(vm &VM, input SourceInput) !reader.Document {
67 if !isnil(vm) && !isnil(vm.owner.bounded_files) {
68 return vm.owner.bounded_files.read_source(vm, input)
69 }
70 return input.read(vm)
71}
72
73// File buffers already have the VM's allocator. Host strings can be reused
74// when evaluation returns, so completed documents retain their own spelling.
75fn (input SourceInput) read(vm &VM) !reader.Document {
76 source := if input.path == '' {
77 input.text
78 } else if isnil(vm) {
79 os.read_file(input.path)!
80 } else {
81 vm.read_source_file(input.path)!
82 }
83 options := reader.ReadOptions{ fold_case: input.fold_case, copy_source: input.path == '' }
84 if input.syntax == .vile || (input.syntax == .auto && input.path != ''
85 && os.file_ext(input.path) == '.vile') {
86 return reader.read_alt(source, options)
87 }
88 return reader.read_all(source, options)
89}
90
91fn (mut vm VM) file_program(filename string, syntax SourceSyntax, caller string, space &Namespace, discard bool) !&Function {
92 path := source_path(filename, caller)!
93 doc := read_source_input(vm, path: path, syntax: syntax) or { return source_error(path, err) }
94 return vm.new_source_program(doc, vm.next_context(), space, path, discard)
95}
96
97fn (mut vm VM) install_load_primitive() {
98 function := &Function{
99 code: unsafe { nil }
100 params: 1
101 rest: true
102 slot_count: 2
103 instructions: [bridge_instruction(.load_enter, 0, 0)]
104 }
105 vm.bind_global('load', procedure_value(&Procedure{ mode: .closure, name: 'load', owner: vm.owner, function: function }))
106}
107
108fn (m &Machine) source_path() string {
109 site := m.error_site()
110 return if isnil(site.function) || isnil(site.function.code) {
111 ''
112 } else {
113 site.function.code.source_file(site.origin)
114 }
115}
116
117@[noinline]
118fn (mut m Machine) enter_load(mut vm VM) ! {
119 path := string_data(m.env.slots[0])!
120 rest := m.env.slots[1]
121 mut space := unsafe { &Namespace(nil) }
122 if rest.tag != .nil {
123 pair := rest.pair_pointer()
124 if pair.cdr.tag != .nil { return failure('load expects one or two arguments') }
125 space = vm.environment_space(pair.car)!
126 }
127 function := vm.file_program(path, .auto, m.source_path(), space, true)!
128 m.truncate(m.base)
129 m.function = function
130 m.env = unsafe { nil }
131 m.ip = 0
132}
133