Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

same-line-print-statement #292

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,420 changes: 2,150 additions & 270 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"devDependencies": {
"@parcel/packager-ts": "^2.2.0",
"@parcel/transformer-typescript-types": "^2.2.0",
"@types/node": "^20.10.6",
"prettier": "^2.5.1",
"turbo": "latest",
"typescript": "^4.5.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Visitor from ".";
import { ASTNode } from "bhai-lang-parser";

import InvalidStateException from "../../exceptions/invalidStateException";
import InterpreterModule from "../../module/interpreterModule";


export default class SameLinePrintStatement implements Visitor {
visitNode(node: ASTNode) {
if (!node.expressions)
throw new InvalidStateException(
`No expressions to print: ${node.expressions}`
);

const value = node.expressions
.map((expression: ASTNode) => {
let currentNodeOutput = InterpreterModule.getVisitor(expression.type).visitNode(expression);
if (currentNodeOutput === true)
currentNodeOutput = "sahi";
else if (currentNodeOutput === false)
currentNodeOutput = "galat";
return currentNodeOutput;
}
)
.join(" ");
process.stdout.write(value);
}
}
2 changes: 2 additions & 0 deletions packages/interpreter/src/module/interpreterModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import InitStatement from "../components/visitor/initStatement";
import NullLiteral from "../components/visitor/nullLiteral";
import NumericLiteral from "../components/visitor/numericLiteral";
import PrintStatement from "../components/visitor/printStatement";
import SameLinePrintStatement from "../components/visitor/sameLinePrintStatement";
import Program from "../components/visitor/program";
import StringLiteral from "../components/visitor/stringLiteral";
import VariableDeclaration from "../components/visitor/variableDeclaration";
Expand All @@ -30,6 +31,7 @@ export default class InterpreterModule {
[NodeType.Program]: new Program(),
[NodeType.InitStatement]: new InitStatement(),
[NodeType.PrintStatement]: new PrintStatement(),
[NodeType.SameLinePrintStatement]: new SameLinePrintStatement(),
[NodeType.EmptyStatement]: new EmptyStatement(),
[NodeType.BlockStatement]: new BlockStatement(),
[NodeType.VariableStatement]: new VariableStatement(),
Expand Down
3 changes: 3 additions & 0 deletions packages/parser/src/components/parser/statement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export default abstract class Statement {

static getStatementImpl(lookahead: Token): Statement {
switch (lookahead.type) {
case TokenTypes.BOL_BHAI_ABHI_TYPE:
return BhaiLangModule.getSameLinePrintStatement();

case TokenTypes.BOL_BHAI_TYPE:
return BhaiLangModule.getPrintStatement();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Statement from ".";

import { TokenTypes } from "../../../constants/bhaiLangSpec";
import { NodeType } from "../../../constants/constants";
import { ASTNode } from "../types/nodeTypes";

import Expression from "./expression";


export default class SameLinePrintStatement extends Statement {
getStatement(): ASTNode {
this._tokenExecutor.eatTokenAndForwardLookahead(TokenTypes.BOL_BHAI_ABHI_TYPE);

const expressions = this._getExpressionList();

this._tokenExecutor.eatTokenAndForwardLookahead(TokenTypes.SEMI_COLON_TYPE);

return {
type: NodeType.SameLinePrintStatement,
expressions,
};
}

private _getExpressionList() {
const expressions: any[] = [];

do {
expressions.push(this._getExpression());
} while (
this._tokenExecutor.getLookahead()?.type === TokenTypes.COMMA_TYPE &&
this._tokenExecutor.eatTokenAndForwardLookahead(TokenTypes.COMMA_TYPE)
);

return expressions;
}

private _getExpression() {
return Expression.getExpressionImpl(
NodeType.AssignmentExpression
).getExpression();
}
}
2 changes: 2 additions & 0 deletions packages/parser/src/constants/bhaiLangSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const TokenTypes = {

BOL_BHAI_TYPE: "bol bhai",

BOL_BHAI_ABHI_TYPE: "bol bhai abhi",

BHAI_YE_HAI_TYPE: "bhai ye hai",

AGAR_BHAI: "agar bhai",
Expand Down
1 change: 1 addition & 0 deletions packages/parser/src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const NodeType = {
ExpressionStatement: "ExpressionStatement",
InitStatement: "InitStatement",
PrintStatement: "PrintStatement",
SameLinePrintStatement: "SameLinePrintStatement",
IfStatement: "IfStatement",
WhileStatement: "WhileStatement",
BreakStatement: "BreakStatement",
Expand Down
11 changes: 11 additions & 0 deletions packages/parser/src/module/bhaiLangModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import ExpressionStatement
import IfStatement from "../components/parser/statement/ifStatement";
import InitStatement from "../components/parser/statement/initStatement";
import PrintStatement from "../components/parser/statement/printStatement";
import SameLinePrintStatement from "../components/parser/statement/sameLinePrintStatement";
import VariableStatement
from "../components/parser/statement/variableStatement";
import WhileStatement from "../components/parser/statement/whileStatement";
Expand All @@ -49,6 +50,7 @@ import { SPEC } from "../constants/bhaiLangSpec";


export default class BhaiLangModule {

private static _tokenizer?: Tokenizer;
private static _initStatement?: InitStatement;
private static _parser?: Parser;
Expand All @@ -57,6 +59,7 @@ export default class BhaiLangModule {
private static _tokenExecutor?: TokenExecutor;
private static _expresionStatement?: ExpressionStatement;
private static _printStatement?: PrintStatement;
private static _sameLinePrintStatement?: SameLinePrintStatement;
private static _emptyStatement?: EmptyStatement;
private static _blockStatement?: BlockStatement;
private static _additiveExpression?: AdditiveExpression;
Expand Down Expand Up @@ -117,6 +120,14 @@ export default class BhaiLangModule {
return this._printStatement;
}

static getSameLinePrintStatement(){
if (!this._sameLinePrintStatement) {
this._sameLinePrintStatement = new SameLinePrintStatement(this.getTokenExecutor());
}

return this._sameLinePrintStatement;
}

static getIfStatement() {
if (!this._ifStatement) {
this._ifStatement = new IfStatement(this.getTokenExecutor());
Expand Down