Removed varKind in favor of mutability
This commit is contained in:
+2
-9
@@ -64,7 +64,7 @@ pub enum Statement {
|
|||||||
|
|
||||||
// let/const/var x = ...
|
// let/const/var x = ...
|
||||||
VarDecl {
|
VarDecl {
|
||||||
kind: VarKind,
|
mutable: bool,
|
||||||
name: String,
|
name: String,
|
||||||
init: Option<Expression>,
|
init: Option<Expression>,
|
||||||
type_: Option<TypeExpr>,
|
type_: Option<TypeExpr>,
|
||||||
@@ -90,7 +90,7 @@ pub enum Statement {
|
|||||||
|
|
||||||
// for (...) stmt
|
// for (...) stmt
|
||||||
For {
|
For {
|
||||||
init: (VarKind, String, Option<Expression>),
|
init: (bool, String, Option<Expression>),
|
||||||
condition: Option<Expression>,
|
condition: Option<Expression>,
|
||||||
update: Option<Box<Statement>>,
|
update: Option<Box<Statement>>,
|
||||||
body: Box<Statement>,
|
body: Box<Statement>,
|
||||||
@@ -115,10 +115,3 @@ pub enum Expression {
|
|||||||
postfixes: Vec<Postfix>,
|
postfixes: Vec<Postfix>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize)]
|
|
||||||
pub enum VarKind {
|
|
||||||
Let,
|
|
||||||
Const,
|
|
||||||
Var,
|
|
||||||
}
|
|
||||||
|
|||||||
+2
-9
@@ -188,7 +188,7 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
let init = inner.next().map(Expression::from);
|
let init = inner.next().map(Expression::from);
|
||||||
|
|
||||||
Statement::VarDecl {
|
Statement::VarDecl {
|
||||||
kind: VarKind::Var,
|
mutable: false,
|
||||||
name: name.as_str().to_string(),
|
name: name.as_str().to_string(),
|
||||||
init,
|
init,
|
||||||
type_,
|
type_,
|
||||||
@@ -243,19 +243,12 @@ impl From<pest::iterators::Pair<'_, Rule>> for Statement {
|
|||||||
Rule::var_decl => {
|
Rule::var_decl => {
|
||||||
let mut it = p.into_inner();
|
let mut it = p.into_inner();
|
||||||
|
|
||||||
let kind = match it.next().unwrap().as_str() {
|
|
||||||
"let" => VarKind::Let,
|
|
||||||
"const" => VarKind::Const,
|
|
||||||
"var" => VarKind::Var,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let name = it.next().unwrap().as_str().to_string();
|
let name = it.next().unwrap().as_str().to_string();
|
||||||
let init_expr = it
|
let init_expr = it
|
||||||
.next()
|
.next()
|
||||||
.map(|e| Expression::from(e.into_inner().next().unwrap()));
|
.map(|e| Expression::from(e.into_inner().next().unwrap()));
|
||||||
|
|
||||||
(kind, name, init_expr)
|
(false, name, init_expr)
|
||||||
}
|
}
|
||||||
_ => unimplemented!(
|
_ => unimplemented!(
|
||||||
"For loop init parsing not implemented yet: {:?}",
|
"For loop init parsing not implemented yet: {:?}",
|
||||||
|
|||||||
+3
-6
@@ -1,4 +1,4 @@
|
|||||||
use parser::ast::{BinaryOp, Block, Expression, Postfix, Statement, TopLevel, TypeExpr, VarKind};
|
use parser::ast::{BinaryOp, Block, Expression, Postfix, Statement, TopLevel, TypeExpr};
|
||||||
|
|
||||||
pub struct RustCodegen {
|
pub struct RustCodegen {
|
||||||
output: String,
|
output: String,
|
||||||
@@ -129,15 +129,12 @@ impl RustCodegen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Statement::VarDecl {
|
Statement::VarDecl {
|
||||||
kind,
|
mutable,
|
||||||
name,
|
name,
|
||||||
init,
|
init,
|
||||||
type_,
|
type_,
|
||||||
} => {
|
} => {
|
||||||
let mutability = match kind {
|
let mutability = if *mutable { "mut " } else { "" };
|
||||||
VarKind::Var => "mut ",
|
|
||||||
_ => "",
|
|
||||||
};
|
|
||||||
|
|
||||||
let ty = type_
|
let ty = type_
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
|||||||
Reference in New Issue
Block a user