Skip to content

Commit

Permalink
tear down to build up
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-johnson-4 committed Nov 21, 2023
1 parent bdcd8ee commit aa7b1b1
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub enum Rhs {
Literal(String),
Variable(String),
App(Vec<Rhs>),
Lambda(Vec<Rhs>,Vec<Rhs>),
Lambda(Vec<Rhs>,Box<Rhs>),
Poly(Vec<Rhs>),
}
impl std::fmt::Display for Rhs {
Expand All @@ -152,9 +152,9 @@ impl std::fmt::Display for Rhs {
Rhs::Literal(s) => write!(f, "{}", s),
Rhs::Variable(s) => write!(f, "{}", s),
Rhs::App(ps) => write!(f, "({})", ps.iter().map(|l| l.to_string()).collect::<Vec<String>>().join(" ") ),
Rhs::Lambda(ls,rs) => write!(f, "(λ{}.{})",
Rhs::Lambda(ls,r) => write!(f, "(λ{}.{})",
ls.iter().map(|l| l.to_string()).collect::<Vec<String>>().join(" "),
rs.iter().map(|r| r.to_string()).collect::<Vec<String>>().join(" "),
r.to_string(),
),
Rhs::Poly(vs) => write!(f, "({})", vs.iter().map(|v| v.to_string()).collect::<Vec<String>>().join(" | ") ),
}
Expand Down
14 changes: 13 additions & 1 deletion src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ impl Context {
}
}


pub fn eval_rhs(mut context: Context, rhs: &[Rhs]) -> Result<Rhs,String> {
unimplemented!("evaluator::eval_rhs")
}
pub fn eval_parse(context: Context, rule: &str, input: StringSlice) -> Result<Rhs,String> {
unimplemented!("evaluator::eval_parse")
}
pub fn eval_lazy(context: Context, f: Rhs, xs: &[Rhs]) -> Result<Rhs,String> {
unimplemented!("evaluator::eval_lazy")
}

/* TODO
pub fn eval_parse(context: Context, rule: &str, input: StringSlice) -> Result<Rhs,String> {
for rhs in context.globals.get(rule).expect(rule) {
if let Rhs::Lambda(lhs,rhs) = rhs {
Expand Down Expand Up @@ -418,4 +430,4 @@ pub fn destructure_rhs(mut context: Context, lhs: &[Rhs], rhs: &[Rhs]) -> Contex
}
context
}

*/
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn parse_one_rhs(input: StringSlice) -> Result<Rhs,String> {
if let Some((lhs,rhs)) = input.after("λ").split_once(".") {
Result::Ok( Rhs::Lambda(
parse_many_rhs(StringSlice::new(lhs.to_string()))?,
parse_many_rhs(StringSlice::new(rhs.to_string()))?
Box::new( parse_one_rhs(StringSlice::new(rhs.to_string()))? )
) )
} else {
Result::Err( format!("Syntax Error: {}", input) )
Expand Down
21 changes: 21 additions & 0 deletions src/parser_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::collections::HashMap;
//Type-0 Grammar Rules
struct Rule {
string: Vec<Symbol>,
retval: Rhs,
}

//Symbols in Production Rules
Expand Down Expand Up @@ -40,6 +41,26 @@ impl Grammar {
}

pub fn compile_rule(grammar: &mut Grammar, rule_name: String, rule: Rhs) {
match rule {
//App(Vec<Rhs>),
//Poly(Vec<Rhs>),
Rhs::Literal(s) => unimplemented!("compile_rule Literal {}", s),
Rhs::Variable(s) => unimplemented!("compile_rule Variable {}", s),
Rhs::Lambda(lhs,rhs) => {
let mut string = Vec::new();
for (li,l) in lhs.iter().enumerate() {
unimplemented!("compile_rule {}.{} := {}", rule_name, li, l)
}
if !grammar.rules.contains_key(&rule_name) {
grammar.rules.insert(rule_name.clone(), Vec::new());
}
grammar.rules.get_mut(&rule_name).expect("parser_generator::compile_rule grammar.rules.get_mut").push(Rule {
string: string,
retval: *rhs,
});
},
r => unimplemented!("compile_rule unknown {}", r)
}
}

pub fn compile(input: &str) -> Grammar {
Expand Down
2 changes: 2 additions & 0 deletions tests/b_controlflow.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* TODO
use lambda_mountain::{Policy,Rhs};
static mut ONCE_COUNTER: u32 = 0;
Expand Down Expand Up @@ -40,3 +41,4 @@ fn print_match0() {
assert_eq!( p.s_hard("match (A 1) ( (λ(A a).a) (λ(B b).b) )"), "1");
assert_eq!( p.s_hard("match (B 2) ( (λ(A a).a) (λ(B b).b) )"), "2");
}
*/
3 changes: 3 additions & 0 deletions tests/b_lazy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* TODO
use lambda_mountain::Policy;
#[test]
Expand All @@ -7,3 +9,4 @@ fn eval_f0() {
assert_eq!( p.s_hard("(T (f 1))"), "(T 1)" );
}
*/
2 changes: 2 additions & 0 deletions tests/b_pre.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* TODO
use lambda_mountain::Policy;
#[test]
Expand Down Expand Up @@ -50,3 +51,4 @@ fn print_pat3() {
assert_eq!( p.s_soft("\"a\""), "a" );
assert_eq!( p.s_soft("a"), "a" );
}
*/
3 changes: 3 additions & 0 deletions tests/b_prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

/*
use lambda_mountain::Policy;
#[test]
Expand Down Expand Up @@ -40,3 +42,4 @@ fn print_ctx0() {
assert_eq!( p.s_hard("ctx ((x 2)) x"), "(x 2)" );
assert_eq!( p.s_hard("ctx ((x 2) (y 3)) y"), "(y 3)" );
}
*/
2 changes: 2 additions & 0 deletions tests/b_program.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
use lambda_mountain::Policy;
#[test]
Expand Down Expand Up @@ -56,3 +57,4 @@ fn eval_pat3() {
assert_eq!( p.s_hard("p [ a ]"), "a" );
assert_eq!( p.s_hard("p a"), "a" );
}
*/
3 changes: 2 additions & 1 deletion tests/b_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
use lambda_mountain::Policy;
#[test]
Expand Down Expand Up @@ -34,4 +35,4 @@ fn infer_app0() {
assert_eq!( p.s_soft("(λ(: s Int). s) 1"), "(: ((: (λ(: s Int).(: s Int)) (Arrow Int Int)) (: 1 Int)) Int)");
assert!( p.soft("(λ(: s Int). s) 1.2").is_err() );
}

*/

0 comments on commit aa7b1b1

Please sign in to comment.