fix recursive functions

This commit is contained in:
2024-10-17 15:53:00 -04:00
parent bf7ce174cd
commit d1082cb159
3 changed files with 126 additions and 88 deletions

View File

@@ -6,6 +6,7 @@ use executor::{Executor, RuntimeError};
use parser::{ParseTree, Parser};
use tokenizer::Tokenizer;
use std::collections::HashMap;
use std::fmt::Display;
use std::io::{Write, Read, BufRead};
use std::fmt;
@@ -104,28 +105,47 @@ impl Display for FunctionType {
}
}
#[derive(Clone, Debug, PartialEq)]
enum Evaluation {
// at this point, it's type is set in stone
Computed(Value),
// at this point, it's type is unknown, and may contradict a variable's type
// or not match the expected value of the expression, this is a runtime error
Uncomputed(Box<ParseTree>),
}
#[derive(Clone, Debug, PartialEq)]
enum Object {
Variable(Evaluation),
Function(Function),
}
#[derive(Clone, Debug, PartialEq)]
pub struct Function {
name: Option<String>,
t: FunctionType,
locals: HashMap<String, Object>,
arg_names: Option<Vec<String>>,
body: Option<Box<ParseTree>>,
}
impl Function {
fn lambda(t: FunctionType, arg_names: Vec<String>, body: Option<Box<ParseTree>>) -> Self {
fn lambda(t: FunctionType, arg_names: Vec<String>, locals: HashMap<String, Object>, body: Option<Box<ParseTree>>) -> Self {
Self {
name: None,
t,
locals,
arg_names: Some(arg_names),
body
}
}
fn named(name: &str, t: FunctionType, arg_names: Option<Vec<String>>, body: Option<Box<ParseTree>>) -> Self {
fn named(name: &str, t: FunctionType, arg_names: Option<Vec<String>>, locals: HashMap<String, Object>, body: Option<Box<ParseTree>>) -> Self {
Self {
name: Some(name.to_string()),
t,
locals,
arg_names,
body
}