context -> process

This commit is contained in:
Scott Richmond 2024-12-11 15:42:50 -05:00
parent 36c5d125fb
commit f4fbae10e6
2 changed files with 19 additions and 14 deletions

View File

@ -38,7 +38,6 @@
use chumsky::{input::Stream, prelude::*}; use chumsky::{input::Stream, prelude::*};
use rust_embed::Embed; use rust_embed::Embed;
use std::collections::HashMap;
mod spans; mod spans;
@ -57,14 +56,14 @@ use crate::base::*;
mod validator; mod validator;
use crate::validator::*; use crate::validator::*;
mod context; mod process;
use crate::context::*; use crate::process::*;
#[derive(Embed)] #[derive(Embed)]
#[folder = "assets/"] #[folder = "assets/"]
struct Asset; struct Asset;
pub fn prelude<'src>() -> Context<'src> { pub fn prelude<'src>() -> Process<'src> {
let prelude = Asset::get("test_prelude.ld").unwrap().data.into_owned(); let prelude = Asset::get("test_prelude.ld").unwrap().data.into_owned();
// we know for sure Prelude should live through the whole run of the program // we know for sure Prelude should live through the whole run of the program
let leaked = Box::leak(Box::new(prelude)); let leaked = Box::leak(Box::new(prelude));
@ -91,7 +90,7 @@ pub fn prelude<'src>() -> Context<'src> {
let p_ast = Box::leak(Box::new(p_ast.unwrap().0)); let p_ast = Box::leak(Box::new(p_ast.unwrap().0));
let base_pkg = base(); let base_pkg = base();
let mut base_ctx = Context::<'src> { let mut base_ctx = Process::<'src> {
locals: vec![], locals: vec![],
ast: p_ast, ast: p_ast,
prelude: base_pkg, prelude: base_pkg,
@ -119,7 +118,7 @@ pub fn prelude<'src>() -> Context<'src> {
} }
}; };
Context { Process {
locals: vec![], locals: vec![],
ast: &Ast::Nil, ast: &Ast::Nil,
prelude: p_ctx, prelude: p_ctx,
@ -167,10 +166,16 @@ pub fn run(src: &'static str) {
pub fn main() { pub fn main() {
let src = " let src = "
fn foo () -> { fn bar
foo
fn foo {
() -> bar(:foo)
(:bar) -> :done
} }
foo () () () ()
fn bar (_) -> :bar
foo ()
"; ";
run(src); run(src);
// struct_scalpel::print_dissection_info::<value::Value>() // struct_scalpel::print_dissection_info::<value::Value>()

View File

@ -1,7 +1,7 @@
use crate::base::*; use crate::base::*;
use crate::parser::*; use crate::parser::*;
use crate::spans::*; use crate::spans::*;
use crate::value::{Fn, Value}; use crate::value::Value;
use imbl::HashMap; use imbl::HashMap;
use imbl::Vector; use imbl::Vector;
use std::cell::RefCell; use std::cell::RefCell;
@ -28,14 +28,14 @@ impl LErr {
type LResult<'src> = Result<Value<'src>, LErr>; type LResult<'src> = Result<Value<'src>, LErr>;
pub struct Context<'src> { pub struct Process<'src> {
pub locals: Vec<(String, Value<'src>)>, pub locals: Vec<(String, Value<'src>)>,
pub prelude: Vec<(String, Value<'src>)>, pub prelude: Vec<(String, Value<'src>)>,
// pub prelude_ast: &'src Ast, // pub prelude_ast: &'src Ast,
pub ast: &'src Ast, pub ast: &'src Ast,
} }
impl<'src> Context<'src> { impl<'src> Process<'src> {
pub fn resolve(&self, word: &String) -> LResult<'src> { pub fn resolve(&self, word: &String) -> LResult<'src> {
let resolved_local = self.locals.iter().rev().find(|(name, _)| word == name); let resolved_local = self.locals.iter().rev().find(|(name, _)| word == name);
@ -61,7 +61,7 @@ impl<'src> Context<'src> {
} }
} }
pub fn match_eq<T>(&self, x: T, y: T) -> Option<&Context<'src>> pub fn match_eq<T>(&self, x: T, y: T) -> Option<&Process<'src>>
where where
T: PartialEq, T: PartialEq,
{ {
@ -72,7 +72,7 @@ impl<'src> Context<'src> {
} }
} }
pub fn match_pattern(&mut self, patt: &Ast, val: &Value<'src>) -> Option<&Context<'src>> { pub fn match_pattern(&mut self, patt: &Ast, val: &Value<'src>) -> Option<&Process<'src>> {
use Ast::*; use Ast::*;
match (patt, val) { match (patt, val) {
(NilPattern, Value::Nil) => Some(self), (NilPattern, Value::Nil) => Some(self),