Skip to content

Commit

Permalink
fix: Bug fixes, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 29, 2023
1 parent 1a2f537 commit 3941a6c
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 4 deletions.
17 changes: 14 additions & 3 deletions server/src/lang/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ impl<'a> Parser<'a> {

fn loop_statement(&mut self) -> ParseResult<StmtId> {
let loop_tok = self.peek_bw(1);
let inner_stmt = self.declaration()?;
self.expected(sym!(LeftBrace))?;
let inner_stmt = self.block()?;
self.match_next(sym!(Semicolon));
Ok(self.arena.statement(Stmt::Loop {
condition: None,
body: inner_stmt,
Expand All @@ -180,7 +182,9 @@ impl<'a> Parser<'a> {
self.expected(sym!(LeftParen))?;
let condition = self.expression()?;
self.expected(sym!(RightParen))?;
let inner_stmt = self.declaration()?;
self.expected(sym!(LeftBrace))?;
let inner_stmt = self.block()?;
self.match_next(sym!(Semicolon));

Ok(self.arena.statement(Stmt::Loop {
condition: Some(condition),
Expand Down Expand Up @@ -240,7 +244,9 @@ impl<'a> Parser<'a> {
}))
};

let inner_stmt = self.declaration()?;
self.expected(sym!(LeftBrace))?;
let inner_stmt = self.block()?;
self.match_next(sym!(Semicolon));

if initializer.is_none() {
return Ok(self.arena.statement(Stmt::Loop {
Expand Down Expand Up @@ -883,6 +889,11 @@ impl<'a> Parser<'a> {
raw: "null".to_string(),
span: tok.span,
})),
TokenType::Undefined => Ok(self.arena.expression(Expr::Literal {
value: Literal::Undefined,
raw: "undefined".to_string(),
span: tok.span,
})),
Str | Num => Ok(self.arena.expression(Expr::Literal {
value: tok.literal.clone().unwrap(),
raw: tok.lexeme.clone().unwrap(),
Expand Down
217 changes: 217 additions & 0 deletions server/src/lang/tests/generic/loops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
#[cfg(test)]
use crate::lang::tests::helpers::compare_parsed_to_expected;

#[cfg(test)]
use serde_json::json;

#[cfg(test)]
use crate::assert_parsing;

#[cfg(test)]
assert_parsing! {
loop_0: {
"loop {}" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Loop",
"condition": null,
"post": null,
"body": {
"type": "Stmt::Block",
"body": [],
}
}
]
}
},
loop_1: {
"loop { print(1); }" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Loop",
"condition": null,
"post": null,
"body": {
"type": "Stmt::Block",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Call",
"callee": {
"type": "Expr::Variable",
"name": "print"
},
"args": [
{
"type": "Expr::Literal",
"value": "Num(1.0)",
"raw": "1"
}
]
}
}
]
}
}
]
}
},
for_0: {
"for (var $i = 0; $i < 10; $i = $i + 1) {}" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Block",
"body": [
{
"type": "Stmt::Declaration",
"variable": "$i",
"expr": {
"raw": "0",
"type": "Expr::Literal",
"value": "Num(0.0)"
},
},
{
"type": "Stmt::Loop",
"body": {
"type": "Stmt::Block",
"body": [],
},
"condition": {
"left": {
"name": "$i",
"type": "Expr::Variable"
},
"operation": "Less",
"right": {
"raw": "10",
"type": "Expr::Literal",
"value": "Num(10.0)"
},
"type": "Expr::Binary"
},
"post": {
"type": "Stmt::Expression",
"expr": {
"dst": "$i",
"type": "Expr::Assignment",
"expr": {
"type": "Expr::Binary",
"left": {
"name": "$i",
"type": "Expr::Variable"
},
"operation": "Add",
"right": {
"raw": "1",
"type": "Expr::Literal",
"value": "Num(1.0)"
},
},
},
},
}
]
}
]
}
},
for_1: {
"for (var $i = 0; $i < 10; $i = $i + 1) { print($i); }" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Block",
"body": [
{
"type": "Stmt::Declaration",
"variable": "$i",
"expr": {
"raw": "0",
"type": "Expr::Literal",
"value": "Num(0.0)"
},
},
{
"type": "Stmt::Loop",
"body": {
"type": "Stmt::Block",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Call",
"callee": {
"type": "Expr::Variable",
"name": "print"
},
"args": [
{
"type": "Expr::Variable",
"name": "$i"
}
]
}
}
],
},
"condition": {
"left": {
"name": "$i",
"type": "Expr::Variable"
},
"operation": "Less",
"right": {
"raw": "10",
"type": "Expr::Literal",
"value": "Num(10.0)"
},
"type": "Expr::Binary"
},
"post": {
"type": "Stmt::Expression",
"expr": {
"dst": "$i",
"type": "Expr::Assignment",
"expr": {
"type": "Expr::Binary",
"left": {
"name": "$i",
"type": "Expr::Variable"
},
"operation": "Add",
"right": {
"raw": "1",
"type": "Expr::Literal",
"value": "Num(1.0)"
},
},
},
},
}
]
}
]
}
},
for_empty: {
"for (;;) {}" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Loop",
"body": {
"type": "Stmt::Block",
"body": [],
},
"condition": null,
"post": null,
}
]
}
}
}
2 changes: 2 additions & 0 deletions server/src/lang/tests/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ pub mod array_literal;
pub mod binary;
pub mod function_call;
pub mod grouping;
pub mod loops;
pub mod number_literal;
pub mod object_literal;
pub mod other_literals;
pub mod unary;
pub mod variable;
75 changes: 75 additions & 0 deletions server/src/lang/tests/generic/number_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,80 @@ assert_parsing! {
}
]
}
},
number_floating: {
"4.0;" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Literal",
"value": "Num(4.0)",
"raw": "4.0"
}
}
]
}
},
number_e: {
"1e2;" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Literal",
"value": "Num(100.0)",
"raw": "1e2"
}
}
]
}
},
number_e_floating_0: {
"1.7976931348623157E+308;" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Literal",
"value": "Num(1.7976931348623157e308)",
"raw": "1.7976931348623157E+308"
}
}
]
}
},
number_e_floating_1: {
"1.7976931348623157E308;" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Literal",
"value": "Num(1.7976931348623157e308)",
"raw": "1.7976931348623157E308"
}
}
]
}
},
number_e_floating_2: {
"1.7976931348623155E-308;" => {
"type": "Stmt::Program",
"body": [
{
"type": "Stmt::Expression",
"expr": {
"type": "Expr::Literal",
"value": "Num(1.7976931348623155e-308)",
"raw": "1.7976931348623155E-308"
}
}
]
}
}
}
Loading

0 comments on commit 3941a6c

Please sign in to comment.