+12
-2
@@ -47,7 +47,7 @@ pub enum BinaryOp {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum TopLevel {
|
pub enum TopLevel {
|
||||||
Import(String),
|
Include(StaticPath),
|
||||||
StructDecl {
|
StructDecl {
|
||||||
export: bool,
|
export: bool,
|
||||||
name: String,
|
name: String,
|
||||||
@@ -66,11 +66,19 @@ pub enum TopLevel {
|
|||||||
pub enum Postfix {
|
pub enum Postfix {
|
||||||
FieldAccess(String),
|
FieldAccess(String),
|
||||||
Call(Vec<Expression>),
|
Call(Vec<Expression>),
|
||||||
|
MacroCall(String),
|
||||||
StructCall(HashMap<String, Expression>),
|
StructCall(HashMap<String, Expression>),
|
||||||
Index(Expression),
|
Index(Expression),
|
||||||
Binary(BinaryOp, Expression),
|
Binary(BinaryOp, Expression),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize)]
|
||||||
|
pub enum Prefix {
|
||||||
|
Ref,
|
||||||
|
RefMut,
|
||||||
|
Deref,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
#[derive(Debug, Clone, Serialize)]
|
||||||
pub enum Statement {
|
pub enum Statement {
|
||||||
Expression(Expression),
|
Expression(Expression),
|
||||||
@@ -134,8 +142,10 @@ pub enum Expression {
|
|||||||
FloatLiteral(f64),
|
FloatLiteral(f64),
|
||||||
BoolLiteral(bool),
|
BoolLiteral(bool),
|
||||||
StringLiteral(String),
|
StringLiteral(String),
|
||||||
Postfix {
|
TupleLiteral(Vec<Expression>),
|
||||||
|
Fix {
|
||||||
initial: Box<Expression>,
|
initial: Box<Expression>,
|
||||||
|
prefixes: Vec<Prefix>,
|
||||||
postfixes: Vec<Postfix>,
|
postfixes: Vec<Postfix>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-6
@@ -9,18 +9,19 @@ integer = @{ ASCII_DIGIT+ }
|
|||||||
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
|
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
|
||||||
boolean = { "true" | "false" }
|
boolean = { "true" | "false" }
|
||||||
string_lit = { "\"" ~ inner_str ~ "\"" }
|
string_lit = { "\"" ~ inner_str ~ "\"" }
|
||||||
inner_str = @{ (!"\"" ~ ANY)* }
|
inner_str = @{ (!"\"" ~ ("\\\"" | ANY))* }
|
||||||
static_path = { identifier ~ ("::" ~ identifier)* }
|
static_path = { identifier ~ ("::" ~ identifier)* }
|
||||||
|
|
||||||
export = { "public" }
|
export = { "public" }
|
||||||
mutable = { "mut" }
|
mutable = { "mut" }
|
||||||
var_decl = { type_expr ~ mutable? ~ identifier }
|
sharp = _{ "#" }
|
||||||
|
var_decl = { type_expr ~ mutable? ~ identifier }
|
||||||
|
|
||||||
// ================= TOP LEVEL =================
|
// ================= TOP LEVEL =================
|
||||||
|
|
||||||
top_level = _{ import | function_decl | struct_decl }
|
top_level = _{ import | function_decl | struct_decl }
|
||||||
|
|
||||||
import = { "import" ~ string_lit ~ ";" }
|
import = { sharp ~ "include" ~ "<" ~ static_path ~ ">" }
|
||||||
|
|
||||||
field_list = { field ~ ("," ~ field)* ~ ","? }
|
field_list = { field ~ ("," ~ field)* ~ ","? }
|
||||||
field = { export? ~ type_expr ~ identifier }
|
field = { export? ~ type_expr ~ identifier }
|
||||||
@@ -44,6 +45,7 @@ statement_list = { statement* }
|
|||||||
statement = _{
|
statement = _{
|
||||||
expr_stmt
|
expr_stmt
|
||||||
| var_decl_statement
|
| var_decl_statement
|
||||||
|
| assign_statement
|
||||||
| if_stmt
|
| if_stmt
|
||||||
| while_stmt
|
| while_stmt
|
||||||
| return_stmt
|
| return_stmt
|
||||||
@@ -64,6 +66,8 @@ continue_stmt = { "continue" ~ ";" }
|
|||||||
|
|
||||||
var_decl_statement = { var_decl ~ ("=" ~ expr)? ~ ";" }
|
var_decl_statement = { var_decl ~ ("=" ~ expr)? ~ ";" }
|
||||||
|
|
||||||
|
assign_statement = { expr ~ "=" ~ expr ~ ";" }
|
||||||
|
|
||||||
if_stmt = {
|
if_stmt = {
|
||||||
"if" ~ "(" ~ expr ~ ")" ~ statement ~ ("else" ~ statement)?
|
"if" ~ "(" ~ expr ~ ")" ~ statement ~ ("else" ~ statement)?
|
||||||
}
|
}
|
||||||
@@ -78,17 +82,26 @@ path_type = { static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")? }
|
|||||||
ref_type = { mutable? ~ "*" }
|
ref_type = { mutable? ~ "*" }
|
||||||
type_expr = { (path_type | tuple_type) ~ ref_type* }
|
type_expr = { (path_type | tuple_type) ~ ref_type* }
|
||||||
|
|
||||||
|
macro_inner_v = {
|
||||||
|
"(" ~ macro_inner_v* ~ ")"
|
||||||
|
| (!("(" | ")") ~ ANY)
|
||||||
|
}
|
||||||
|
|
||||||
|
tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
|
||||||
|
|
||||||
primary = {
|
primary = {
|
||||||
integer
|
integer
|
||||||
| float
|
| float
|
||||||
| boolean
|
| boolean
|
||||||
| string_lit
|
| string_lit
|
||||||
| static_path
|
| static_path
|
||||||
|
| tuple
|
||||||
}
|
}
|
||||||
|
|
||||||
postfix = {
|
postfix = {
|
||||||
field_px
|
field_px
|
||||||
| call_px
|
| call_px
|
||||||
|
| macro_call_px
|
||||||
| struct_px
|
| struct_px
|
||||||
| index_px
|
| index_px
|
||||||
| binary_px
|
| binary_px
|
||||||
@@ -96,12 +109,23 @@ postfix = {
|
|||||||
|
|
||||||
field_px = { "." ~ identifier }
|
field_px = { "." ~ identifier }
|
||||||
call_px = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
|
call_px = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
|
||||||
|
macro_call_px = { "!" ~ "(" ~ macro_inner_v* ~ ")" }
|
||||||
struct_px = { "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ "}" }
|
struct_px = { "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ "}" }
|
||||||
index_px = { "[" ~ expr ~ "]" }
|
index_px = { "[" ~ expr ~ "]" }
|
||||||
binary_px = { bin_op ~ expr }
|
binary_px = { bin_op ~ expr }
|
||||||
bin_op = { "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" }
|
bin_op = { "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" }
|
||||||
struct_field_decl = { identifier ~ ":" ~ expr }
|
struct_field_decl = { identifier ~ ":" ~ expr }
|
||||||
|
|
||||||
|
prefix = {
|
||||||
|
deref_px
|
||||||
|
| mut_ref_px
|
||||||
|
| ref_px
|
||||||
|
}
|
||||||
|
|
||||||
|
deref_px = { "*" }
|
||||||
|
mut_ref_px = { "&" ~ mutable }
|
||||||
|
ref_px = { "&" }
|
||||||
|
|
||||||
expr = {
|
expr = {
|
||||||
primary ~ postfix*
|
prefix* ~ primary ~ postfix*
|
||||||
}
|
}
|
||||||
|
|||||||
+89
-83
@@ -28,42 +28,45 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
|
|||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::type_expr => {
|
let mut inner = pair.into_inner();
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
TypeExpr(
|
match rule {
|
||||||
TypeExprKind::from(inner.next().unwrap()),
|
Rule::type_expr => TypeExpr(
|
||||||
inner.map(TypePostfix::from).collect(),
|
TypeExprKind::from(inner.next().unwrap()),
|
||||||
)
|
inner.map(TypePostfix::from).collect(),
|
||||||
}
|
),
|
||||||
_ => unimplemented!("{pair:#?}"),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TypePostfix {
|
impl From<pest::iterators::Pair<'_, Rule>> for TypePostfix {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
|
let inner = pair.into_inner();
|
||||||
|
|
||||||
|
match rule {
|
||||||
Rule::ref_type => {
|
Rule::ref_type => {
|
||||||
if pair.into_inner().peek().is_some() {
|
if inner.peek().is_some() {
|
||||||
TypePostfix::RefMut
|
TypePostfix::RefMut
|
||||||
} else {
|
} else {
|
||||||
TypePostfix::Ref
|
TypePostfix::Ref
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unimplemented!("{pair:#?}"),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
|
impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
|
||||||
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::tuple_type => {
|
let mut inner = pair.into_inner();
|
||||||
TypeExprKind::Tuple(pair.into_inner().map(TypeExpr::from).collect())
|
|
||||||
}
|
match rule {
|
||||||
|
Rule::tuple_type => TypeExprKind::Tuple(inner.map(TypeExpr::from).collect()),
|
||||||
Rule::path_type => {
|
Rule::path_type => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let path = StaticPath::from(inner.next().unwrap());
|
let path = StaticPath::from(inner.next().unwrap());
|
||||||
let params = inner.map(TypeExpr::from).collect::<Vec<_>>();
|
let params = inner.map(TypeExpr::from).collect::<Vec<_>>();
|
||||||
|
|
||||||
@@ -73,7 +76,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for TypeExprKind {
|
|||||||
TypeExprKind::PathParams(path, params)
|
TypeExprKind::PathParams(path, params)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => unimplemented!("{pair:#?}"),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,23 +116,20 @@ impl From<pest::iterators::Pair<'_, Rule>> for FieldList {
|
|||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for ParamList {
|
impl From<pest::iterators::Pair<'_, Rule>> for ParamList {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
let params = pair.into_inner().map(VarDecl::from).collect();
|
ParamList(pair.into_inner().map(VarDecl::from).collect())
|
||||||
|
|
||||||
ParamList(params)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> {
|
fn try_from(pair: pest::iterators::Pair<Rule>) -> Result<Self, ()> {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::import => {
|
let mut inner = pair.into_inner();
|
||||||
let path = pair.into_inner().next().unwrap().as_str().to_string();
|
|
||||||
Ok(TopLevel::Import(path))
|
|
||||||
}
|
|
||||||
Rule::function_decl => {
|
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
|
match rule {
|
||||||
|
Rule::import => Ok(TopLevel::Include(StaticPath::from(inner.next().unwrap()))),
|
||||||
|
|
||||||
|
Rule::function_decl => {
|
||||||
let export = if let Some(first) = inner.peek() {
|
let export = if let Some(first) = inner.peek() {
|
||||||
if first.as_rule() == Rule::export {
|
if first.as_rule() == Rule::export {
|
||||||
inner.next();
|
inner.next();
|
||||||
@@ -162,7 +162,6 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Rule::struct_decl => {
|
Rule::struct_decl => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let export = if let Some(first) = inner.peek() {
|
let export = if let Some(first) = inner.peek() {
|
||||||
if first.as_rule() == Rule::export {
|
if first.as_rule() == Rule::export {
|
||||||
inner.next();
|
inner.next();
|
||||||
@@ -185,7 +184,7 @@ impl TryFrom<pest::iterators::Pair<'_, Rule>> for TopLevel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Rule::EOI => Err(()),
|
Rule::EOI => Err(()),
|
||||||
_ => unimplemented!("TopLevel parsing not implemented yet {:?}", pair.as_rule()),
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -208,24 +207,19 @@ impl From<pest::iterators::Pair<'_, Rule>> for Block {
|
|||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::statement => {
|
let mut inner = pair.clone().into_inner();
|
||||||
let inner = pair.into_inner().next().unwrap();
|
|
||||||
Statement::from(inner)
|
|
||||||
}
|
|
||||||
|
|
||||||
Rule::expr_stmt => {
|
match rule {
|
||||||
let expr_pair = pair.into_inner().next().unwrap();
|
Rule::statement => Statement::from(inner.next().unwrap()),
|
||||||
Statement::Expression(Expression::from(expr_pair))
|
|
||||||
}
|
|
||||||
|
|
||||||
Rule::block => Statement::Block(Block::from(pair.into_inner().next().unwrap())),
|
Rule::expr_stmt => Statement::Expression(Expression::from(inner.next().unwrap())),
|
||||||
|
|
||||||
|
Rule::block => Statement::Block(Block::from(inner.next().unwrap())),
|
||||||
|
|
||||||
Rule::var_decl_statement => Statement::VarDecl(VarDeclStmt::from(pair)),
|
Rule::var_decl_statement => Statement::VarDecl(VarDeclStmt::from(pair)),
|
||||||
|
|
||||||
Rule::return_stmt => {
|
Rule::return_stmt => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
let expr = inner.next().map(Expression::from);
|
let expr = inner.next().map(Expression::from);
|
||||||
|
|
||||||
Statement::Return(expr)
|
Statement::Return(expr)
|
||||||
@@ -236,8 +230,6 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
Rule::continue_stmt => Statement::Continue,
|
Rule::continue_stmt => Statement::Continue,
|
||||||
|
|
||||||
Rule::if_stmt => {
|
Rule::if_stmt => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
let condition = Expression::from(inner.next().unwrap());
|
let condition = Expression::from(inner.next().unwrap());
|
||||||
let then_branch = Statement::from(inner.next().unwrap());
|
let then_branch = Statement::from(inner.next().unwrap());
|
||||||
|
|
||||||
@@ -251,8 +243,6 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Rule::while_stmt => {
|
Rule::while_stmt => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
|
|
||||||
let condition = Expression::from(inner.next().unwrap());
|
let condition = Expression::from(inner.next().unwrap());
|
||||||
let body = Statement::from(inner.next().unwrap());
|
let body = Statement::from(inner.next().unwrap());
|
||||||
|
|
||||||
@@ -262,71 +252,86 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unimplemented!(
|
Rule::assign_statement => Statement::VarAssign(VarAssignStmt {
|
||||||
"Statement parsing not implemented yet: {:?}",
|
target: Expression::from(inner.next().unwrap()),
|
||||||
pair.as_rule()
|
value: Expression::from(inner.next().unwrap()),
|
||||||
),
|
}),
|
||||||
|
|
||||||
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
impl From<pest::iterators::Pair<'_, Rule>> for Expression {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
|
let mut inner = pair.clone().into_inner();
|
||||||
|
|
||||||
|
match rule {
|
||||||
Rule::expr => {
|
Rule::expr => {
|
||||||
let mut inner = pair.into_inner();
|
let mut prefixes = Vec::new();
|
||||||
|
|
||||||
|
while inner
|
||||||
|
.peek()
|
||||||
|
.map(|v| v.as_rule() == Rule::prefix)
|
||||||
|
.unwrap_or_default()
|
||||||
|
{
|
||||||
|
prefixes.push(Prefix::from(inner.next().unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
let exp = Expression::from(inner.next().unwrap());
|
let exp = Expression::from(inner.next().unwrap());
|
||||||
|
|
||||||
if inner.len() > 0 {
|
if inner.len() > 0 || prefixes.len() > 0 {
|
||||||
Expression::Postfix {
|
Expression::Fix {
|
||||||
initial: Box::new(exp),
|
initial: Box::new(exp),
|
||||||
|
prefixes,
|
||||||
postfixes: inner.map(|p| Postfix::from(p)).collect(),
|
postfixes: inner.map(|p| Postfix::from(p)).collect(),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
exp
|
exp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Rule::primary => Expression::from(pair.into_inner().next().unwrap()),
|
Rule::primary => Expression::from(inner.next().unwrap()),
|
||||||
Rule::static_path => Expression::Path(StaticPath::from(pair)),
|
Rule::static_path => Expression::Path(StaticPath::from(pair)),
|
||||||
Rule::integer => {
|
Rule::integer => Expression::IntLiteral(pair.as_str().parse::<i64>().unwrap()),
|
||||||
let value = pair.as_str().parse::<i64>().unwrap();
|
Rule::float => Expression::FloatLiteral(pair.as_str().parse::<f64>().unwrap()),
|
||||||
Expression::IntLiteral(value)
|
Rule::boolean => Expression::BoolLiteral(pair.as_str().parse::<bool>().unwrap()),
|
||||||
}
|
Rule::string_lit => Expression::StringLiteral(inner.as_str().to_string()),
|
||||||
Rule::float => {
|
Rule::tuple => Expression::TupleLiteral(inner.map(Expression::from).collect()),
|
||||||
let value = pair.as_str().parse::<f64>().unwrap();
|
_ => unimplemented!("{rule:#?}"),
|
||||||
Expression::FloatLiteral(value)
|
}
|
||||||
}
|
}
|
||||||
Rule::boolean => {
|
}
|
||||||
let value = pair.as_str().parse::<bool>().unwrap();
|
|
||||||
Expression::BoolLiteral(value)
|
|
||||||
}
|
|
||||||
Rule::string_lit => {
|
|
||||||
let inner_str = pair.into_inner().next().unwrap().as_str();
|
|
||||||
Expression::StringLiteral(inner_str.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => unimplemented!(
|
impl From<pest::iterators::Pair<'_, Rule>> for Prefix {
|
||||||
"Expression parsing not implemented yet {:?}",
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
pair.as_rule()
|
match pair.as_rule() {
|
||||||
),
|
Rule::prefix => Self::from(pair.into_inner().next().unwrap()),
|
||||||
|
Rule::deref_px => Self::Deref,
|
||||||
|
Rule::mut_ref_px => Self::RefMut,
|
||||||
|
Rule::ref_px => Self::Ref,
|
||||||
|
_ => unimplemented!("{pair:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
||||||
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
fn from(pair: pest::iterators::Pair<Rule>) -> Self {
|
||||||
match pair.as_rule() {
|
let rule = pair.as_rule();
|
||||||
Rule::postfix => Postfix::from(pair.into_inner().next().unwrap()),
|
let mut inner = pair.into_inner();
|
||||||
|
|
||||||
|
match rule {
|
||||||
|
Rule::postfix => Postfix::from(inner.next().unwrap()),
|
||||||
|
|
||||||
Rule::field_px => {
|
Rule::field_px => {
|
||||||
let field_name = pair.into_inner().next().unwrap().as_str().to_string();
|
let field_name = inner.next().unwrap().as_str().to_string();
|
||||||
Postfix::FieldAccess(field_name)
|
Postfix::FieldAccess(field_name)
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::call_px => Postfix::Call(pair.into_inner().map(Expression::from).collect()),
|
Rule::call_px => Postfix::Call(inner.map(Expression::from).collect()),
|
||||||
|
|
||||||
Rule::struct_px => Postfix::StructCall(
|
Rule::struct_px => Postfix::StructCall(
|
||||||
pair.into_inner()
|
inner
|
||||||
.map(|p| {
|
.map(|p| {
|
||||||
let mut pi = p.into_inner();
|
let mut pi = p.into_inner();
|
||||||
(
|
(
|
||||||
@@ -337,10 +342,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
|||||||
.collect(),
|
.collect(),
|
||||||
),
|
),
|
||||||
|
|
||||||
Rule::index_px => Postfix::Index(Expression::from(pair.into_inner().next().unwrap())),
|
Rule::index_px => Postfix::Index(Expression::from(inner.next().unwrap())),
|
||||||
|
|
||||||
Rule::binary_px => {
|
Rule::binary_px => {
|
||||||
let mut inner = pair.into_inner();
|
|
||||||
let op_pair = inner.next().unwrap();
|
let op_pair = inner.next().unwrap();
|
||||||
let op = match op_pair.as_str() {
|
let op = match op_pair.as_str() {
|
||||||
"+" => BinaryOp::Plus,
|
"+" => BinaryOp::Plus,
|
||||||
@@ -362,7 +366,9 @@ impl From<pest::iterators::Pair<'_, Rule>> for Postfix {
|
|||||||
Postfix::Binary(op, Expression::from(inner.next().unwrap()))
|
Postfix::Binary(op, Expression::from(inner.next().unwrap()))
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unimplemented!("Postfix parsing not implemented yet {:?}", pair.as_rule()),
|
Rule::macro_call_px => Postfix::MacroCall(inner.as_str().to_string()),
|
||||||
|
|
||||||
|
_ => unimplemented!("{rule:#?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+88
-64
@@ -1,6 +1,6 @@
|
|||||||
use parser::ast::{
|
use parser::ast::{
|
||||||
BinaryOp, Block, Expression, IfStmt, Postfix, Statement, StaticPath, TopLevel, TypeExpr,
|
BinaryOp, Block, Expression, IfStmt, Postfix, Prefix, Statement, StaticPath, TopLevel,
|
||||||
TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt,
|
TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, WhileStmt,
|
||||||
};
|
};
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -112,71 +112,96 @@ impl GetRust for Expression {
|
|||||||
Expression::IntLiteral(n) => n.to_string(),
|
Expression::IntLiteral(n) => n.to_string(),
|
||||||
Expression::FloatLiteral(n) => n.to_string(),
|
Expression::FloatLiteral(n) => n.to_string(),
|
||||||
Expression::BoolLiteral(b) => b.to_string(),
|
Expression::BoolLiteral(b) => b.to_string(),
|
||||||
Expression::StringLiteral(s) => format!("\"{}\".to_string()", s),
|
Expression::StringLiteral(s) => format!("\"{s}\""),
|
||||||
|
Expression::TupleLiteral(t) => {
|
||||||
|
format!(
|
||||||
|
"({})",
|
||||||
|
t.iter()
|
||||||
|
.map(Expression::get_rust)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Expression::Fix {
|
||||||
|
initial,
|
||||||
|
prefixes,
|
||||||
|
postfixes,
|
||||||
|
} => prefixes.get_rust() + &initial.get_rust() + &postfixes.get_rust(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Expression::Postfix { initial, postfixes } => {
|
impl GetRust for Prefix {
|
||||||
let base = initial.get_rust();
|
fn get_rust(&self) -> String {
|
||||||
postfixes.get_rust_with_base(&base)
|
match self {
|
||||||
|
Self::Deref => "*",
|
||||||
|
Self::Ref => "&",
|
||||||
|
Self::RefMut => "&mut ",
|
||||||
|
}
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GetRust for [Prefix] {
|
||||||
|
fn get_rust(&self) -> String {
|
||||||
|
self.iter().map(Prefix::get_rust).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GetRust for Postfix {
|
||||||
|
fn get_rust(&self) -> String {
|
||||||
|
match self {
|
||||||
|
Postfix::FieldAccess(field) => format!(".{}", field),
|
||||||
|
|
||||||
|
Postfix::Call(args) => {
|
||||||
|
let args = args
|
||||||
|
.iter()
|
||||||
|
.map(|a| a.get_rust())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
format!("({})", args)
|
||||||
|
}
|
||||||
|
|
||||||
|
Postfix::MacroCall(inner) => {
|
||||||
|
format!("!({})", inner)
|
||||||
|
}
|
||||||
|
|
||||||
|
Postfix::StructCall(fields) => {
|
||||||
|
let fields = fields
|
||||||
|
.iter()
|
||||||
|
.map(|(k, v)| format!("{}: {}", k, v.get_rust()))
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(", ");
|
||||||
|
format!("{{ {} }}", fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
Postfix::Index(idx) => {
|
||||||
|
format!("[{}]", idx.get_rust())
|
||||||
|
}
|
||||||
|
|
||||||
|
Postfix::Binary(op, rhs) => {
|
||||||
|
let op_str = match op {
|
||||||
|
BinaryOp::Plus => "+",
|
||||||
|
BinaryOp::Minus => "-",
|
||||||
|
BinaryOp::Multiply => "*",
|
||||||
|
BinaryOp::Divide => "/",
|
||||||
|
BinaryOp::Modulo => "%",
|
||||||
|
BinaryOp::Equal => "==",
|
||||||
|
BinaryOp::NotEqual => "!=",
|
||||||
|
BinaryOp::LessThan => "<",
|
||||||
|
BinaryOp::GreaterThan => ">",
|
||||||
|
BinaryOp::LessThanOrEqual => "<=",
|
||||||
|
BinaryOp::GreaterThanOrEqual => ">=",
|
||||||
|
};
|
||||||
|
format!("{} {}", op_str, rhs.get_rust())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper — applies a slice of postfixes onto an already-rendered base string.
|
impl GetRust for [Postfix] {
|
||||||
trait PostfixChain {
|
fn get_rust(&self) -> String {
|
||||||
fn get_rust_with_base(&self, base: &str) -> String;
|
self.iter().map(Postfix::get_rust).collect()
|
||||||
}
|
|
||||||
|
|
||||||
impl PostfixChain for [Postfix] {
|
|
||||||
fn get_rust_with_base(&self, base: &str) -> String {
|
|
||||||
let mut result = base.to_string();
|
|
||||||
|
|
||||||
for postfix in self {
|
|
||||||
result = match postfix {
|
|
||||||
Postfix::FieldAccess(field) => format!("{}.{}", result, field),
|
|
||||||
|
|
||||||
Postfix::Call(args) => {
|
|
||||||
let args = args
|
|
||||||
.iter()
|
|
||||||
.map(|a| a.get_rust())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ");
|
|
||||||
format!("{}({})", result, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
Postfix::StructCall(fields) => {
|
|
||||||
let fields = fields
|
|
||||||
.iter()
|
|
||||||
.map(|(k, v)| format!("{}: {}", k, v.get_rust()))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(", ");
|
|
||||||
format!("{} {{ {} }}", result, fields)
|
|
||||||
}
|
|
||||||
|
|
||||||
Postfix::Index(idx) => {
|
|
||||||
format!("{}[{}]", result, idx.get_rust())
|
|
||||||
}
|
|
||||||
|
|
||||||
Postfix::Binary(op, rhs) => {
|
|
||||||
let op_str = match op {
|
|
||||||
BinaryOp::Plus => "+",
|
|
||||||
BinaryOp::Minus => "-",
|
|
||||||
BinaryOp::Multiply => "*",
|
|
||||||
BinaryOp::Divide => "/",
|
|
||||||
BinaryOp::Modulo => "%",
|
|
||||||
BinaryOp::Equal => "==",
|
|
||||||
BinaryOp::NotEqual => "!=",
|
|
||||||
BinaryOp::LessThan => "<",
|
|
||||||
BinaryOp::GreaterThan => ">",
|
|
||||||
BinaryOp::LessThanOrEqual => "<=",
|
|
||||||
BinaryOp::GreaterThanOrEqual => ">=",
|
|
||||||
};
|
|
||||||
format!("{} {} {}", result, op_str, rhs.get_rust())
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,9 +220,8 @@ impl ToRust for Block {
|
|||||||
impl ToRust for TopLevel {
|
impl ToRust for TopLevel {
|
||||||
fn to_rust(&self, cg: &mut RustCodegen) {
|
fn to_rust(&self, cg: &mut RustCodegen) {
|
||||||
match self {
|
match self {
|
||||||
TopLevel::Import(path) => {
|
TopLevel::Include(path) => {
|
||||||
let path = path.replace('"', "");
|
cg.addln(&format!("use {};", path.get_rust()));
|
||||||
cg.addln(&format!("use {};", path));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TopLevel::StructDecl {
|
TopLevel::StructDecl {
|
||||||
|
|||||||
+3
-1
@@ -1 +1,3 @@
|
|||||||
i32 add(i32* a, i32* b) {}
|
void main() {
|
||||||
|
(i32, i32, str*) my_tuple = (10, 20, "hello, world");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user