Compare commits
4 Commits
f2b0e38464
...
7397ecc84b
Author | SHA1 | Date | |
---|---|---|---|
7397ecc84b | |||
b5ee47d2c8 | |||
782c1c93f0 | |||
488d7b1edb |
@ -28,3 +28,4 @@ i <- 1
|
||||
loop until i > 10
|
||||
print(i)
|
||||
i <- 1
|
||||
## Loops
|
||||
|
54
peg.txt
54
peg.txt
@ -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 := ![_]
|
@ -21,7 +21,6 @@ impl <'a> LexicalContext<'a> {
|
||||
is_tail: false,
|
||||
}
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
fn new(parent: &'a LexicalContext<'a>) -> Self {
|
||||
LexicalContext {
|
||||
parent: Some(parent),
|
||||
@ -177,43 +176,13 @@ pub fn emit_expr(w: &mut dyn Write, expr: &parser::Expr, ctx: &mut LexicalContex
|
||||
emit_all(w, body, &mut fun_ctx)?;
|
||||
write!(w, "}}")?;
|
||||
}
|
||||
parser::Expr::Plus(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) => {
|
||||
parser::Expr::BinaryOp(op, e1, e2) => {
|
||||
emit_expr(w, e1.as_ref(), ctx)?;
|
||||
write!(w, " {} ", op)?;
|
||||
emit_expr(w, e2.as_ref(), ctx)?;
|
||||
}
|
||||
parser::Expr::UnaryMinus(e) => {
|
||||
write!(w, "-(")?;
|
||||
parser::Expr::UnaryOp(op, e) => {
|
||||
write!(w, "{}(", op)?;
|
||||
emit_expr(w, e.as_ref(), ctx)?;
|
||||
write!(w, ")")?;
|
||||
}
|
||||
|
34
src/main.rs
34
src/main.rs
@ -13,32 +13,18 @@ use std::path::PathBuf;
|
||||
#[clap(author, version, about = "Compiler for Deelang")]
|
||||
struct Cli {
|
||||
#[clap(help="Specify a file to compile")]
|
||||
file: Option<PathBuf>,
|
||||
file: PathBuf,
|
||||
#[clap(short, long, help="Emit a parse tree")]
|
||||
parse: bool,
|
||||
#[clap(short, long, help="Cross-compile to ECMAScript")]
|
||||
#[clap(short, long, help="Cross compile to ECMAScript")]
|
||||
ecmascript: bool,
|
||||
#[clap(long, help="Only run the pre-processor")]
|
||||
#[clap(long, help="Run the preprocessor")]
|
||||
preprocess: bool,
|
||||
}
|
||||
|
||||
fn repl(cli: &Cli) {
|
||||
let mut toplevel = emitter::LexicalContext::toplevel();
|
||||
let mut out = io::stdout();
|
||||
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");
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
let mut file = File::open(&cli.file).expect("Couldn't read file");
|
||||
let mut prgm = String::new();
|
||||
file.read_to_string(&mut prgm).unwrap();
|
||||
if cli.preprocess {
|
||||
@ -55,11 +41,3 @@ fn script(cli: &Cli) {
|
||||
emitter::emit_all(&mut out, &tree, &mut toplevel).ok();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
match cli.file {
|
||||
None => repl(&cli),
|
||||
Some(_) => script(&cli),
|
||||
}
|
||||
}
|
||||
|
@ -14,14 +14,8 @@ pub enum Expr {
|
||||
Atom(Atom),
|
||||
Funcall(String, Vec<Expr>),
|
||||
Funcdef(Option<String>, Block),
|
||||
UnaryMinus(Box<Expr>),
|
||||
Plus(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>),
|
||||
UnaryOp(String, Box<Expr>),
|
||||
BinaryOp(String, Box<Expr>, Box<Expr>),
|
||||
}
|
||||
|
||||
pub type Block = Vec<Stmt>;
|
||||
@ -56,18 +50,18 @@ peg::parser! {
|
||||
l:_loop() { Stmt::Loop(l) } /
|
||||
e:expr() stop() { Stmt::BareExpr(e) }
|
||||
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::Minus(Box::new(e1), Box::new(e2)) }
|
||||
e1:(@) "+" _ e2:@ { Expr::BinaryOp("+".to_string(), 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::Div(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)) }
|
||||
e1:(@) "/" _ e2:@ { Expr::BinaryOp("/".to_string(), Box::new(e1), Box::new(e2)) }
|
||||
e1:(@) "%" _ e2:@ { Expr::BinaryOp("%".to_string(), Box::new(e1), Box::new(e2)) }
|
||||
--
|
||||
"(" _ e:expr() ")" _ { e }
|
||||
['"'] s:$((!['"'] [_] / r#"\""#)*) ['"'] { Expr::Atom(Atom::String(s.to_string())) }
|
||||
@ -198,11 +192,6 @@ pub fn parse(prgm: &str) -> Vec<Stmt> {
|
||||
deelang_parser::program(&prgm).unwrap()
|
||||
}
|
||||
|
||||
pub fn parse_stmt(stmt: &str) -> Stmt {
|
||||
let stmt = preprocess(stmt);
|
||||
deelang_parser::stmt(&stmt).unwrap()
|
||||
}
|
||||
|
||||
impl fmt::Display for Stmt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
@ -258,15 +247,8 @@ impl fmt::Display for Expr {
|
||||
match self {
|
||||
Expr::Atom(a) => write!(f, "{}", a),
|
||||
Expr::Id(id) => write!(f, "{}", id),
|
||||
Expr::Equal(e1, e2) => write!(f, "{} = {}", e1, e2),
|
||||
Expr::Relop(op, e1, e2) => write!(f, "{} {} {}", e1, op, e2),
|
||||
Expr::Plus(e1, e2) => write!(f, "{} + {}", e1, e2),
|
||||
Expr::Minus(e1, e2) => write!(f, "{} - {}", e1, e2),
|
||||
Expr::Div(e1, e2) => write!(f, "{} / {}", e1, e2),
|
||||
Expr::Mult(e1, e2) => write!(f, "{} * {}", e1, e2),
|
||||
Expr::Mod(e1, e2) => write!(f, "{} % {}", e1, e2),
|
||||
Expr::UnaryMinus(e) => write!(f, "-{}", e),
|
||||
|
||||
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!(),
|
||||
}
|
||||
@ -334,18 +316,22 @@ one <- 3 - 2
|
||||
four <- (3 - 1) * 2";
|
||||
let expected = vec![
|
||||
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(2.0))),
|
||||
)),
|
||||
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(2.0))),
|
||||
)),
|
||||
Stmt::Assignment("four".to_string(),
|
||||
Expr::Mult(
|
||||
Box::new(Expr::Minus(
|
||||
Expr::BinaryOp(
|
||||
"*".to_string(),
|
||||
Box::new(Expr::BinaryOp(
|
||||
"-".to_string(),
|
||||
Box::new(Expr::Atom(Atom::Num(3.0))),
|
||||
Box::new(Expr::Atom(Atom::Num(1.0))),
|
||||
)),
|
||||
@ -360,7 +346,8 @@ four <- (3 - 1) * 2";
|
||||
let prgm = "apple <- pear(x, y) + z";
|
||||
let expected = vec![
|
||||
Stmt::Assignment("apple".to_string(),
|
||||
Expr::Plus(
|
||||
Expr::BinaryOp(
|
||||
"+".to_string(),
|
||||
Box::new(Expr::Funcall(
|
||||
"pear".to_string(),
|
||||
vec![Expr::Id("x".to_string()), Expr::Id("y".to_string())],
|
||||
@ -407,7 +394,8 @@ foo <- x -> y -> x * y";
|
||||
Stmt::BareExpr(Expr::Funcdef(
|
||||
Some("y".to_string()),
|
||||
vec![
|
||||
Stmt::BareExpr(Expr::Mult(
|
||||
Stmt::BareExpr(Expr::BinaryOp(
|
||||
"*".to_string(),
|
||||
Box::new(Expr::Id("x".to_string())),
|
||||
Box::new(Expr::Id("y".to_string())),
|
||||
))
|
||||
@ -449,7 +437,7 @@ else
|
||||
fn test_loop() {
|
||||
let prgm = r"loop until i > 100 a";
|
||||
let expected = vec![Stmt::Loop(Loop::Until(GuardedBlock {
|
||||
guard: Expr::Relop(
|
||||
guard: Expr::BinaryOp(
|
||||
">".to_string(),
|
||||
Box::new(Expr::Id("i".to_string())),
|
||||
Box::new(Expr::Atom(Atom::Num(100.0))),
|
||||
|
Loading…
Reference in New Issue
Block a user