Skip to content

Commit

Permalink
feat: add grammer for Statements
Browse files Browse the repository at this point in the history
Co-authored-by: Samyak S Sarnayak <[email protected]>
  • Loading branch information
ishwar00 and Samyak2 committed Jun 21, 2023
1 parent cf25411 commit 505f3e4
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions compiler/src/bakugo.pest
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,6 @@ COMMENT = _{ "/*" ~ (!("*/" | NewLine) ~ ANY)* ~ "*/" }

Ident = @{ Letter ~ (Letter | UnicodeDigit)* }

Add = { "+" }
Sub = { "-" }
Mul = { "*" }
Quo = { "/" }
Rem = { "%" }

Or = { "|" }
And = { "&" }
Not = { "!" }

Inc = { "++" }
Dec = { "--" }
Land = { "&&" }
Lor = { "||" }

Eql = { "==" }
Neq = { "!=" }
Lss = { "<" }
Gtr = { ">" }
Leq = { "<=" }
Geq = { ">=" }

Assign = { "=" }
Comma = { "," }
Semicolon = { ";" }

Lparen = { "(" }
Lbrace = { "{" }

Rparen = { ")" }
Rbrace = { "}" }

IntLit = @{ "0" | ('1'..'9' ~ ("_"? ~ DecimalDigits)?) }
DecimalDigits = @{ DecimalDigit ~ ("_"? ~ DecimalDigit)* }

Expand Down Expand Up @@ -149,11 +117,13 @@ BasicLit = { IntLit | RuneLit | StringLit }
OperandName = { Ident }

// Primary expressions
// FIXME: PrimaryExpr is left-recursive
PrimaryExpr = { Operand | PrimaryExpr ~ Arguments }

Arguments = { "(" ~ (((Type ~ ("," ~ ExpressionList)?) | ExpressionList) ~ ","?)? ~ ")" }

// Operators
// FIXME: PrimaryExpr is left-recursive
Expression = { UnaryExpr | Expression ~ BinaryOp ~ Expression }
UnaryExpr = { (UnaryOp ~ UnaryExpr) | PrimaryExpr }

Expand All @@ -164,3 +134,19 @@ MulOp = { "*" | "/" | "%" | "&" }

UnaryOp = { "+" | "-" | "!" | "*" | "&" }

// Statements

Statement = { Declaration | SimpleStmt | ReturnStmt | Block | IfStmt }

SimpleStmt = { ExpressionStmt | IncDecStmt | Assignment }

ExpressionStmt = { Expression }

IncDecStmt = { Expression ~ ( "++" | "--" ) }

Assignment = { ExpressionList ~ "=" ~ ExpressionList }

IfStmt = { "if" ~ (SimpleStmt ~ ";")? ~ Expression ~ Block ~ ("else" ~ (IfStmt | Block ))? }

ReturnStmt = { "return" ~ ExpressionList? }

0 comments on commit 505f3e4

Please sign in to comment.