Skip to content
Snippets Groups Projects
Commit 2bb6b324 authored by Per Lindgren's avatar Per Lindgren
Browse files

move to new syntax, wip6

parent 11982323
Branches
No related tags found
No related merge requests found
...@@ -7,5 +7,5 @@ fn main() { ...@@ -7,5 +7,5 @@ fn main() {
} }
fn b(x: i32) -> i32 { fn b(x: i32) -> i32 {
x; x
} }
...@@ -28,13 +28,11 @@ pub struct Stmts { ...@@ -28,13 +28,11 @@ pub struct Stmts {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum Stmt { pub enum Stmt {
// Id mut? (: Type)? (= Expr)?
Let(Id, bool, Option<Type>, Option<Box<Expr>>), Let(Id, bool, Option<Type>, Option<Box<Expr>>),
Assign(Box<Expr>, Box<Expr>), Assign(Box<Expr>, Box<Expr>),
While(Box<Expr>, Stmts), While(Box<Expr>, Stmts),
If(Box<Expr>, Stmts, Option<Stmts>), If(Box<Expr>, Stmts, Option<Stmts>),
Expr(Box<Expr>), Expr(Box<Expr>),
// perhaps block should be expression, hmm everything is expressions?
Block(Stmts), Block(Stmts),
Semi, Semi,
} }
...@@ -216,13 +214,13 @@ impl Stmts { ...@@ -216,13 +214,13 @@ impl Stmts {
fn ifmt(&self, fmt: &mut Formatter, indent: usize) -> Result<(), Error> { fn ifmt(&self, fmt: &mut Formatter, indent: usize) -> Result<(), Error> {
write!(fmt, "{{")?; write!(fmt, "{{")?;
let len = self.stmts.len(); let len = self.stmts.len();
for (_i, s) in (&self.stmts).into_iter().enumerate() { for (i, s) in (&self.stmts).into_iter().enumerate() {
// set tab // set tab
write!(fmt, "\n{}", " ".repeat(indent + 4))?; write!(fmt, "\n{}", " ".repeat(indent + 4))?;
s.ifmt(fmt, indent + 4)?; s.ifmt(fmt, indent + 4)?;
// if i < len - 1 || self.ret { if i < len - 1 || self.ret {
// write!(fmt, ";")?; write!(fmt, ";")?;
// } }
} }
write!(fmt, "\n{}}}", " ".repeat(indent)) write!(fmt, "\n{}}}", " ".repeat(indent))
} }
......
...@@ -441,7 +441,7 @@ pub fn check(p: &Program) -> Result<(), Error> { ...@@ -441,7 +441,7 @@ pub fn check(p: &Program) -> Result<(), Error> {
var_env.push_param_scope(arg_ty); var_env.push_param_scope(arg_ty);
let stmt_type = check_stmts(&fd.body, &fn_env, &type_env, &mut var_env); let stmt_type = check_stmts(&fd.body, &fn_env, &type_env, &mut var_env);
trace!("result {}: {:?}", fd.id, &stmt_type); trace!("result {}: {} {:?}", &fd.id, &fd.result, &stmt_type);
let stmt_type = strip_mut(stmt_type?); let stmt_type = strip_mut(stmt_type?);
......
...@@ -15,13 +15,9 @@ match { ...@@ -15,13 +15,9 @@ match {
// A comma separated sequence with optional trailing comma // A comma separated sequence with optional trailing comma
Comma<T>: Vec<T> = { Comma<T>: Vec<T> = {
<v:(<T> ",")*> <e:T?> => match e { <mut v:(<T> ",")*> <e:T?> => match e {
None => v, None => v,
Some(e) => { Some(e) => { v.push(e); v },
let mut v = v;
v.push(e);
v
}
} }
} }
...@@ -32,16 +28,12 @@ CommaNoTrail<T>: Vec<T> = { ...@@ -32,16 +28,12 @@ CommaNoTrail<T>: Vec<T> = {
// Parenthesized, comma delimeted // Parenthesized, comma delimeted
ParCNT<T>: Vec<T> = { ParCNT<T>: Vec<T> = {
"()" => Vec::new(), // seems like a hack? "()" => Vec::new(),
"(" <CommaNoTrail<T>> ")" => <>, "(" <CommaNoTrail<T>> ")" => <>,
} }
pub Path: Path = { pub Path: Path = {
<h: (<Id> "::")+> <t: Id> => { <mut h: (<Id> "::")+> <t: Id> => { h.push(t); Path(h) }
let mut h = h;
h.push(t);
Path(h)
}
} }
// Type and FnDecl declarations may occur in any order. // Type and FnDecl declarations may occur in any order.
...@@ -113,10 +105,7 @@ Params : Params = { ...@@ -113,10 +105,7 @@ Params : Params = {
Param : Param = { Param : Param = {
<is_mut: "mut"?> <id: Id> ":" <ty: Type> => <is_mut: "mut"?> <id: Id> ":" <ty: Type> =>
Param { Param {
is_mut: match is_mut { is_mut: is_mut.is_some(),
Some(_) => true,
_ => false,
},
id, id,
ty, ty,
}, },
...@@ -140,8 +129,8 @@ pub Block: Stmts = { ...@@ -140,8 +129,8 @@ pub Block: Stmts = {
Stmts { Stmts {
stmts, stmts,
ret: match st { ret: match st {
Some(Stmt::Semi) => true, Some(_) => false,
_ => false _ => true,
} }
} }
} }
......
...@@ -177,5 +177,5 @@ fn check_let_if() { ...@@ -177,5 +177,5 @@ fn check_let_if() {
#[test] #[test]
fn wip() { fn wip() {
let _ = check(&read_file::parse("examples/wip.rs")).is_ok(); let _ = check(&read_file::parse("examples/wip.rs")).unwrap();
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment