Skip to content

Commit

Permalink
feat: float values support #103 (#230)
Browse files Browse the repository at this point in the history
* feat: float values support

* float value test added for parser and interpreter

* feat-float-values: number regex modified, invalid parsing tests added

* package json added

* turbo command added

* feat-float_values: turbo version changed to latest

* fix: revert package-lock

Co-authored-by: Aniket Singh <aniketsingh0104@gmail.com>
  • Loading branch information
harshkanjariya and aniketsingh0104 committed Apr 17, 2022
1 parent dd076b3 commit de68714
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions packages/interpreter/test/integration/positiveTestsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,26 @@ export const WithOutputPositiveTests = [
`,
output: "nahi galat",
},
{
name: "float value addition with integer value test, should success",
input: `
hi bhai
bhai ye hai a = 1.2, b = 2;
bol bhai a + b;
bye bhai
`,
output: "3.2"
},
{
name: "float value addition with float value value test, should success",
input: `
hi bhai
bhai ye hai a = 1.2, b = 2.3;
bol bhai a + b;
bye bhai
`,
output: "3.5"
},
{
name: "printStatement test with multiple expressions, should success",
input: `
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/constants/bhaiLangSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const SPEC = [
{ regex: /^\bagla dekh bhai\b/, tokenType: TokenTypes.AGLA_DEKH_BHAI },

// Number
{ regex: /^-?\d+/, tokenType: TokenTypes.NUMBER_TYPE },
{ regex: /^[+-]?([\d]*[.])?[\d]+/, tokenType: TokenTypes.NUMBER_TYPE },

// Boolean
{ regex: /^\bsahi\b/, tokenType: TokenTypes.BOOLEAN_TYPE },
Expand Down
28 changes: 28 additions & 0 deletions packages/parser/test/integration/negativeTestsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,34 @@ export const NegativeExpressionsTests = [
`,
output: SyntaxError,
},
// float expression test
{
name: "float expression test with multiple continuous decimal points - 1, should throw an exception",
input: `
hi bhai
bhai ye hai a = 1..2;
bye bhai;
`,
output: SyntaxError,
},
{
name: "float expression test with multiple continuous decimal points - 2, should throw an exception",
input: `
hi bhai
bhai ye hai a = ..2;
bye bhai;
`,
output: SyntaxError,
},
{
name: "float expression test with multiple decimal points at different places, should throw an exception",
input: `
hi bhai
bhai ye hai a = 1.2.3;
bye bhai;
`,
output: SyntaxError,
},
];

export const IfStatementNagativeTests = [
Expand Down
36 changes: 36 additions & 0 deletions packages/parser/test/integration/positiveTestsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,42 @@ export const ExpressionsTests = [
`,
output: `{\"type\":\"Program\",\"body\":{\"type\":\"InitStatement\",\"body\":[{\"type\":\"ExpressionStatement\",\"expression\":{\"type\":\"AssignmentExpression\",\"operator\":\"*=\",\"left\":{\"type\":\"IdentifierExpression\",\"name\":\"a\"},\"right\":{\"type\":\"NumericLiteral\",\"value\":4}}}]}}`,
},
{
name: "float number assignment expression test, should success",
input: `
hi bhai
a = 1.2;
bye bhai
`,
output: `{\"type\":\"Program\",\"body\":{\"type\":\"InitStatement\",\"body\":[{\"type\":\"ExpressionStatement\",\"expression\":{\"type\":\"AssignmentExpression\",\"operator\":\"=\",\"left\":{\"type\":\"IdentifierExpression\",\"name\":\"a\"},\"right\":{\"type\":\"NumericLiteral\",\"value\":1.2}}}]}}`,
},
{
name: "float number assignment expression test with negative number, should success",
input: `
hi bhai
a = -1.2;
bye bhai
`,
output: `{\"type\":\"Program\",\"body\":{\"type\":\"InitStatement\",\"body\":[{\"type\":\"ExpressionStatement\",\"expression\":{\"type\":\"AssignmentExpression\",\"operator\":\"=\",\"left\":{\"type\":\"IdentifierExpression\",\"name\":\"a\"},\"right\":{\"type\":\"NumericLiteral\",\"value\":-1.2}}}]}}`,
},
{
name: "float number assignment expression test without digits before decimal point, should success",
input: `
hi bhai
a = .6;
bye bhai
`,
output: `{\"type\":\"Program\",\"body\":{\"type\":\"InitStatement\",\"body\":[{\"type\":\"ExpressionStatement\",\"expression\":{\"type\":\"AssignmentExpression\",\"operator\":\"=\",\"left\":{\"type\":\"IdentifierExpression\",\"name\":\"a\"},\"right\":{\"type\":\"NumericLiteral\",\"value\":0.6}}}]}}`,
},
{
name: "float number assignment expression test with negative number and without digits before decimal point, should success",
input: `
hi bhai
a = -.6;
bye bhai
`,
output: `{\"type\":\"Program\",\"body\":{\"type\":\"InitStatement\",\"body\":[{\"type\":\"ExpressionStatement\",\"expression\":{\"type\":\"AssignmentExpression\",\"operator\":\"=\",\"left\":{\"type\":\"IdentifierExpression\",\"name\":\"a\"},\"right\":{\"type\":\"NumericLiteral\",\"value\":-0.6}}}]}}`,
},
// paranthesized expression tests
{
name: "paranthesized expression test with one parenthesis and simple expression, should success",
Expand Down

1 comment on commit de68714

@PulkitGupta707
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bhai kya bnaya hai LMAO XDDDDD

Please sign in to comment.