Skip to content

Commit

Permalink
add group expressions (...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aelto committed Jul 24, 2022
1 parent dc65081 commit c70ea44
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/ast/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum Expression {
Nesting(Vec<Expression>),
Cast(String, Rc<Expression>),

Group(Rc<Expression>),

Error,
}

Expand All @@ -41,6 +43,7 @@ impl visitor::Visited for Expression {
}
Expression::Nesting(x) => x.accept(visitor),
Expression::Error => todo!(),
Expression::Group(x) => x.accept(visitor),
}
}
}
Expand All @@ -67,11 +70,16 @@ impl Codegen for Expression {
Expression::Error => todo!(),
Expression::Nesting(x) => x.emit(context, f),
Expression::Cast(t, x) => {
write!(f, "(({t})")?;
write!(f, "({t})(")?;
x.emit(context, f)?;
write!(f, ")")
}
Expression::ClassInstantiation(x) => x.emit(context, f),
Expression::Group(x) => {
write!(f, "(")?;
x.emit(context, f)?;
write!(f, ")")
}
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/ast/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn emit_function(
write!(f, " {}{}(", this.name, generic_variant_suffix)?;
}

this.parameters.emit(context, f)?;
this.parameters.emit_join(context, f, ", ")?;
write!(f, ")")?;

if let Some(t) = &this.type_declaration {
Expand Down Expand Up @@ -132,6 +132,7 @@ pub enum FunctionBodyStatement {
WhileStatement(WhileStatement),
DoWhileStatement(DoWhileStatement),
SwitchStatement(SwitchStatement),
Delete(Rc<Expression>),
}

impl visitor::Visited for FunctionBodyStatement {
Expand All @@ -146,6 +147,7 @@ impl visitor::Visited for FunctionBodyStatement {
FunctionBodyStatement::WhileStatement(x) => x.accept(visitor),
FunctionBodyStatement::DoWhileStatement(x) => x.accept(visitor),
FunctionBodyStatement::SwitchStatement(x) => x.accept(visitor),
FunctionBodyStatement::Delete(x) => x.accept(visitor),
FunctionBodyStatement::Break => {}
FunctionBodyStatement::Continue => {}
};
Expand Down Expand Up @@ -200,6 +202,11 @@ impl Codegen for FunctionBodyStatement {
FunctionBodyStatement::Continue => {
writeln!(f, "continue;")?;
}
FunctionBodyStatement::Delete(x) => {
write!(f, "delete ")?;
x.emit(context, f)?;
writeln!(f, ";")?;
}
};

Ok(())
Expand Down Expand Up @@ -248,7 +255,7 @@ pub enum ParameterType {
}

impl Codegen for ParameterType {
fn emit(&self, context: &Context, f: &mut Vec<u8>) -> Result<(), std::io::Error> {
fn emit(&self, _: &Context, f: &mut Vec<u8>) -> Result<(), std::io::Error> {
use std::io::Write as IoWrite;

match self {
Expand Down
6 changes: 4 additions & 2 deletions src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ GlobalFunctionDeclaration: Rc<FunctionDeclaration> = {
let has_generic_types = generic_types.is_some();
let declaration = Rc::new(FunctionDeclaration {
context: Rc::new(RefCell::new(Context::new(&format!("function: {}", &name), generic_types.clone()))),
function_type: FunctionType::Function,
function_type,
name: name.clone(),
generic_types,
parameters,
Expand Down Expand Up @@ -222,6 +222,7 @@ FunctionBodyStatement: FunctionBodyStatement = {
KeywordReturn <expression:Expression?> ";" => FunctionBodyStatement::Return(expression),
KeywordBreak ";" => FunctionBodyStatement::Break,
KeywordContinue ";" => FunctionBodyStatement::Continue,
KeywordDelete <expression:Expression> ";" => FunctionBodyStatement::Delete(expression),

<var:VariableAssignment> ";" => FunctionBodyStatement::Assignement(<>),
IfStatement => FunctionBodyStatement::IfStatement(<>),
Expand Down Expand Up @@ -434,7 +435,7 @@ ComparisonType: ComparisonType = {

Term: Rc<Expression> = {
Integer => Rc::new(Expression::Integer(<>)),
"(" <Expression> ")",
"(" <Expression> ")" => Rc::new(Expression::Group(<>)),
StringLiteral => Rc::new(Expression::String(String::from(<>))),
CharLiteral => Rc::new(Expression::Name(String::from(<>))),

Expand Down Expand Up @@ -621,6 +622,7 @@ match {
"break" => KeywordBreak,
"continue" => KeywordContinue,
"exec" => KeywordExec,
"delete" => KeywordDelete,
} else {
// These items have next highest precedence.

Expand Down

0 comments on commit c70ea44

Please sign in to comment.