Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
- add hd and tl
- add if for better efficency
  • Loading branch information
linsyking committed Apr 6, 2023
1 parent 76e58e3 commit 007fdc2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
30 changes: 27 additions & 3 deletions examples/syn.meow
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
true() {"T"}
T() {"T"}

false() {"F"}
F() {"F"}

0() {"0"}

Expand Down Expand Up @@ -116,7 +116,15 @@ in(x,y) {
}

not(x) {
if(x, false(), true())
if(x, F(), T())
}

or(x,y) {
if(x,T(), y)
}

and(x,y) {
if(x,y,F())
}

eq0(x) {
Expand Down Expand Up @@ -261,3 +269,19 @@ fib(x) {
)
)
}

tl(x) {
var en = {
"#$" + enraw(hd(x)) = "#$";
encode(x)
};
decode(en)
}

wd(x) {
if(
or(eq(x, ""), eq(hd(x)," ")),
"",
hd(x) + wd(tl(x))
)
}
63 changes: 58 additions & 5 deletions src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ impl Context {
body: Vec::new(),
}),
),
(
"hd".to_string(),
Symbol::Macro(Macro {
args: vec!["x".to_string()],
body: Vec::new(),
}),
),
(
"tl".to_string(),
Symbol::Macro(Macro {
args: vec!["x".to_string()],
body: Vec::new(),
}),
),
(
"if".to_string(),
Symbol::Macro(Macro {
args: vec!["x".to_string(), "x".to_string(), "x".to_string()],
body: Vec::new(),
}),
),
]),
rules: Vec::new(),
}
Expand All @@ -61,8 +82,8 @@ fn eval_macap(mac: &Macro, args: &Vec<Vec<Tok>>, context: &Box<Context>) -> Stri

fn clear_context(context: &Context) -> Context {
// clear the context
let mut newcontext = Context::new();
newcontext.symbols = context
let mut newcontext = context.clone();
newcontext.symbols = newcontext
.symbols
.iter()
.filter(|(_, v)| match v {
Expand All @@ -71,13 +92,14 @@ fn clear_context(context: &Context) -> Context {
})
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
newcontext.rules.clear();
newcontext
}

fn clear_rules(context: &Context) -> Context {
// clear the context
let mut newcontext = Context::new();
newcontext.symbols = context.symbols.clone();
let mut newcontext = context.clone();
newcontext.rules.clear();
newcontext
}

Expand All @@ -98,7 +120,7 @@ fn apply_rule(x: &String, rules: &Vec<Rule>, context: &mut Box<Context>) -> Stri

fn eval_strict(expr: &mut Vec<Tok>, context: &mut Box<Context>) -> String {
// Take one argument from the expression
// println!("eval strict: {:?}", expr);
// println!("[strict] {:?}", expr);
let first = eat_one_para(expr);
match &first {
Tok::Literal(lit) => lit.clone(),
Expand All @@ -124,6 +146,37 @@ fn eval_strict(expr: &mut Vec<Tok>, context: &mut Box<Context>) -> String {
let y = eval_strict(expr, cleancontext);
x + y.as_str()
}
"hd" => {
// Head of a string
let cleancontext = &mut Box::new(clear_rules(context));
let x = eval_strict(expr, cleancontext);
if x.is_empty() {
String::new()
} else {
x.chars().next().unwrap().to_string()
}
}
"tl" => {
// Tail of a string
let cleancontext = &mut Box::new(clear_rules(context));
let x = eval_strict(expr, cleancontext);
if x.is_empty() {
String::new()
} else {
x.chars().skip(1).collect()
}
}
"if" => {
let cleancontext = &mut Box::new(clear_rules(context));
let x = eval_strict(expr, cleancontext);
let y = &mut eval_lazy(expr, cleancontext);
let z = &mut eval_lazy(expr, cleancontext);
if &x == "T" {
eval_strict(y, cleancontext)
} else {
eval_strict(z, cleancontext)
}
}
_ => {
let sym = context
.symbols
Expand Down
2 changes: 1 addition & 1 deletion src/expr.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Tokz: Tok = {
}

VarName: String = {
r"[a-z0-9_]*" => String::from_str(<>).unwrap(),
r"[a-zA-Z0-9_]*" => String::from_str(<>).unwrap(),
}

Literal: String = {
Expand Down
2 changes: 1 addition & 1 deletion src/parse.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ VarDef: (String, String) = {
}

VarName: String = {
r"[a-z0-9_]*" => String::from_str(<>).unwrap()
r"[a-zA-Z0-9_]*" => String::from_str(<>).unwrap()
}

Literal: String = {
Expand Down

0 comments on commit 007fdc2

Please sign in to comment.