Extract ast::Call
This commit is contained in:
parent
eb51dd2702
commit
38659d86aa
16
src/ast.rs
16
src/ast.rs
|
|
@ -6,10 +6,7 @@ pub enum Statement {
|
||||||
target: Location,
|
target: Location,
|
||||||
source: Box<Expression>,
|
source: Box<Expression>,
|
||||||
},
|
},
|
||||||
Call {
|
Call(Call),
|
||||||
callee: Ident,
|
|
||||||
args: Vec<Expression>,
|
|
||||||
},
|
|
||||||
Local {
|
Local {
|
||||||
name: Ident,
|
name: Ident,
|
||||||
init: Option<Box<Expression>>,
|
init: Option<Box<Expression>>,
|
||||||
|
|
@ -31,13 +28,16 @@ pub enum Location {
|
||||||
pub enum Expression {
|
pub enum Expression {
|
||||||
Constant(Constant),
|
Constant(Constant),
|
||||||
Variable(Location),
|
Variable(Location),
|
||||||
Call {
|
Call(Call),
|
||||||
callee: Ident,
|
|
||||||
args: Vec<Expression>,
|
|
||||||
},
|
|
||||||
Function(Function),
|
Function(Function),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Call {
|
||||||
|
pub callee: Ident,
|
||||||
|
pub args: Vec<Expression>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub name: Option<Ident>,
|
pub name: Option<Ident>,
|
||||||
|
|
|
||||||
29
src/run.rs
29
src/run.rs
|
|
@ -1,7 +1,7 @@
|
||||||
use std::{collections::HashMap, fmt::Debug, rc::Rc};
|
use std::{collections::HashMap, fmt::Debug, rc::Rc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{Constant, Expression, Location, Statement},
|
ast::{Call, Constant, Expression, Location, Statement},
|
||||||
types::{Value, ValueInner},
|
types::{Value, ValueInner},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -91,18 +91,23 @@ impl Eval for Location {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Eval for Call {
|
||||||
|
fn eval(&self, scope: &mut Scope) -> Value {
|
||||||
|
let Self { callee, args } = self;
|
||||||
|
let Some(function) = scope.functions.get(callee.as_str()).cloned() else {
|
||||||
|
panic!("attempt to call non-existent function {callee}")
|
||||||
|
};
|
||||||
|
let args = args.iter().map(|arg| arg.eval(scope)).collect();
|
||||||
|
(function.inner)(scope, args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Eval for Expression {
|
impl Eval for Expression {
|
||||||
fn eval(&self, scope: &mut Scope) -> Value {
|
fn eval(&self, scope: &mut Scope) -> Value {
|
||||||
match self {
|
match self {
|
||||||
Expression::Constant(inner) => inner.eval(scope),
|
Expression::Constant(inner) => inner.eval(scope),
|
||||||
Expression::Variable(inner) => inner.eval(scope),
|
Expression::Variable(inner) => inner.eval(scope),
|
||||||
Expression::Call { callee, args } => {
|
Expression::Call(inner) => inner.eval(scope),
|
||||||
let Some(function) = scope.functions.get(callee.as_str()).cloned() else {
|
|
||||||
panic!("attempt to call non-existent function {callee}")
|
|
||||||
};
|
|
||||||
let args = args.iter().map(|arg| arg.eval(scope)).collect();
|
|
||||||
(function.inner)(scope, args)
|
|
||||||
}
|
|
||||||
Expression::Function { .. } => todo!(),
|
Expression::Function { .. } => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -134,12 +139,8 @@ impl Exec for Statement {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Statement::Call { callee, args } => {
|
Statement::Call(call) => {
|
||||||
Expression::Call {
|
call.eval(scope);
|
||||||
callee: callee.clone(),
|
|
||||||
args: args.clone(),
|
|
||||||
}
|
|
||||||
.eval(scope);
|
|
||||||
}
|
}
|
||||||
Statement::Local { .. } => todo!(),
|
Statement::Local { .. } => todo!(),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -149,7 +149,7 @@ fn build_in_scope(parent: &BuildContext<'_>, code: &ast::Function) -> Function {
|
||||||
source: Box::new(build_expression(&scope, source)),
|
source: Box::new(build_expression(&scope, source)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ast::Statement::Call { callee, args } => {
|
ast::Statement::Call(ast::Call { callee, args }) => {
|
||||||
body.push(Statement::Call(Call {
|
body.push(Statement::Call(Call {
|
||||||
callee: Box::new(build_expression(
|
callee: Box::new(build_expression(
|
||||||
&scope,
|
&scope,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user