Skip to content

Commit

Permalink
Add if else and rename (_|_) -> (_Y_).
Browse files Browse the repository at this point in the history
Signed-off-by: Slendi <[email protected]>
  • Loading branch information
xslendix committed Oct 6, 2023
1 parent 0a05a49 commit 6cac1cb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Node {

VarSet(String, Box<Node>),
While(Box<Node>, Box<Node>),
IfElse(Box<Node>, Box<Node>, Option<Box<Node>>),
}

impl Debug for Node {
Expand All @@ -24,6 +25,7 @@ impl Debug for Node {
Block(v) => write!(fmt, "Block{:?}", v),
VarSet(name, v) => write!(fmt, "{} = {:?}", name, v),
While(v, block) => write!(fmt, "While({:?})->{:?}", v, block),
IfElse(a, b, c) => write!(fmt, "If({:?})->{:?}->{:?}", a, b, c),
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/gp.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ List<T>: Vec<T> =
Stmt = {
Block,
<Expr> "~~",
"(_|_)" <Expr> <Block> => Box::new(Node::While(<>)),
"(_Y_)" <Expr> <Block> => Box::new(Node::While(<>)),
"(.Y.)" <Expr> <Block> <(r"U+O+H+" <Block>)?> => Box::new(Node::IfElse(<>)),
}

Block: Box<Node> = {
Expand Down Expand Up @@ -72,6 +73,7 @@ Identifier = r"[a-zA-Z_][a-zA-Z0-9_]*";
match {
r"[oO][wW][oO]",
r"[uU][wW][uU]",
r"U+O+H+",
} else {
r"[a-zA-Z_][a-zA-Z0-9_]*",
_,
Expand Down
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum Value {
Integer(i64),
String(String),
Err,
None,
}

impl Value {
Expand All @@ -29,6 +30,7 @@ impl Value {
Integer(x) => *x != 0,
String(x) => x.len() != 0,
Err => false,
None => false,
}
}
}
Expand All @@ -40,6 +42,7 @@ impl Debug for Value {
Integer(v) => write!(fmt, "{:?}", v),
String(v) => write!(fmt, "{:?}", v),
Err => write!(fmt, "Err"),
None => write!(fmt, "None"),
}
}
}
Expand Down Expand Up @@ -100,6 +103,17 @@ impl ast::Node {
}
last_result
}
IfElse(expr, real, fake) => {
if expr.interpret(vars).is_truthy() {
real.interpret(vars)
} else {
if let Some(x) = fake {
x.interpret(vars)
} else {
Value::None
}
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test.gpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a :3 8==D~~

"Create a sum"
sum >///< 8D~~
(_|_) a Nya!
(_Y_) a Nya!
sum >///< sum :3 8==D~~
a >///< a >:3 8=D~~
nya~
Expand Down

0 comments on commit 6cac1cb

Please sign in to comment.