Skip to content

Commit

Permalink
adjust function grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
SkymanOne committed Jan 29, 2024
1 parent 0c00290 commit fcfa90c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = [
members = [
"crates/derive_node",
"crates/folidity",
"crates/parser",
Expand All @@ -26,4 +26,4 @@ thiserror = "1.0"
syn = "2.0"
synstructure = "0.13"
proc-macro2 = "1.0"
quote = "1.0"
quote = "1.0"
2 changes: 1 addition & 1 deletion crates/parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub struct FunctionDeclaration {
/// Function logical bounds
pub st_block: Option<StBlock>,
/// The body of the function.
pub body: StatementBlock,
pub body: Statement,
}

#[derive(Clone, Debug, PartialEq, Node)]
Expand Down
74 changes: 40 additions & 34 deletions crates/parser/src/folidity.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,42 @@ ModelDeclaration: ast::ModelDeclaration = {
FromState: (ast::Identifier, Option<ast::Identifier>) = {
"from" <s:Identifier> <si:Identifier?> => {
(s, si)
}
}
}

StateDeclaration: ast::StateDeclaration = {
<start:@L> "state" <i:Identifier> "(" <m:Identifier> ")"
<start:@L> "state" <i:Identifier> "(" <m:Identifier> ")"
<from:FromState?> <st:StBlock?> <end:@R> => {
ast::StateDeclaration::new(start, end, i, Some(ast::StateBody::Model(m)), from, st)
},

<start:@L> "state" <i:Identifier> "{" <params:Params> "}"
<start:@L> "state" <i:Identifier> "{" <params:Params> "}"
<from:FromState?> <st:StBlock?> <end:@R> => {
ast::StateDeclaration::new(start, end, i, Some(ast::StateBody::Raw(params)), from, st)
ast::StateDeclaration::new(start, end, i, Some(ast::StateBody::Raw(params)), from, st)
},

<start:@L> "state" <i:Identifier> ";"<end:@R> => {
ast::StateDeclaration::new(start, end, i, None, None, None)
},
ast::StateDeclaration::new(start, end, i, None, None, None)
},
}

FunDeclaration: ast::FunctionDeclaration = {
<start:@L> <init:"@init"?> <attrs:AccessAttr*> <view:View?> "fn" <ty:FuncReturnType> <i:Identifier>
"(" <params:MutParams> ")" <sb:StateBound?> <st:StBlock?> <body:StatementBlock> <end:@R> => {
<start:@L> <init:"@init"?> <attrs:AccessAttr*> <view:View?> "fn" <ty:FuncReturnType> <i:Identifier>
"(" <params:MutParams> ")" <sb:StateBound?> <st:StBlock?> <body:FunBody> <end:@R> => {
let is_pub = attrs.len() > 0;
let vis = match view {
Some(v) => v,
_ if is_pub => ast::FunctionVisibility::Pub,
_ => ast::FunctionVisibility::Priv
};
ast::FunctionDeclaration::new(
start,
start,
end,
init.is_some(),
attrs,
vis,
ty,
i,
i,
params,
sb,
st,
Expand All @@ -88,6 +88,11 @@ FunDeclaration: ast::FunctionDeclaration = {
}
}

FunBody: ast::Statement = {
<block:StatementBlock> => ast::Statement::Block(block),
"=" <Statement> => <>
}

AccessAttr: ast::AccessAttribute = {
<start:@L> "@" "(" <first:Expression?> <mut memebers:("|" <Expression>)*> ")" <end:@R> => {
let mut all = if first.is_some() { vec![first.unwrap()] } else { vec![] };
Expand All @@ -107,16 +112,15 @@ View: ast::FunctionVisibility = {
}

StateParam: ast::StateParam = {
<start:@L> <ty:Identifier> <name_op:Identifier?> <end:@R> => {
let n = match name_op {
Some(i) => Some(i),
None => None,
};
ast::StateParam::new(start, end, Some(ty), n)
<start:@L> <ty:Identifier> <end:@R> => {
ast::StateParam::new(start, end, Some(ty), None)
},
<start:@L> "(" <ty:Identifier> <name_op:Identifier> ")" <end:@R> => {
ast::StateParam::new(start, end, Some(ty), Some(name_op))
},
<start:@L> "()" <end:@R> => {
ast::StateParam::new(start, end, None, None)
}
}
}

FuncReturnType: ast::FuncReturnType = {
Expand All @@ -128,7 +132,7 @@ FuncReturnType: ast::FuncReturnType = {
MutParam: ast::Param = {
<start:@L> <m:"mut"?> <i:Identifier> ":" <t:Type> <end:@R> => {
ast::Param::new(start, end, t, i, m.is_some())
}
}
}

MutParams: Vec<ast::Param> = {
Expand Down Expand Up @@ -177,7 +181,7 @@ StateBound: ast::StateBound = {
}

StBlock: ast::StBlock = {
"st" "{" <start:@L> <e:Expression?> <mut exprs:("," <Expression>)+> <end:@R> "}" => {
"st" "[" <start:@L> <e:Expression?> <mut exprs:("," <Expression>)+> <end:@R> "]" => {
let mut all = if e.is_some() { vec![e.unwrap()] } else { vec![] };
all.append(&mut exprs);
ast::StBlock::new(start, end, all)
Expand All @@ -201,12 +205,12 @@ Variable: ast::Variable = {

<start:@L> "let" <mt:"mut"?> "{" <is:Identifier+> "}" <ty:(":" <Type>)?> <val:("=" <Expression>)?> ";" <end:@R> => {
ast::Variable::new(start, end, is, mt.is_some(), ty, val)
},
},

<start:@L> "let" <mt:"mut"?> "{" <is:Identifier+> "}" <ty:(":" <Type>)?> <error:!> ";" <end:@R> => {
errors.push(error);
ast::Variable::new(start, end, is, mt.is_some(), ty, None)
},
},
}

Assign: ast::Assign = {
Expand Down Expand Up @@ -271,7 +275,7 @@ Args: Vec<ast::Expression> = {
<first:Expression?> <mut args:("," <Expression>)*> => {
let mut all = if first.is_some() { vec![first.unwrap()] } else { vec![] };
all.append(&mut args);
all
all
}
}

Expand Down Expand Up @@ -354,50 +358,50 @@ Expression: ast::Expression = {
ast::Expression::Equal(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},
<start:@L> <lhs:Expression> "!=" <rhs:Expression> <end:@R> => {
ast::Expression::NotEqual(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},
<start:@L> <lhs:Expression> ">" <rhs:Expression> <end:@R> => {
ast::Expression::Greater(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},
<start:@L> <lhs:Expression> "<" <rhs:Expression> <end:@R> => {
ast::Expression::Less(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},
<start:@L> <lhs:Expression> ">=" <rhs:Expression> <end:@R> => {
ast::Expression::GreaterEq(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},
<start:@L> <lhs:Expression> "<=" <rhs:Expression> <end:@R> => {
ast::Expression::LessEq(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},
<start:@L> <lhs:Expression> "in" <rhs:Expression> <end:@R> => {
ast::Expression::In(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},

#[precedence(level="7")] #[assoc(side="left")]
<start:@L> <lhs:Expression> "||" <rhs:Expression> <end:@R> => {
ast::Expression::Or(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},
<start:@L> <lhs:Expression> "&&" <rhs:Expression> <end:@R> => {
ast::Expression::And(
ast::BinaryExpression::new(start, end, Box::new(lhs), Box::new(rhs))
)
},
},

}

Term: ast::Expression = {
Expand Down Expand Up @@ -438,7 +442,7 @@ Term: ast::Expression = {
},


"(" <Expression> ")" => <>
"(" <Expression> ")" => <>
}

Type: ast::Type = {
Expand Down Expand Up @@ -490,6 +494,8 @@ extern {
"(" => Token::LParen,
")" => Token::RParen,
"{" => Token::LCurly,
"[" => Token::LSquare,
"]" => Token::RSquare,
"}" => Token::RCurly,
"<" => Token::LAngle,
">" => Token::RAngle,
Expand Down Expand Up @@ -551,4 +557,4 @@ extern {
"," => Token::Coma,
"move" => Token::Move,
}
}
}
7 changes: 7 additions & 0 deletions crates/parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ pub enum Token<'input> {
#[token(">")]
RAngle,

#[token("[")]
LSquare,
#[token("]")]
RSquare,

#[token("=")]
Assign,

Expand Down Expand Up @@ -226,6 +231,8 @@ impl<'input> fmt::Display for Token<'input> {
Token::RParen => word(")"),
Token::LCurly => word("{"),
Token::RCurly => word("}"),
Token::LSquare => word("["),
Token::RSquare => word("]"),
Token::LAngle => word("<"),
Token::RAngle => word(">"),
Token::Assign => word("="),
Expand Down
41 changes: 21 additions & 20 deletions crates/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@ fn comment_token() {
const SRC: &str = r#"
@init
@(any)
fn () init(proposal: string,
start_block: int,
max_size: int,
end_block: int)
fn () init(proposal: string,
start_block: int,
max_size: int,
end_block: int)
when () -> BeginState
{
move BeginState {
proposal,
start_block,
end_block,
max_size
};
}
= move BeginState {
proposal,
start_block,
end_block,
max_size
};
"#;

#[test]
Expand All @@ -77,22 +75,25 @@ fn test_simple_func() {
assert!(matches!(&ty.ty, TypeVariant::Unit))
}

assert_eq!(func_decl.body.statements.len(), 1);
let statement = &func_decl.body.statements[0];
assert!(matches!(statement, Statement::StateTransition(_)))
let statement = &func_decl.body;
assert!(
matches!(statement, Statement::StateTransition(_)),
"Got {:?}",
statement
)
}
}

const FACTORIAL_SRC: &str = r#"
state NoState;
state EmptyState;
fn (out: int) calculate(value: int)
st {
st [
value > 0,
out < 10000
}
]
{
if value == 1 {
move SimpleState{};
move EmptyState{};
return value;
} else {
return calculate(
Expand Down Expand Up @@ -123,7 +124,7 @@ fn test_factorial() {
let first_decl = &tree.declarations[0];
assert!(matches!(first_decl, Declaration::StateDeclaration(_)));
if let Declaration::StateDeclaration(state) = first_decl {
assert_eq!(state.name.name, "NoState");
assert_eq!(state.name.name, "EmptyState");
assert_eq!(state.body, None);
assert_eq!(state.from, None);
assert_eq!(state.st_block, None);
Expand Down
8 changes: 8 additions & 0 deletions out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Compiling folidity-parser v1.0.0 (/Users/skymanone/Projects/folidity/crates/parser)
Compiling folidity v1.0.0 (/Users/skymanone/Projects/folidity/crates/folidity)
Finished test [unoptimized + debuginfo] target(s) in 0.51s
Running unittests src/lib.rs (target/debug/deps/derive_node-00a8a454367af414)
Running unittests src/main.rs (target/debug/deps/folidity-394d4be1e2852a7e)
Running unittests src/lib.rs (target/debug/deps/folidity_parser-5e57cdcdcf157d2e)
Doc-tests derive-node
Doc-tests folidity-parser

0 comments on commit fcfa90c

Please sign in to comment.