Compare commits

...

12 Commits

Author SHA1 Message Date
7397ecc84b Nitpicks 2024-11-18 22:15:52 -06:00
b5ee47d2c8 Drop specific operators as terms, "Binary" or "Unary" ops only
This preps for Order of Operations
2024-11-18 22:02:33 -06:00
782c1c93f0 Drop REPL (George Bush voice) "for now" 2024-11-18 21:49:43 -06:00
488d7b1edb more cleanup 2024-11-18 21:45:10 -06:00
f2b0e38464 Audit other files for desired features 2024-11-18 21:42:17 -06:00
1e2c107c1b Remove no longer desired features from example 2024-11-18 21:36:24 -06:00
c29d463adf Clippy pass 2024-11-18 21:25:06 -06:00
5f57c5aed3 Saner munging 2024-11-18 21:21:25 -06:00
c345ac4fe5 Finish emitter 2024-11-18 21:16:02 -06:00
7f241523d4 Use new looping construct in examples 2024-11-18 10:41:51 -06:00
08cab74fb5 Add some display for each, order of operations won't be correct without context 2024-11-18 10:16:36 -06:00
c9295b5777 Add loop test 2024-11-16 14:29:02 -06:00
9 changed files with 166 additions and 200 deletions

View File

@@ -2,7 +2,6 @@ fun <- ->
do-something() do-something()
do-something-else() do-something-else()
foo <- -> foo <- ->
bar <- -> bar <- ->
baz <- -> baz <- ->
@@ -11,8 +10,7 @@ foo <- ->
foo() ## Inside foo but not in baz or bar foo() ## Inside foo but not in baz or bar
add <- x -> y -> add <- x -> y -> x + y ## Multivariable functions by currying
return x + y ## Multivariable functions by currying
add(x) ## Returns a _function_ add(x) ## Returns a _function_
add(x, y) ## This is equivalent to add(x)(y) add(x, y) ## This is equivalent to add(x)(y)

View File

@@ -28,3 +28,4 @@ i <- 1
loop until i > 10 loop until i > 10
print(i) print(i)
i <- 1 i <- 1
## Loops

View File

@@ -1,24 +1,11 @@
## By the end of the book I want to have an interpreter
## for this little language, maybe a compiler too
print(1 + 1) ## 2 print(1 + 1) ## 2
print("Hello world!") ## Hello world print("Hello world!") ## Hello world
name <- read()
print("Hi there $name") ## Hi there <name>
have <- 10 have <- 10
want <- 11 want <- 11
need <- have - want ## This is subtraction need <- have - want ## This is subtraction
have-want <- 1 ## This is a variable named "have-want" have-want <- 1 ## This is a variable named "have-want"
print(HaVe-WAnt) ## 1 (case doesn't matter)
%%option meaningful-casing
Apples <- 1
print(apples) ## <undef> (or maybe it does)
print("one fish"); print("two fish") ## Two statements on the same line
say-hi <- -> print("Hi from inside a function") say-hi <- -> print("Hi from inside a function")
say-hi() ## Hi from inside a function say-hi() ## Hi from inside a function
@@ -31,8 +18,8 @@ duck <- {
eats? <- food -> food = "bread" eats? <- food -> food = "bread"
} ## Objects created on the fly } ## Objects created on the fly
print duck.eats?("bread") ## true print(duck.eats?("bread")) ## true
print duck.eats?("corn") ## false print(duck.eats?("corn")) ## false
cow <- { cow <- {
talk <- print("Moo") talk <- print("Moo")
@@ -43,15 +30,15 @@ human <- {
eats? <- _ -> true eats? <- _ -> true
} }
print cow.eats?("grass") ## true print(cow.eats?("grass")) ## true
print cow.eats?("corn") ## true print(cow.eats?("corn")) ## true
talk-or-pass-wind <- character -> talk-or-pass-wind <- character ->
if character has talk then if character has talk
character.talk() character.talk()
else else
print("*Fart*") print("*Fart*")
talk-or-pass-wind(duck) ## "Quack" talk-or-pass-wind(duck) ## "Quack"
talk-or-pass-wind(cow) ## "Moo" talk-or-pass-wind(cow) ## "Moo"
talk-or-pass-wind(human) ## "*Fart*" talk-or-pass-wind(human) ## "*Fart*"

View File

@@ -1,11 +1,10 @@
## Obligatory ## Obligatory
fizzbuzz <- -> fizzbuzz <- ->
loop(range(100), x -> loop over x in range(100)
if x % 15 = 0 print("fizzbuzz") if x % 15 = 0 print("fizzbuzz")
elif x % 3 = 0 print("fizz") elif x % 3 = 0 print("fizz")
elif x % 5 = 0 print("buzz") elif x % 5 = 0 print("buzz")
else print(x) else print(x)
)
fizzbuzz() fizzbuzz()

54
peg.txt
View File

@@ -1,54 +0,0 @@
program := __* stmt*
stmt := assignment /
funcall stop /
conditional /
loop /
expr stop
expr := - _ expr /
expr "+" _ expr /
expr "-" - expr /
expr "*" _ expr /
expr "/" _ expr /
"(" _ expr ")" /
['"'] ((!['"'] [_] / #"\""#)*) ['"'] /
funcall /
fundef /
boolean /
id /
num
boolean := ("true" / "false") _
id := letter (letter / digit / ['?'|'.'|'-'])* _
num := (digit+ "."? digit* / "." digit+) _
assignment := id "<-" _ expr stop
funcall := id "(" _ expr ** ("," _) ")" _
funcdef := id? "->" _ block
conditional := if __* elif* __* else? __*
if := "if" _ expr block
elif := "elif" _ expr block
else := "else" _ block
loop := "loop" _ (until / over / block)
until := "until" _ expr block
over := "over"_ id "in" _ expr block
block := expr / indented_block
indented_block := stop indent __* stmt+ dedent
letter := ['A'..='Z'] / ['a'..='z']
digit := ['0'..='9']
stop := __+ / eof
indent := ">>>"
dedent := "<<<"
_ := ['\t'|' ']*
__ := comment? newline / comment &eof
comment := "#" (!newline [_])* &(newline / eof)
newline := "\r\n" \ "\r" \ "\n"
eof := ![_]

View File

@@ -4,7 +4,7 @@ use std::collections::HashSet;
use crate::parser; use crate::parser;
fn munge(s: &str) -> String { fn munge(s: &str) -> String {
s.replace("?", "_INT_").replace("-", "_DASH_") s.replace("?", "p").replace("-", "_")
} }
pub struct LexicalContext<'a> { pub struct LexicalContext<'a> {
@@ -21,7 +21,6 @@ impl <'a> LexicalContext<'a> {
is_tail: false, is_tail: false,
} }
} }
#[allow(dead_code)]
fn new(parent: &'a LexicalContext<'a>) -> Self { fn new(parent: &'a LexicalContext<'a>) -> Self {
LexicalContext { LexicalContext {
parent: Some(parent), parent: Some(parent),
@@ -78,13 +77,6 @@ pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) ->
writeln!(w, "return;")?; writeln!(w, "return;")?;
} }
} }
parser::Stmt::Funcall(call) => {
if ctx.is_tail {
write!(w, "return ")?;
}
emit_expr(w, call, ctx)?;
writeln!(w, ";")?;
}
parser::Stmt::Conditional(if_blocks, else_block) => { parser::Stmt::Conditional(if_blocks, else_block) => {
let mut first_block = true; let mut first_block = true;
for if_block in if_blocks { for if_block in if_blocks {
@@ -107,7 +99,36 @@ pub fn emit(w: &mut dyn Write, stmt: &parser::Stmt, ctx: &mut LexicalContext) ->
writeln!(w, "}}")?; writeln!(w, "}}")?;
} }
} }
parser::Stmt::Loop(_) => todo!() parser::Stmt::Loop(eloop) => {
let is_tail = ctx.is_tail;
ctx.is_tail = false;
match &eloop {
parser::Loop::Over(id, expr, block) => {
write!(w, "for (let {} of ", id)?;
emit_expr(w, expr, ctx)?;
writeln!(w, ") {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, block, &mut block_ctx)?;
writeln!(w, "}}")?;
ctx.is_tail = is_tail;
}
parser::Loop::Until(guarded_block) => {
write!(w, "while (!(")?;
emit_expr(w, &guarded_block.guard, ctx)?;
write!(w, ")) {{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, &guarded_block.block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
parser::Loop::Bare(block) => {
write!(w, "for(;;){{")?;
let mut block_ctx = LexicalContext::new(ctx);
emit_all(w, block, &mut block_ctx)?;
writeln!(w, "}}")?;
}
}
ctx.is_tail = is_tail;
}
} }
Ok(()) Ok(())
} }
@@ -155,42 +176,16 @@ pub fn emit_expr(w: &mut dyn Write, expr: &parser::Expr, ctx: &mut LexicalContex
emit_all(w, body, &mut fun_ctx)?; emit_all(w, body, &mut fun_ctx)?;
write!(w, "}}")?; write!(w, "}}")?;
} }
parser::Expr::Plus(e1, e2) => { parser::Expr::BinaryOp(op, e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " + ")?;
emit_expr(w, e2.as_ref(), ctx)?;
}
parser::Expr::Minus(e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " - ")?;
emit_expr(w, e2.as_ref(), ctx)?;
}
parser::Expr::Mult(e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " * ")?;
emit_expr(w, e2.as_ref(), ctx)?;
}
parser::Expr::Div(e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " / ")?;
emit_expr(w, e2.as_ref(), ctx)?;
}
parser::Expr::Mod(e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " % ")?;
emit_expr(w, e2.as_ref(), ctx)?;
}
parser::Expr::Equal(e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " == ")?;
emit_expr(w, e2.as_ref(), ctx)?;
}
parser::Expr::Relop(op, e1, e2) => {
emit_expr(w, e1.as_ref(), ctx)?; emit_expr(w, e1.as_ref(), ctx)?;
write!(w, " {} ", op)?; write!(w, " {} ", op)?;
emit_expr(w, e2.as_ref(), ctx)?; emit_expr(w, e2.as_ref(), ctx)?;
} }
_ => todo!(), parser::Expr::UnaryOp(op, e) => {
write!(w, "{}(", op)?;
emit_expr(w, e.as_ref(), ctx)?;
write!(w, ")")?;
}
} }
Ok(()) Ok(())
} }

View File

@@ -1,11 +1,5 @@
const print = console.log const print = console.log
function loop(seq, f) {
for (el of seq) {
f(el)
}
}
function range(x) { function range(x) {
const ret = [] const ret = []
for (let i = 0; i < x; i++) { for (let i = 0; i < x; i++) {

View File

@@ -13,32 +13,18 @@ use std::path::PathBuf;
#[clap(author, version, about = "Compiler for Deelang")] #[clap(author, version, about = "Compiler for Deelang")]
struct Cli { struct Cli {
#[clap(help="Specify a file to compile")] #[clap(help="Specify a file to compile")]
file: Option<PathBuf>, file: PathBuf,
#[clap(short, long, help="Emit a parse tree")] #[clap(short, long, help="Emit a parse tree")]
parse: bool, parse: bool,
#[clap(short, long, help="Cross-compile to ECMAScript")] #[clap(short, long, help="Cross compile to ECMAScript")]
ecmascript: bool, ecmascript: bool,
#[clap(long, help="Only run the pre-processor")] #[clap(long, help="Run the preprocessor")]
preprocess: bool, preprocess: bool,
} }
fn repl(cli: &Cli) { fn main() {
let mut toplevel = emitter::LexicalContext::toplevel(); let cli = Cli::parse();
let mut out = io::stdout(); let mut file = File::open(&cli.file).expect("Couldn't read file");
loop {
let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
let tree = parser::parse_stmt(&line);
if cli.parse {
println!("{:#?}", tree);
} else if cli.ecmascript {
emitter::emit(&mut out, &tree, &mut toplevel).ok();
}
};
}
fn script(cli: &Cli) {
let mut file = File::open(cli.file.as_ref().unwrap()).expect("Could not read file");
let mut prgm = String::new(); let mut prgm = String::new();
file.read_to_string(&mut prgm).unwrap(); file.read_to_string(&mut prgm).unwrap();
if cli.preprocess { if cli.preprocess {
@@ -55,11 +41,3 @@ fn script(cli: &Cli) {
emitter::emit_all(&mut out, &tree, &mut toplevel).ok(); emitter::emit_all(&mut out, &tree, &mut toplevel).ok();
} }
} }
fn main() {
let cli = Cli::parse();
match cli.file {
None => repl(&cli),
Some(_) => script(&cli),
}
}

View File

@@ -3,7 +3,6 @@ use std::fmt;
#[derive(Debug,PartialEq,Clone)] #[derive(Debug,PartialEq,Clone)]
pub enum Stmt { pub enum Stmt {
Assignment(String, Expr), Assignment(String, Expr),
Funcall(Expr),
Conditional(Vec<GuardedBlock>, Option<Block>), Conditional(Vec<GuardedBlock>, Option<Block>),
Loop(Loop), Loop(Loop),
BareExpr(Expr), BareExpr(Expr),
@@ -15,14 +14,8 @@ pub enum Expr {
Atom(Atom), Atom(Atom),
Funcall(String, Vec<Expr>), Funcall(String, Vec<Expr>),
Funcdef(Option<String>, Block), Funcdef(Option<String>, Block),
UnaryMinus(Box<Expr>), UnaryOp(String, Box<Expr>),
Plus(Box<Expr>, Box<Expr>), BinaryOp(String, Box<Expr>, Box<Expr>),
Minus(Box<Expr>, Box<Expr>),
Mult(Box<Expr>, Box<Expr>),
Div(Box<Expr>, Box<Expr>),
Mod(Box<Expr>, Box<Expr>),
Equal(Box<Expr>, Box<Expr>),
Relop(String, Box<Expr>, Box<Expr>),
} }
pub type Block = Vec<Stmt>; pub type Block = Vec<Stmt>;
@@ -34,16 +27,6 @@ pub enum Atom {
Bool(bool), Bool(bool),
} }
impl fmt::Display for Atom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Atom::String(a) => write!(f, "\"{}\"", a),
Atom::Num(a) => write!(f, "{}", a),
Atom::Bool(a) => write!(f, "{}", a),
}
}
}
#[derive(Debug,PartialEq,Clone)] #[derive(Debug,PartialEq,Clone)]
pub struct GuardedBlock { pub struct GuardedBlock {
pub guard: Expr, pub guard: Expr,
@@ -63,23 +46,22 @@ peg::parser! {
= __* s:stmt()* { s } = __* s:stmt()* { s }
pub rule stmt() -> Stmt pub rule stmt() -> Stmt
= a:assignment() { a } / = a:assignment() { a } /
f:funcall() stop() { Stmt::Funcall(f) } /
c:conditional() { c } / c:conditional() { c } /
l:_loop() { Stmt::Loop(l) } / l:_loop() { Stmt::Loop(l) } /
e:expr() stop() { Stmt::BareExpr(e) } e:expr() stop() { Stmt::BareExpr(e) }
rule expr() -> Expr = precedence! { rule expr() -> Expr = precedence! {
e1:(@) "=" _ e2:@ { Expr::Equal(Box::new(e1), Box::new(e2)) } e1:(@) "=" _ e2:@ { Expr::BinaryOp("=".to_string(), Box::new(e1), Box::new(e2))}
-- --
e1:(@) r:relop() e2:@ { Expr::Relop(r, Box::new(e1), Box::new(e2)) } e1:(@) r:relop() e2:@ { Expr::BinaryOp(r, Box::new(e1), Box::new(e2)) }
-- --
"-" _ e1:@ { Expr::UnaryMinus(Box::new(e1)) } "-" _ e1:@ { Expr::UnaryOp("-".to_string(), Box::new(e1)) }
-- --
e1:(@) "+" _ e2:@ { Expr::Plus(Box::new(e1), Box::new(e2)) } e1:(@) "+" _ e2:@ { Expr::BinaryOp("+".to_string(), Box::new(e1), Box::new(e2)) }
e1:(@) "-" _ e2:@ { Expr::Minus(Box::new(e1), Box::new(e2)) } e1:(@) "-" _ e2:@ { Expr::BinaryOp("-".to_string(), Box::new(e1), Box::new(e2)) }
-- --
e1:(@) "*" _ e2:@ { Expr::Mult(Box::new(e1), Box::new(e2)) } e1:(@) "*" _ e2:@ { Expr::BinaryOp("*".to_string(), Box::new(e1), Box::new(e2)) }
e1:(@) "/" _ e2:@ { Expr::Div(Box::new(e1), Box::new(e2)) } e1:(@) "/" _ e2:@ { Expr::BinaryOp("/".to_string(), Box::new(e1), Box::new(e2)) }
e1:(@) "%" _ e2:@ { Expr::Mod(Box::new(e1), Box::new(e2)) } e1:(@) "%" _ e2:@ { Expr::BinaryOp("%".to_string(), Box::new(e1), Box::new(e2)) }
-- --
"(" _ e:expr() ")" _ { e } "(" _ e:expr() ")" _ { e }
['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) } ['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) }
@@ -210,9 +192,77 @@ pub fn parse(prgm: &str) -> Vec<Stmt> {
deelang_parser::program(&prgm).unwrap() deelang_parser::program(&prgm).unwrap()
} }
pub fn parse_stmt(stmt: &str) -> Stmt { impl fmt::Display for Stmt {
let stmt = preprocess(stmt); fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
deelang_parser::stmt(&stmt).unwrap() match self {
Stmt::Assignment(id, expr) => writeln!(f, "{} <- {}", id, expr),
Stmt::BareExpr(expr) => writeln!(f, "{}", expr),
Stmt::Conditional(guarded_blocks, else_block) => {
let (if_block, elif_blocks) = guarded_blocks.split_first().unwrap();
write!(f, "if {}", if_block.guard)?;
write_block(f, &if_block.block)?;
for elif_block in elif_blocks {
write!(f, "elif {}", elif_block.guard)?;
write_block(f, &if_block.block)?;
}
if let Some(block) = else_block {
write!(f, "else")?;
write_block(f, block)?;
}
Ok(())
}
Stmt::Loop(loop_inner) => match &loop_inner {
Loop::Bare(block) => {
write!(f, "loop")?;
write_block(f, block)
}
Loop::Over(id, pred, block) => {
write!(f, "loop over {} in {}", id, pred)?;
write_block(f, block)
}
Loop::Until(guarded_block) => {
write!(f, "loop until {}", guarded_block.guard)?;
write_block(f, &guarded_block.block)
}
},
}
}
}
fn write_block (f: &mut fmt::Formatter<'_>, block: &Block) -> fmt::Result {
if block.len() == 1 {
writeln!(f, " {}", block[0])
} else {
writeln!(f)?;
writeln!(f, ">>>")?;
for stmt in block {
write!(f, "{}", stmt)?;
}
writeln!(f, "<<<")
}
}
impl fmt::Display for Expr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expr::Atom(a) => write!(f, "{}", a),
Expr::Id(id) => write!(f, "{}", id),
Expr::BinaryOp(op, e1, e2) => write!(f, "{} {} {}", e1, op, e2),
Expr::UnaryOp(op, e) => write!(f, "{} {}", op, e),
Expr::Funcdef(_arg, _block) => todo!(),
Expr::Funcall(_id, _args) => todo!(),
}
}
}
impl fmt::Display for Atom {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Atom::String(a) => write!(f, "\"{}\"", a),
Atom::Num(a) => write!(f, "{}", a),
Atom::Bool(a) => write!(f, "{}", a),
}
}
} }
@@ -232,8 +282,8 @@ apple <- 1 ## This is too
#[test] #[test]
fn test_funcall() { fn test_funcall() {
let expected = vec![ let expected = vec![
Stmt::Funcall(Expr::Funcall("pear".to_string(), vec![])), Stmt::BareExpr(Expr::Funcall("pear".to_string(), vec![])),
Stmt::Funcall(Expr::Funcall("pear".to_string(), Stmt::BareExpr(Expr::Funcall("pear".to_string(),
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())], vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
)) ))
]; ];
@@ -266,18 +316,22 @@ one <- 3 - 2
four <- (3 - 1) * 2"; four <- (3 - 1) * 2";
let expected = vec![ let expected = vec![
Stmt::Assignment("three".to_string(), Stmt::Assignment("three".to_string(),
Expr::Plus( Expr::BinaryOp(
"+".to_string(),
Box::new(Expr::Atom(Atom::Num(1.0))), Box::new(Expr::Atom(Atom::Num(1.0))),
Box::new(Expr::Atom(Atom::Num(2.0))), Box::new(Expr::Atom(Atom::Num(2.0))),
)), )),
Stmt::Assignment("one".to_string(), Stmt::Assignment("one".to_string(),
Expr::Minus( Expr::BinaryOp(
"-".to_string(),
Box::new(Expr::Atom(Atom::Num(3.0))), Box::new(Expr::Atom(Atom::Num(3.0))),
Box::new(Expr::Atom(Atom::Num(2.0))), Box::new(Expr::Atom(Atom::Num(2.0))),
)), )),
Stmt::Assignment("four".to_string(), Stmt::Assignment("four".to_string(),
Expr::Mult( Expr::BinaryOp(
Box::new(Expr::Minus( "*".to_string(),
Box::new(Expr::BinaryOp(
"-".to_string(),
Box::new(Expr::Atom(Atom::Num(3.0))), Box::new(Expr::Atom(Atom::Num(3.0))),
Box::new(Expr::Atom(Atom::Num(1.0))), Box::new(Expr::Atom(Atom::Num(1.0))),
)), )),
@@ -292,7 +346,8 @@ four <- (3 - 1) * 2";
let prgm = "apple <- pear(x, y) + z"; let prgm = "apple <- pear(x, y) + z";
let expected = vec![ let expected = vec![
Stmt::Assignment("apple".to_string(), Stmt::Assignment("apple".to_string(),
Expr::Plus( Expr::BinaryOp(
"+".to_string(),
Box::new(Expr::Funcall( Box::new(Expr::Funcall(
"pear".to_string(), "pear".to_string(),
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())], vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
@@ -326,8 +381,8 @@ foo <- x -> y -> x * y";
Expr::Funcdef( Expr::Funcdef(
None, None,
vec![ vec![
Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![])), Stmt::BareExpr(Expr::Funcall("bar".to_string(), vec![])),
Stmt::Funcall(Expr::Funcall("baz".to_string(), vec![])), Stmt::BareExpr(Expr::Funcall("baz".to_string(), vec![])),
], ],
) )
), ),
@@ -339,7 +394,8 @@ foo <- x -> y -> x * y";
Stmt::BareExpr(Expr::Funcdef( Stmt::BareExpr(Expr::Funcdef(
Some("y".to_string()), Some("y".to_string()),
vec![ vec![
Stmt::BareExpr(Expr::Mult( Stmt::BareExpr(Expr::BinaryOp(
"*".to_string(),
Box::new(Expr::Id("x".to_string())), Box::new(Expr::Id("x".to_string())),
Box::new(Expr::Id("y".to_string())), Box::new(Expr::Id("y".to_string())),
)) ))
@@ -366,18 +422,30 @@ else
vec![ vec![
GuardedBlock { GuardedBlock {
guard: Expr::Id("foo".to_string()), guard: Expr::Id("foo".to_string()),
block: vec![Stmt::Funcall(Expr::Funcall("bar".to_string(), vec![]))] block: vec![Stmt::BareExpr(Expr::Funcall("bar".to_string(), vec![]))]
}, },
GuardedBlock { GuardedBlock {
guard: Expr::Id("baz".to_string()), guard: Expr::Id("baz".to_string()),
block: vec![Stmt::Funcall(Expr::Funcall("foobar".to_string(), vec![]))], block: vec![Stmt::BareExpr(Expr::Funcall("foobar".to_string(), vec![]))],
}, },
], ],
Some(vec![Stmt::Funcall(Expr::Funcall("quux".to_string(), vec![]))]), Some(vec![Stmt::BareExpr(Expr::Funcall("quux".to_string(), vec![]))]),
)]; )];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected); assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
} }
#[test]
fn test_loop() {
let prgm = r"loop until i > 100 a";
let expected = vec![Stmt::Loop(Loop::Until(GuardedBlock {
guard: Expr::BinaryOp(
">".to_string(),
Box::new(Expr::Id("i".to_string())),
Box::new(Expr::Atom(Atom::Num(100.0))),
),
block: vec![Stmt::BareExpr(Expr::Id("a".to_string()))],
}))];
assert_eq!(deelang_parser::program(prgm).unwrap(), expected);
}
#[test] #[test]
fn test_preprocess() { fn test_preprocess() {
let prgm = r" let prgm = r"