Skip to content

Commit

Permalink
Create abstraction function resolve_anonymous_expression().
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivorforce committed Jul 18, 2024
1 parent 064bb41 commit f32f7f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
37 changes: 5 additions & 32 deletions src/interpreter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ mod tests {
use crate::interpreter::runtime::Runtime;
use crate::interpreter::vm::VM;
use crate::parser::parse_expression;
use crate::program::expression_tree::ExpressionTree;
use crate::program::functions::{FunctionHead, FunctionImplementation, FunctionInterface, FunctionLogic, FunctionRepresentation};
use crate::program::generics::TypeForest;
use crate::program::functions::{FunctionHead, FunctionInterface, FunctionLogic, FunctionRepresentation};
use crate::program::module::module_name;
use crate::program::traits::RequirementsAssumption;
use crate::program::types::TypeProto;
use crate::resolver::imperative::ImperativeResolver;
use crate::resolver::imperative_builder::ImperativeBuilder;
use crate::resolver::function::resolve_anonymous_expression;
use crate::transpiler::LanguageContext;

/// This tests the transpiler, interpreter and function calls.
Expand Down Expand Up @@ -193,32 +189,9 @@ mod tests {

let mut scope = runtime.make_scope()?;

let mut builder = ImperativeBuilder {
runtime: &runtime,
types: Box::new(TypeForest::new()),
expression_tree: Box::new(ExpressionTree::new(Uuid::new_v4())),
locals_names: Default::default(),
};

let mut resolver = ImperativeResolver {
return_type: Rc::clone(&function_interface.return_type),
builder,
ambiguities: vec![],
};

let head_expression = resolver.resolve_expression(&parsed_source, &scope)?;
resolver.builder.types.bind(head_expression, &function_interface.return_type)?;
resolver.builder.expression_tree.root = head_expression; // TODO This is kinda dumb; but we can't write into an existing head expression
resolver.resolve_all_ambiguities()?;

let implementation = Box::new(FunctionImplementation {
interface: Rc::clone(&function_interface),
requirements_assumption: RequirementsAssumption::empty(),
expression_tree: resolver.builder.expression_tree,
type_forest: resolver.builder.types,
parameter_locals: vec![],
locals_names: resolver.builder.locals_names,
});
let implementation = resolve_anonymous_expression(
&function_interface, &parsed_source, &scope, &mut runtime
)?;

// TODO We shouldn't need a function head for this.
let dummy_head = FunctionHead::new_static(
Expand Down
34 changes: 33 additions & 1 deletion src/resolver/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::error::RResult;
use crate::interpreter::runtime::Runtime;
use crate::program::allocation::ObjectReference;
use crate::program::expression_tree::ExpressionTree;
use crate::program::functions::{FunctionHead, FunctionImplementation};
use crate::program::functions::{FunctionHead, FunctionImplementation, FunctionInterface};
use crate::program::generics::TypeForest;
use crate::program::traits::{RequirementsAssumption, TraitConformance, TraitConformanceRule};
use crate::resolver::imperative::ImperativeResolver;
Expand Down Expand Up @@ -61,6 +61,38 @@ pub fn resolve_function_body(head: &Rc<FunctionHead>, body: &ast::Expression, sc
}))
}

pub fn resolve_anonymous_expression(interface: &Rc<FunctionInterface>, body: &ast::Expression, scope: &scopes::Scope, runtime: &mut Runtime) -> RResult<Box<FunctionImplementation>> {
// TODO This is almost the same function as the above, with the difference that
// 1) We have no parameters
// 2) We have no requirements
let mut builder = ImperativeBuilder {
runtime: &runtime,
types: Box::new(TypeForest::new()),
expression_tree: Box::new(ExpressionTree::new(Uuid::new_v4())),
locals_names: Default::default(),
};

let mut resolver = ImperativeResolver {
return_type: Rc::clone(&interface.return_type),
builder,
ambiguities: vec![],
};

let head_expression = resolver.resolve_expression(&body, &scope)?;
resolver.builder.types.bind(head_expression, &interface.return_type)?;
resolver.builder.expression_tree.root = head_expression; // TODO This is kinda dumb; but we can't write into an existing head expression
resolver.resolve_all_ambiguities()?;

Ok(Box::new(FunctionImplementation {
interface: Rc::clone(&interface),
requirements_assumption: RequirementsAssumption::empty(),
expression_tree: resolver.builder.expression_tree,
type_forest: resolver.builder.types,
parameter_locals: vec![],
locals_names: resolver.builder.locals_names,
}))
}

fn add_conformances_to_scope(scope: &mut scopes::Scope, granted_requirements: &Vec<Rc<TraitConformance>>) -> RResult<()> {
// TODO Register generic types as variables so they can be referenced in the function

Expand Down

0 comments on commit f32f7f4

Please sign in to comment.