Skip to content

Commit

Permalink
shin-asm: handle parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
DCNick3 committed Sep 1, 2023
1 parent eeec1d1 commit 347c797
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion shin-asm/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ LABEL_3:
LABEL_4:
// eh, parser seems to get stuck on parenthesis
exp $result, 1 * $2 + $3 & 7
exp $result, 1 * ($2 + $3 & 7)
"#
.to_string(),
);
Expand Down
1 change: 1 addition & 0 deletions shin-asm/src/hir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl<'a> BlockCollector<'a> {
self.alloc_expr(Expr::NameRef(e.ident().unwrap().text().into()), ptr)
}
ast::Expr::RegisterRefExpr(e) => self.alloc_expr(Expr::RegisterRef(e.value()), ptr),
ast::Expr::ParenExpr(e) => self.collect_expr_opt(e.expr()), // TODO: handle reverse source map
ast::Expr::ArrayExpr(e) => {
let mut values = Vec::new();
for value in e.values() {
Expand Down
12 changes: 12 additions & 0 deletions shin-asm/src/parser/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub(super) fn atom_expr(p: &mut Parser<'_>) -> Option<CompletedMarker> {
p.bump(REGISTER_IDENT);
m.complete(p, REGISTER_REF_EXPR)
}
T!['('] => paren_expr(p),
T!['['] => array_expr(p),
T!['{'] => mapping_expr(p),
_ => {
Expand All @@ -46,6 +47,17 @@ pub(super) fn atom_expr(p: &mut Parser<'_>) -> Option<CompletedMarker> {
Some(done)
}

fn paren_expr(p: &mut Parser<'_>) -> CompletedMarker {
assert!(p.at(T!['(']));
let m = p.start();

p.bump(T!['(']);
expr(p);
p.expect(T![')']);

m.complete(p, PAREN_EXPR)
}

fn array_expr(p: &mut Parser<'_>) -> CompletedMarker {
assert!(p.at(T!['[']));
let m = p.start();
Expand Down
1 change: 1 addition & 0 deletions shin-asm/src/parser/syntax_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ syntax_kind! {
CALL_EXPR,
CALL_EXPR_ARG_LIST,

PAREN_EXPR,
ARRAY_EXPR,

MAPPING_EXPR,
Expand Down
20 changes: 20 additions & 0 deletions shin-asm/src/syntax/ast/nodes/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Expr {
#[ast(transparent)]
RegisterRefExpr(RegisterRefExpr),
#[ast(transparent)]
ParenExpr(ParenExpr),
#[ast(transparent)]
ArrayExpr(ArrayExpr),
#[ast(transparent)]
MappingExpr(MappingExpr),
Expand Down Expand Up @@ -51,6 +53,24 @@ impl RegisterRefExpr {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, AstNode)]
#[ast(kind = ARRAY_EXPR)]
pub struct ParenExpr {
pub(crate) syntax: SyntaxNode,
}

impl ParenExpr {
pub fn l_paren_token(&self) -> Option<LParen> {
support::token(self.syntax())
}
pub fn expr(&self) -> Option<Expr> {
support::child(self.syntax())
}
pub fn r_paren_token(&self) -> Option<RParen> {
support::token(self.syntax())
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, AstNode)]
#[ast(kind = ARRAY_EXPR)]
pub struct ArrayExpr {
Expand Down
12 changes: 12 additions & 0 deletions shin-asm/src/syntax/ast/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ impl RegisterIdent {
todo!()
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, AstToken)]
#[ast(kind = L_PAREN)]
pub struct LParen {
pub(crate) syntax: SyntaxToken,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, AstToken)]
#[ast(kind = R_PAREN)]
pub struct RParen {
pub(crate) syntax: SyntaxToken,
}
2 changes: 1 addition & 1 deletion shin-asm/test_data/parser/ok/0017_parenthesis.sal
Original file line number Diff line number Diff line change
@@ -1 +1 @@
exp $result, (1 * $2 + $3 & 7)
HELLO (2 + 2)
18 changes: 18 additions & 0 deletions shin-asm/test_data/parser/ok/0017_parenthesis.sast
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SOURCE_FILE
INSTRUCTIONS_BLOCK
INSTRUCTION
INSTRUCTION_NAME
IDENT "HELLO"
WHITESPACE " "
INSTR_ARG_LIST
PAREN_EXPR
L_PAREN "("
BIN_EXPR
LITERAL
INT_NUMBER "2"
WHITESPACE " "
PLUS "+"
WHITESPACE " "
LITERAL
INT_NUMBER "2"
R_PAREN ")"

0 comments on commit 347c797

Please sign in to comment.