Skip to content

Commit

Permalink
Merge pull request #16 from source-academy/cse/assignment
Browse files Browse the repository at this point in the history
ECE - Evaluate Assignment
  • Loading branch information
xyliew25 authored Jan 29, 2024
2 parents e33afaa + d6d6568 commit 6066a6a
Show file tree
Hide file tree
Showing 17 changed files with 917 additions and 536 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# compiled output
/dist
/node_modules
tsconfig.tsbuildinfo

# Logs
logs
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"repository": "https://github.com/source-academy/java-slang.git",
"license": "Apache-2.0",
"scripts": {
"build": "tsc --build --force",
"test": "jest",
"test:watch": "jest --watch"
},
Expand Down
56 changes: 45 additions & 11 deletions src/ast/astExtractor/block-statement-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
BinaryExpressionCtx,
BlockStatementCstNode,
ExpressionCtx,
FqnOrRefTypeCtx,
FqnOrRefTypePartCommonCtx,
FqnOrRefTypePartFirstCtx,
IToken,
IntegerLiteralCtx,
IntegralTypeCtx,
Expand All @@ -22,30 +25,41 @@ import {
BinaryExpression,
BlockStatement,
Expression,
ExpressionName,
} from "../types/blocks-and-statements";

export class BlockStatementExtractor extends BaseJavaCstVisitorWithDefaults {
private type: UnannType;
private identifier: Identifier;
private value: Expression;
private name: ExpressionName;

constructor() {
super();
}

extract(cst: BlockStatementCstNode): BlockStatement {
this.visit(cst);
return {
kind: "LocalVariableDeclarationStatement",
localVariableType: this.type,
variableDeclaratorList: [
{
kind: "VariableDeclarator",
variableDeclaratorId: this.identifier,
variableInitializer: this.value,
}
],
};
if (cst.children.localVariableDeclarationStatement) {
return {
kind: "LocalVariableDeclarationStatement",
localVariableType: this.type,
variableDeclaratorList: [
{
kind: "VariableDeclarator",
variableDeclaratorId: this.identifier,
variableInitializer: this.value,
},
],
};
} else {
return {
kind: "Assignment",
left: this.name,
operator: "=",
right: this.value,
};
}
}

integralType(ctx: IntegralTypeCtx) {
Expand Down Expand Up @@ -77,6 +91,9 @@ export class BlockStatementExtractor extends BaseJavaCstVisitorWithDefaults {
binaryExpression(ctx: BinaryExpressionCtx) {
if (ctx.BinaryOperator && ctx.BinaryOperator.length > 0) {
return this.makeBinaryExpression(ctx.BinaryOperator, ctx.unaryExpression);
} else if (ctx.AssignmentOperator && ctx.expression) {
this.value = this.visit(ctx.expression);
this.name = this.visit(ctx.unaryExpression[0]);
} else {
return this.visit(ctx.unaryExpression[0]);
}
Expand Down Expand Up @@ -183,9 +200,26 @@ export class BlockStatementExtractor extends BaseJavaCstVisitorWithDefaults {
return this.visit(ctx.literal);
} else if (ctx.parenthesisExpression) {
return this.visit(ctx.parenthesisExpression);
} else if (ctx.fqnOrRefType) {
return this.visit(ctx.fqnOrRefType);
}
}

fqnOrRefType(ctx: FqnOrRefTypeCtx) {
return this.visit(ctx.fqnOrRefTypePartFirst);
}

fqnOrRefTypePartFirst(ctx: FqnOrRefTypePartFirstCtx) {
return this.visit(ctx.fqnOrRefTypePartCommon);
}

fqnOrRefTypePartCommon(ctx: FqnOrRefTypePartCommonCtx) {
return ctx.Identifier && {
kind: "ExpressionName",
name: ctx.Identifier[0].image,
};
}

literal(ctx: LiteralCtx) {
if (ctx.integerLiteral) {
return this.visit(ctx.integerLiteral);
Expand Down
2 changes: 2 additions & 0 deletions src/ast/types/ast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CompilationUnit } from "./packages-and-modules";
import {
Assignment,
Block,
BlockStatement,
Expression,
Expand All @@ -10,6 +11,7 @@ interface NodeMap {
Block: Block;
BlockStatement: BlockStatement;
Expression: Expression;
Assignment: Assignment;
}

export type Node = NodeMap[keyof NodeMap];
2 changes: 1 addition & 1 deletion src/ast/types/blocks-and-statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type LocalVariableType = UnannType;
export interface VariableDeclarator {
kind: "VariableDeclarator";
variableDeclaratorId: VariableDeclaratorId;
variableInitializer: VariableInitializer;
variableInitializer?: VariableInitializer;
}

export type VariableDeclaratorId = Identifier;
Expand Down
Loading

0 comments on commit 6066a6a

Please sign in to comment.