-
Notifications
You must be signed in to change notification settings - Fork 4
cse-machine-version independent from js-slang #50
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b883867
Implement translator feature
9762b2f
Implement translator feature
09a879f
Implement translator feature
f3b66ef
add commend on double to string
6a03161
cse-machine-utils
e236834
Merge branch 'main' into feature-csemachine-utils
WangYuyang1013 a91c061
cse-machine-utils fixed code error
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // astHelpers.ts | ||
| import type * as es from 'estree'; | ||
| import { StatementSequence } from './types'; | ||
| import { ControlItem } from './control'; | ||
|
|
||
| /** | ||
| * Create a StatementSequence node. | ||
| */ | ||
| export const statementSequence = ( | ||
| body: es.Statement[], | ||
| loc?: es.SourceLocation | null | ||
| ): StatementSequence => ({ | ||
| type: 'StatementSequence', | ||
| body, | ||
| loc, | ||
| innerComments: undefined, | ||
| }); | ||
|
|
||
| export const isNode = (item: any): item is es.Node => { | ||
| return typeof item === 'object' && item !== null && 'type' in item; | ||
| }; | ||
|
|
||
| export const isBlockStatement = (node: es.Node | StatementSequence): node is es.BlockStatement => { | ||
| return node.type === 'BlockStatement'; | ||
| }; | ||
|
|
||
| export const hasDeclarations = (node: es.BlockStatement): boolean => { | ||
| return node.body.some(stmt => stmt.type === 'VariableDeclaration' || stmt.type === 'FunctionDeclaration'); | ||
| }; | ||
|
|
||
| export const blockArrowFunction = ( | ||
| params: es.Identifier[], | ||
| body: es.Statement[] | es.BlockStatement | es.Expression, | ||
| loc?: es.SourceLocation | null | ||
| ): es.ArrowFunctionExpression => ({ | ||
| type: 'ArrowFunctionExpression', | ||
| expression: false, | ||
| generator: false, | ||
| params, | ||
| body: Array.isArray(body) ? blockStatement(body) : body, | ||
| loc | ||
| }) | ||
|
|
||
| export const blockStatement = ( | ||
| body: es.Statement[], | ||
| loc?: es.SourceLocation | null | ||
| ): es.BlockStatement => ({ | ||
| type: 'BlockStatement', | ||
| body, | ||
| loc | ||
| }) | ||
|
|
||
| export const constantDeclaration = ( | ||
| name: string, | ||
| init: es.Expression, | ||
| loc?: es.SourceLocation | null | ||
| ) => declaration(name, 'declaration', init, loc) | ||
|
|
||
| export const declaration = ( | ||
| name: string, | ||
| kind: AllowedDeclarations, | ||
| init: es.Expression, | ||
| loc?: es.SourceLocation | null | ||
| ): pyVariableDeclaration => ({ | ||
| type: 'VariableDeclaration', | ||
| declarations: [ | ||
| { | ||
| type: 'VariableDeclarator', | ||
| id: identifier(name), | ||
| init | ||
| } | ||
| ], | ||
| kind: 'declaration', | ||
| loc | ||
| }) | ||
|
|
||
| type AllowedDeclarations = 'declaration' | 'const' | ||
|
|
||
| export interface pyVariableDeclaration { | ||
| type: "VariableDeclaration"; | ||
| declarations: pyVariableDeclarator[]; | ||
| kind: "declaration" | "const"; | ||
| loc?: es.SourceLocation | null | undefined; | ||
| range?: [number, number] | undefined; | ||
| } | ||
|
|
||
| export interface pyVariableDeclarator { | ||
| type: "VariableDeclarator"; | ||
| id: Pattern; | ||
| init?: es.Expression | null | undefined; | ||
| } | ||
|
|
||
| export type Pattern = es.Identifier | es.ObjectPattern | es.ArrayPattern | es.RestElement | es.AssignmentPattern | es.MemberExpression; | ||
|
|
||
| export const identifier = (name: string, loc?: es.SourceLocation | null): es.Identifier => ({ | ||
| type: 'Identifier', | ||
| name, | ||
| loc | ||
| }) | ||
|
|
||
| export const returnStatement = ( | ||
| argument: es.Expression, | ||
| loc?: es.SourceLocation | null | ||
| ): es.ReturnStatement => ({ | ||
| type: 'ReturnStatement', | ||
| argument, | ||
| loc | ||
| }) | ||
|
|
||
| export const hasReturnStatement = (block: es.BlockStatement | StatementSequence): boolean => { | ||
| let hasReturn = false | ||
| for (const statement of block.body) { | ||
| if (isReturnStatement(statement)) { | ||
| hasReturn = true | ||
| } else if (isIfStatement(statement)) { | ||
| // Parser enforces that if/else have braces (block statement) | ||
| hasReturn = hasReturn || hasReturnStatementIf(statement as es.IfStatement) | ||
| } else if (isBlockStatement(statement) || isStatementSequence(statement)) { | ||
| hasReturn = hasReturn && hasReturnStatement(statement) | ||
| } | ||
| } | ||
| return hasReturn | ||
| } | ||
|
|
||
| export const isReturnStatement = (node: es.Node): node is es.ReturnStatement => { | ||
| return (node as es.ReturnStatement).type == 'ReturnStatement' | ||
| } | ||
|
|
||
| export const isIfStatement = (node: es.Node): node is es.IfStatement => { | ||
| return (node as es.IfStatement).type == 'IfStatement' | ||
| } | ||
|
|
||
| export const hasReturnStatementIf = (statement: es.IfStatement): boolean => { | ||
| let hasReturn = true | ||
| // Parser enforces that if/else have braces (block statement) | ||
| hasReturn = hasReturn && hasReturnStatement(statement.consequent as es.BlockStatement) | ||
| if (statement.alternate) { | ||
| if (isIfStatement(statement.alternate)) { | ||
| hasReturn = hasReturn && hasReturnStatementIf(statement.alternate as es.IfStatement) | ||
| } else if (isBlockStatement(statement.alternate) || isStatementSequence(statement.alternate)) { | ||
| hasReturn = hasReturn && hasReturnStatement(statement.alternate) | ||
| } | ||
| } | ||
| return hasReturn | ||
| } | ||
|
|
||
| export const isStatementSequence = (node: ControlItem): node is StatementSequence => { | ||
| return (node as StatementSequence).type == 'StatementSequence' | ||
| } | ||
|
|
||
| export const literal = ( | ||
| value: string | number | boolean | null, | ||
| loc?: es.SourceLocation | null | ||
| ): es.Literal => ({ | ||
| type: 'Literal', | ||
| value, | ||
| loc | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import * as es from 'estree' | ||
| import { Environment } from './environment' | ||
| import { Context } from './context' | ||
| import { StatementSequence } from './types' | ||
| import { blockArrowFunction, blockStatement, hasReturnStatement, identifier, isBlockStatement, returnStatement } from './ast-helper' | ||
| import { ControlItem } from './control' | ||
|
|
||
| export class Closure { | ||
| public originalNode?: es.ArrowFunctionExpression | ||
|
|
||
| /** Unique ID defined for closure */ | ||
| //public readonly id: string | ||
|
|
||
| /** Name of the constant declaration that the closure is assigned to */ | ||
| public declaredName?: string | ||
|
|
||
| constructor( | ||
| public node: es.ArrowFunctionExpression, | ||
| public environment: Environment, | ||
| public context: Context, | ||
| public predefined: boolean = false | ||
| ) { | ||
| this.originalNode = node | ||
| } | ||
|
|
||
| static makeFromArrowFunction( | ||
| node: es.ArrowFunctionExpression, | ||
| environment: Environment, | ||
| context: Context, | ||
| // TODO: Consider implementing a mechanism that more closely mimics Python’s implicit return (i.e., automatically inserting "return None") | ||
| dummyReturn: boolean = false, | ||
| predefined: boolean = false | ||
| ): Closure { | ||
| const functionBody: es.BlockStatement | StatementSequence = | ||
| !isBlockStatement(node.body) && !isStatementSequence(node.body) | ||
| ? blockStatement([returnStatement(node.body, node.body.loc)], node.body.loc) | ||
| : dummyReturn && !hasReturnStatement(node.body) | ||
| ? blockStatement( | ||
| [ | ||
| ...node.body.body, | ||
| returnStatement(identifier('undefined', node.body.loc), node.body.loc) | ||
| ], | ||
| node.body.loc | ||
| ) | ||
| : node.body | ||
|
|
||
| const closure = new Closure(blockArrowFunction(node.params as es.Identifier[], functionBody, node.loc), | ||
| environment, context, predefined) | ||
|
|
||
| closure.originalNode = node | ||
|
|
||
| return closure | ||
| } | ||
| } | ||
|
|
||
| export const isStatementSequence = (node: ControlItem): node is StatementSequence => { | ||
| return (node as StatementSequence).type == 'StatementSequence' | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is part of the CSE machine spec right? I don't think we need a dummyReturn in Python though. CPython just inserts a
return Noneimplicitly in the function.Nothing to improve here, just me grumbling :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's just mimicking CPython's implicit return of None? I'll add a todo for better implementation options.