Postfix
This commit is contained in:
+10
-3
@@ -38,12 +38,19 @@ pub enum Expression {
|
|||||||
FloatLiteral(f64),
|
FloatLiteral(f64),
|
||||||
BoolLiteral(bool),
|
BoolLiteral(bool),
|
||||||
StringLiteral(String),
|
StringLiteral(String),
|
||||||
FunctionCall {
|
Postfix {
|
||||||
callee: Box<Expression>,
|
initial: Box<Expression>,
|
||||||
args: Vec<Expression>,
|
postfixes: Vec<Postfix>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
#[serde(tag = "type", content = "value")]
|
||||||
|
pub enum Postfix {
|
||||||
|
FieldAccess(String),
|
||||||
|
FunctionCall(Vec<Expression>),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub struct ParamList(pub Vec<(String, TypeExpr)>);
|
pub struct ParamList(pub Vec<(String, TypeExpr)>);
|
||||||
|
|
||||||
|
|||||||
@@ -43,21 +43,19 @@ expr_stmt = { expr ~ ";" }
|
|||||||
// ================= EXPRESSIONS =================
|
// ================= EXPRESSIONS =================
|
||||||
type_expr = { identifier }
|
type_expr = { identifier }
|
||||||
|
|
||||||
var_path = { identifier ~ ("." ~ identifier)* }
|
primary = {
|
||||||
|
|
||||||
term = {
|
|
||||||
integer
|
integer
|
||||||
| float
|
| float
|
||||||
| boolean
|
| boolean
|
||||||
| string_lit
|
| string_lit
|
||||||
| var_path
|
| identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
func_call = {
|
postfix = {
|
||||||
var_path ~ "(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
|
("." ~ identifier)
|
||||||
|
| ("(" ~ (expr ~ ("," ~ expr)*)? ~ ")")
|
||||||
}
|
}
|
||||||
|
|
||||||
expr = {
|
expr = {
|
||||||
func_call
|
primary ~ postfix*
|
||||||
| term
|
|
||||||
}
|
}
|
||||||
|
|||||||
+33
-14
@@ -169,11 +169,20 @@ impl Statement {
|
|||||||
impl Expression {
|
impl Expression {
|
||||||
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
match pair.as_rule() {
|
||||||
Rule::expr | Rule::term => {
|
Rule::expr => {
|
||||||
let inner = pair.into_inner().next().unwrap();
|
let mut inner = pair.into_inner();
|
||||||
Expression::from_pair(inner)
|
let exp = Expression::from_pair(inner.next().unwrap());
|
||||||
|
|
||||||
|
if inner.len() > 0 {
|
||||||
|
Expression::Postfix {
|
||||||
|
initial: Box::new(exp),
|
||||||
|
postfixes: inner.map(|p| Postfix::from_pair(p)).collect(),
|
||||||
}
|
}
|
||||||
Rule::var_path => Expression::Identifier(pair.as_str().to_string()),
|
} else {
|
||||||
|
exp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Rule::primary => Expression::from_pair(pair.into_inner().next().unwrap()),
|
||||||
Rule::identifier => Expression::Identifier(pair.as_str().to_string()),
|
Rule::identifier => Expression::Identifier(pair.as_str().to_string()),
|
||||||
Rule::integer => {
|
Rule::integer => {
|
||||||
let value = pair.as_str().parse::<i64>().unwrap();
|
let value = pair.as_str().parse::<i64>().unwrap();
|
||||||
@@ -192,16 +201,6 @@ impl Expression {
|
|||||||
Expression::StringLiteral(inner_str.to_string())
|
Expression::StringLiteral(inner_str.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::func_call => {
|
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let callee = Expression::from_pair(inner.next().unwrap());
|
|
||||||
let args = inner.map(Expression::from_pair).collect();
|
|
||||||
Expression::FunctionCall {
|
|
||||||
callee: Box::new(callee),
|
|
||||||
args,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => unimplemented!(
|
_ => unimplemented!(
|
||||||
"Expression parsing not implemented yet {:?}",
|
"Expression parsing not implemented yet {:?}",
|
||||||
pair.as_rule()
|
pair.as_rule()
|
||||||
@@ -209,3 +208,23 @@ impl Expression {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Postfix {
|
||||||
|
pub fn from_pair(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
|
match pair.as_rule() {
|
||||||
|
Rule::postfix => {
|
||||||
|
let inner = pair.into_inner();
|
||||||
|
if let Some(field) = inner.peek() {
|
||||||
|
if field.as_rule() == Rule::identifier {
|
||||||
|
Postfix::FieldAccess(field.as_str().to_string())
|
||||||
|
} else {
|
||||||
|
Postfix::FunctionCall(inner.map(|p| Expression::from_pair(p)).collect())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Postfix::FunctionCall(vec![])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => unimplemented!("Postfix parsing not implemented yet {:?}", pair.as_rule()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -6,5 +6,5 @@ struct Point {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function add(a: int, b: int): int {
|
function add(a: int, b: int): int {
|
||||||
idk.do_something();
|
idk.do_something(123, "abc");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user