pub struct Compiler { /* private fields */ }
Expand description
Type which makes parsing, typechecking and compiling an AST into bytecode
Implementations
sourceimpl Compiler
impl Compiler
sourcepub fn implicit_prelude(self, implicit_prelude: bool) -> Self
pub fn implicit_prelude(self, implicit_prelude: bool) -> Self
Sets whether the implicit prelude should be include when compiling a file using this compiler (default: true)
pub fn set_implicit_prelude(&mut self, implicit_prelude: bool)
sourcepub fn emit_debug_info(self, emit_debug_info: bool) -> Self
pub fn emit_debug_info(self, emit_debug_info: bool) -> Self
Sets whether the compiler should emit debug information such as source maps and variable names. (default: true)
pub fn set_emit_debug_info(&mut self, emit_debug_info: bool)
sourcepub fn run_io(self, run_io: bool) -> Self
pub fn run_io(self, run_io: bool) -> Self
Sets whether IO
expressions are evaluated.
(default: false)
pub fn set_run_io(&mut self, run_io: bool)
sourcepub fn full_metadata(self, full_metadata: bool) -> Self
pub fn full_metadata(self, full_metadata: bool) -> Self
Sets whether full metadata is required (default: false)
pub fn set_full_metadata(&mut self, full_metadata: bool)
pub fn code_map(&self) -> &CodeMap
pub fn update_filemap<S>(&mut self, file: &str, source: S) -> Option<Arc<FileMap>>where
S: Into<String>,
pub fn get_filemap(&self, file: &str) -> Option<&Arc<FileMap>>
pub fn mut_symbols(&mut self) -> &mut Symbols
sourcepub fn parse_expr(
&mut self,
type_cache: &TypeCache<Symbol, ArcType>,
file: &str,
expr_str: &str
) -> StdResult<SpannedExpr<Symbol>, InFile<Error>>
pub fn parse_expr(
&mut self,
type_cache: &TypeCache<Symbol, ArcType>,
file: &str,
expr_str: &str
) -> StdResult<SpannedExpr<Symbol>, InFile<Error>>
Parse expr_str
, returning an expression if successful
sourcepub fn parse_partial_expr(
&mut self,
type_cache: &TypeCache<Symbol, ArcType>,
file: &str,
expr_str: &str
) -> StdResult<SpannedExpr<Symbol>, (Option<SpannedExpr<Symbol>>, InFile<Error>)>
pub fn parse_partial_expr(
&mut self,
type_cache: &TypeCache<Symbol, ArcType>,
file: &str,
expr_str: &str
) -> StdResult<SpannedExpr<Symbol>, (Option<SpannedExpr<Symbol>>, InFile<Error>)>
Parse input
, returning an expression if successful
sourcepub fn typecheck_expr(
&mut self,
vm: &Thread,
file: &str,
expr_str: &str,
expr: &mut SpannedExpr<Symbol>
) -> Result<ArcType>
pub fn typecheck_expr(
&mut self,
vm: &Thread,
file: &str,
expr_str: &str,
expr: &mut SpannedExpr<Symbol>
) -> Result<ArcType>
Parse and typecheck expr_str
returning the typechecked expression and type of the
expression
pub fn typecheck_str(
&mut self,
vm: &Thread,
file: &str,
expr_str: &str,
expected_type: Option<&ArcType>
) -> Result<(SpannedExpr<Symbol>, ArcType)>
sourcepub fn compile_script(
&mut self,
vm: &Thread,
filename: &str,
expr_str: &str,
expr: &SpannedExpr<Symbol>
) -> Result<CompiledModule>
pub fn compile_script(
&mut self,
vm: &Thread,
filename: &str,
expr_str: &str,
expr: &SpannedExpr<Symbol>
) -> Result<CompiledModule>
Compiles expr
into a function which can be added and run by the vm
sourcepub fn compile_to_bytecode<S>(
&mut self,
thread: &Thread,
name: &str,
expr_str: &str,
serializer: S
) -> StdResult<S::Ok, Either<Error, S::Error>>where
S: Serializer,
S::Error: 'static,
pub fn compile_to_bytecode<S>(
&mut self,
thread: &Thread,
name: &str,
expr_str: &str,
serializer: S
) -> StdResult<S::Ok, Either<Error, S::Error>>where
S: Serializer,
S::Error: 'static,
Compiles the source code expr_str
into bytecode serialized using serializer
sourcepub fn load_bytecode<'vm, D>(
&mut self,
thread: &'vm Thread,
name: &str,
deserializer: D
) -> impl Future<Item = (), Error = Error> + 'vmwhere
D: Deserializer<'vm> + 'vm,
D::Error: Send + Sync,
pub fn load_bytecode<'vm, D>(
&mut self,
thread: &'vm Thread,
name: &str,
deserializer: D
) -> impl Future<Item = (), Error = Error> + 'vmwhere
D: Deserializer<'vm> + 'vm,
D::Error: Send + Sync,
Loads bytecode from a Deserializer
and stores it into the module name
.
load_script
is equivalent to compile_to_bytecode
followed by load_bytecode
sourcepub fn extract_metadata(
&mut self,
vm: &Thread,
file: &str,
expr_str: &str
) -> Result<(SpannedExpr<Symbol>, ArcType, Metadata)>
pub fn extract_metadata(
&mut self,
vm: &Thread,
file: &str,
expr_str: &str
) -> Result<(SpannedExpr<Symbol>, ArcType, Metadata)>
Parses and typechecks expr_str
followed by extracting metadata from the created
expression
sourcepub fn load_script(
&mut self,
vm: &Thread,
filename: &str,
input: &str
) -> Result<()>
pub fn load_script(
&mut self,
vm: &Thread,
filename: &str,
input: &str
) -> Result<()>
Compiles input
and if it is successful runs the resulting code and stores the resulting
value in the vm.
If at any point the function fails the resulting error is returned and nothing is added to the VM.
pub fn load_script_async<'vm>(
&mut self,
vm: &'vm Thread,
filename: &str,
input: &str
) -> impl Future<Item = (), Error = Error> + 'vm
sourcepub fn load_file<'vm>(&mut self, vm: &'vm Thread, filename: &str) -> Result<()>
pub fn load_file<'vm>(&mut self, vm: &'vm Thread, filename: &str) -> Result<()>
Loads filename
and compiles and runs its input by calling load_script
pub fn load_file_async<'vm>(
&mut self,
vm: &'vm Thread,
filename: &str
) -> impl Future<Item = (), Error = Error>
sourcepub fn run_expr<'vm, T>(
&mut self,
vm: &'vm Thread,
name: &str,
expr_str: &str
) -> Result<(T, ArcType)>where
T: for<'value> Getable<'vm, 'value> + VmType + Send + 'vm,
pub fn run_expr<'vm, T>(
&mut self,
vm: &'vm Thread,
name: &str,
expr_str: &str
) -> Result<(T, ArcType)>where
T: for<'value> Getable<'vm, 'value> + VmType + Send + 'vm,
Compiles and runs the expression in expr_str
. If successful the value from running the
expression is returned
Examples
Import from gluon’s standard library and evaluate a string
let vm = new_vm();
let (result, _) = Compiler::new()
.run_expr::<String>(
&vm,
"example",
" let string = import! \"std/string.glu\" in string.trim \" Hello world \t\" "
)
.unwrap();
assert_eq!(result, "Hello world");
sourcepub fn run_expr_async<T>(
&mut self,
vm: &Thread,
name: &str,
expr_str: &str
) -> impl Future<Item = (T, ArcType), Error = Error>where
T: for<'vm, 'value> Getable<'vm, 'value> + VmType + Send + 'static,
pub fn run_expr_async<T>(
&mut self,
vm: &Thread,
name: &str,
expr_str: &str
) -> impl Future<Item = (T, ArcType), Error = Error>where
T: for<'vm, 'value> Getable<'vm, 'value> + VmType + Send + 'static,
Compiles and runs the expression in expr_str
. If successful the value from running the
expression is returned
Examples
Import from gluon’s standard library and evaluate a string
let vm = new_vm();
let result = Compiler::new()
.run_expr::<String>(&vm, "example",
" let string = import! \"std/string.glu\" in string.trim \" Hello world \t\" ")
.unwrap();
let expected = ("Hello world".to_string(), Type::string());
assert_eq!(result, expected);
}