diff --git a/frontend/components/text-area.tsx b/frontend/components/text-area.tsx index 2821598..9861469 100644 --- a/frontend/components/text-area.tsx +++ b/frontend/components/text-area.tsx @@ -53,7 +53,7 @@ const Cursor: React.SFC = (props) => { return (
) } -interface TextAreaProps extends EditorState { +export interface TextAreaProps extends EditorState { setCursorPosition: typeof _setCursorPosition, addSignAfterCursor: typeof _addSignAfterCursor, moveCursor: typeof _moveCursor, @@ -172,7 +172,6 @@ class TextArea extends React.Component { } } -export { TextAreaProps } export default connect( (state: State) => ({ ...state.editor, diff --git a/frontend/lib/editor.ts b/frontend/lib/editor.ts index 3fe9d61..08d56be 100644 --- a/frontend/lib/editor.ts +++ b/frontend/lib/editor.ts @@ -1,11 +1,8 @@ import { Text } from '../redux/editor/types' import { Sign } from './sign' -function CodeToString(text: Text): string { +export function CodeToString(text: Text): string { return text.map( (row) => row.letters.map((letter) => `${Sign[letter.sign]};`).join('') ).join('\n') } - -export { CodeToString } - diff --git a/frontend/lib/sign.ts b/frontend/lib/sign.ts index bd06aa8..e62e408 100644 --- a/frontend/lib/sign.ts +++ b/frontend/lib/sign.ts @@ -1,4 +1,4 @@ -enum Sign { +export enum Sign { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, _, @@ -11,6 +11,3 @@ enum Sign { CLASS, FUNCTION, RETURN, CONTINUE, BREAK, SPACE, SEMICOLON, QUOTE, COMMA, PROP, } - -export { Sign } - diff --git a/frontend/lib/text-area.ts b/frontend/lib/text-area.ts index cdc017b..887795d 100644 --- a/frontend/lib/text-area.ts +++ b/frontend/lib/text-area.ts @@ -7,7 +7,7 @@ function CreateId(): string { return Math.random().toString(36).substr(2, 5) } -class LetterImp implements Letter { +export class LetterImp implements Letter { [immerable] = true readonly id: string constructor(readonly sign: Sign) { @@ -15,7 +15,7 @@ class LetterImp implements Letter { } } -class LetterRowImp implements LetterRow { +export class LetterRowImp implements LetterRow { [immerable] = true readonly id: string constructor(readonly letters: Letter[]) { @@ -23,7 +23,7 @@ class LetterRowImp implements LetterRow { } } -function TrimPositionRowCol(position: PositionRowCol, text: Text): +export function TrimPositionRowCol(position: PositionRowCol, text: Text): PositionRowCol { const rowLength = (text.length > 0) ? text.length - 1 : 0 const row = Math.min(rowLength, position.row) @@ -32,7 +32,7 @@ function TrimPositionRowCol(position: PositionRowCol, text: Text): return { row, col } } -function PositionPixelsToRowCol( +export function PositionPixelsToRowCol( letterSize: LetterSize, position: PositionPixels): PositionRowCol { const boxSize = letterSize.edgePx + 2 * letterSize.marginPx const row = Math.floor(position.y / boxSize) @@ -41,7 +41,7 @@ function PositionPixelsToRowCol( return { row, col } } -function PositionRowColToPixels( +export function PositionRowColToPixels( letterSize: LetterSize, position: PositionRowCol): PositionPixels { const boxSize = letterSize.edgePx + 2 * letterSize.marginPx const x = position.col * boxSize @@ -50,7 +50,8 @@ function PositionRowColToPixels( } // Expects cursor to be in a valid state. -function MoveCursorRight(position: PositionRowCol, text: Text): PositionRowCol { +export function MoveCursorRight( + position: PositionRowCol, text: Text): PositionRowCol { const rowWidth = text[position.row].letters.length if (position.col + 1 <= rowWidth) { return { row: position.row, col: position.col + 1 } @@ -62,7 +63,8 @@ function MoveCursorRight(position: PositionRowCol, text: Text): PositionRowCol { } // Expects cursor to be in a valid state. -function MoveCursorLeft(position: PositionRowCol, text: Text): PositionRowCol { +export function MoveCursorLeft( + position: PositionRowCol, text: Text): PositionRowCol { if (position.col - 1 >= 0) { return { row: position.row, col: position.col - 1 } } else if (position.row - 1 >= 0) { @@ -73,7 +75,8 @@ function MoveCursorLeft(position: PositionRowCol, text: Text): PositionRowCol { } // Expects cursor to be in a valid state. -function MoveCursorUp(position: PositionRowCol, text: Text): PositionRowCol { +export function MoveCursorUp( + position: PositionRowCol, text: Text): PositionRowCol { const rowUp = position.row - 1 if (rowUp >= 0) { return { @@ -89,7 +92,8 @@ function MoveCursorUp(position: PositionRowCol, text: Text): PositionRowCol { } // Expects cursor to be in a valid state. -function MoveCursorDown(position: PositionRowCol, text: Text): PositionRowCol { +export function MoveCursorDown( + position: PositionRowCol, text: Text): PositionRowCol { const rowDown = position.row + 1 if (rowDown < text.length) { return { @@ -104,7 +108,3 @@ function MoveCursorDown(position: PositionRowCol, text: Text): PositionRowCol { } } } - - -export { LetterImp, LetterRowImp, PositionPixelsToRowCol, PositionRowColToPixels, TrimPositionRowCol, MoveCursorRight, MoveCursorLeft, MoveCursorDown, MoveCursorUp } - diff --git a/frontend/redux/configure-store.ts b/frontend/redux/configure-store.ts index d904bb6..f10e95b 100644 --- a/frontend/redux/configure-store.ts +++ b/frontend/redux/configure-store.ts @@ -133,8 +133,4 @@ const rootReducer = ( else return action.reducer(state) } - -const configureStore = () => createStore(rootReducer, getInitialState()) - -export { configureStore } - +export const configureStore = () => createStore(rootReducer, getInitialState()) diff --git a/frontend/redux/editor/actions.ts b/frontend/redux/editor/actions.ts index 18a5e89..44d01c3 100644 --- a/frontend/redux/editor/actions.ts +++ b/frontend/redux/editor/actions.ts @@ -9,7 +9,7 @@ import { LetterImp, LetterRowImp, MoveCursorDown, MoveCursorLeft, MoveCursorRigh import { MyAction, State } from '../types' import { PositionPixels, RunState } from './types' -const setCursorPosition = +export const setCursorPosition = (positionPixels: PositionPixels): MyAction => ({ type: 'Set cursor position', payload: positionPixels, @@ -22,7 +22,7 @@ const setCursorPosition = }) }) -const addSignAfterCursor = +export const addSignAfterCursor = (sign: Sign): MyAction => ({ type: 'Add sign after cursor', payload: sign, @@ -35,9 +35,9 @@ const addSignAfterCursor = }) }) -enum Direction { UP, RIGHT, DOWN, LEFT } +export enum Direction { UP, RIGHT, DOWN, LEFT } -const moveCursor = +export const moveCursor = (direction: Direction): MyAction => ({ type: 'Move cursor', payload: direction, @@ -61,7 +61,7 @@ const moveCursor = }) }) -const removeAfterCursor = +export const removeAfterCursor = (): MyAction => ({ type: 'Remove after cursor', reducer: produce((state: State) => { @@ -78,7 +78,7 @@ const removeAfterCursor = }) }) -const newlineAfterCursor = +export const newlineAfterCursor = (): MyAction => ({ type: 'Newline after cursor', reducer: produce((state: State) => { @@ -92,7 +92,7 @@ const newlineAfterCursor = }) }) -const appendToOutput = +export const appendToOutput = (output: string): MyAction => ({ type: 'Append to output', payload: output, @@ -101,7 +101,7 @@ const appendToOutput = }) }) -const finishExecution = +export const finishExecution = (): MyAction => ({ type: 'Execution finished', reducer: produce((state: State) => { @@ -114,11 +114,12 @@ const finishExecution = }) }) -type NextStepArgs = { +export type NextStepArgs = { resolveNextStep: () => void, lineno: number } -const newNextStep = + +export const newNextStep = (args: NextStepArgs): MyAction => ({ type: 'New next step', payload: args, @@ -130,7 +131,7 @@ const newNextStep = }) -const giflangSetup: GiflangSetup = { +export const giflangSetup: GiflangSetup = { onPrint: (str: string) => { storeInstance.dispatch(appendToOutput(str))}, onFinish: @@ -145,7 +146,7 @@ const giflangSetup: GiflangSetup = { } } -const executionStarted = +export const executionStarted = (worker: Worker, isDebugMode: boolean): MyAction => ({ type: 'Execution started', reducer: produce((state: State) => { @@ -155,7 +156,7 @@ const executionStarted = }) }) -async function StartExecution(code: string, isDebugMode: boolean) { +export async function StartExecution(code: string, isDebugMode: boolean) { const vanillaWorker = new Worker() const workerProxy = Comlink.wrap< @@ -169,7 +170,7 @@ async function StartExecution(code: string, isDebugMode: boolean) { worker.run(code) } -const startExecution = +export const startExecution = (isDebugMode: boolean): MyAction => ({ type: 'Start execution', reducer: produce((state: State) => { @@ -178,5 +179,3 @@ const startExecution = StartExecution(CodeToString(state.editor.text), isDebugMode) }) }) -export { setCursorPosition, addSignAfterCursor, moveCursor, Direction, removeAfterCursor, newlineAfterCursor, appendToOutput, startExecution, finishExecution } - diff --git a/frontend/redux/editor/types.ts b/frontend/redux/editor/types.ts index 0a3b54e..89604eb 100644 --- a/frontend/redux/editor/types.ts +++ b/frontend/redux/editor/types.ts @@ -1,41 +1,41 @@ import { Sign } from '../../lib/sign' -interface LetterSize { +export interface LetterSize { edgePx: number marginPx: number } -interface Letter { +export interface Letter { id: string sign: Sign } -interface LetterRow { +export interface LetterRow { id: string letters: Letter[] } -type AlphabetCategory = { name: string, signs: Sign[] } -type CategorizedAlphabet = AlphabetCategory[] -type Text = LetterRow[] -type FileName = string -type SignToGifMap = Map +export type AlphabetCategory = { name: string, signs: Sign[] } +export type CategorizedAlphabet = AlphabetCategory[] +export type Text = LetterRow[] +export type FileName = string +export type SignToGifMap = Map -interface PositionPixels { +export interface PositionPixels { x: number y: number } -interface PositionRowCol { +export interface PositionRowCol { row: number col: number } -enum RunState { +export enum RunState { STARTING, RUNNING, DEBUG_WAITING, DEBUG_RUNNING, NOT_RUNNING } -interface ExecutionState { +export interface ExecutionState { state: RunState output: string worker: Worker | null @@ -43,7 +43,7 @@ interface ExecutionState { lineno: number } -interface EditorState { +export interface EditorState { text: Text, cursorPosition: PositionRowCol letterSize: LetterSize, @@ -51,7 +51,3 @@ interface EditorState { alphabet: CategorizedAlphabet, execution: ExecutionState, } - - -export { EditorState, RunState, Text, ExecutionState, LetterSize, SignToGifMap, PositionRowCol, PositionPixels, Letter, LetterRow, CategorizedAlphabet, AlphabetCategory } - diff --git a/frontend/redux/storage/actions.ts b/frontend/redux/storage/actions.ts index f8da5d5..76614f9 100644 --- a/frontend/redux/storage/actions.ts +++ b/frontend/redux/storage/actions.ts @@ -4,7 +4,7 @@ import { produce } from 'immer' import { CodeToString } from '../../lib/editor' import { MyAction, State } from '../types' -const saveCode = +export const saveCode = (): MyAction => ({ type: 'Save code', reducer: produce((state: State) => { @@ -14,6 +14,3 @@ const saveCode = }) }) }) - -export { saveCode } - diff --git a/frontend/redux/types.ts b/frontend/redux/types.ts index 60424fc..73c0917 100644 --- a/frontend/redux/types.ts +++ b/frontend/redux/types.ts @@ -1,16 +1,13 @@ import { EditorState } from './editor/types' -interface State { +export interface State { readonly editor: EditorState } // "My" prefix is used to distinguish this interface from "Action" interface // found in redux. -interface MyAction { +export interface MyAction { type: string payload?: Payload reducer: (state: State) => State } - -export { State, MyAction } - diff --git a/interpreter/ast/ast-node.ts b/interpreter/ast/ast-node.ts index d111515..2b2a78b 100644 --- a/interpreter/ast/ast-node.ts +++ b/interpreter/ast/ast-node.ts @@ -1,13 +1,10 @@ -type JisonLocator = { +export type JisonLocator = { first_line: number, first_column: number, last_line: number, last_column: number } -abstract class AstNode { +export abstract class AstNode { constructor(readonly locator: JisonLocator) { } } - -export { AstNode, JisonLocator } - diff --git a/interpreter/ast/completion.ts b/interpreter/ast/completion.ts index 5317d5f..765cb02 100644 --- a/interpreter/ast/completion.ts +++ b/interpreter/ast/completion.ts @@ -1,13 +1,13 @@ import { Instance } from '../object-model/instance' -enum CompletionType { +export enum CompletionType { NORMAL, RETURN, BREAK, CONTINUE, } -abstract class Completion { +export abstract class Completion { isNormal(): this is NormalCompletion { return this instanceof NormalCompletion } @@ -30,34 +30,31 @@ abstract class Completion { // typeguards from Copmletion would not work as intended. // Issue: https://github.com/microsoft/TypeScript/issues/33475 -class NormalCompletion extends Completion { +export class NormalCompletion extends Completion { private normalBrand: any constructor() { super() this.normalBrand = '' } } -class BreakCompletion extends Completion { +export class BreakCompletion extends Completion { private breakBrand: any constructor() { super() this.breakBrand = '' } } -class ContinueCompletion extends Completion { +export class ContinueCompletion extends Completion { private continueBrand: any constructor() { super() this.continueBrand = '' } } -class ReturnCompletion extends Completion { +export class ReturnCompletion extends Completion { private returnBrand: any constructor(readonly value: Instance) { super() this.returnBrand = '' } } - -export { Completion, NormalCompletion, BreakCompletion, ContinueCompletion, ReturnCompletion, CompletionType } - diff --git a/interpreter/ast/expr.ts b/interpreter/ast/expr.ts index 4a1c5f9..76f714c 100644 --- a/interpreter/ast/expr.ts +++ b/interpreter/ast/expr.ts @@ -1,18 +1,18 @@ import { AstNode, JisonLocator } from './ast-node' import { Operator } from './operator' -type Expr = RefExpr | ValueExpr +export type Expr = RefExpr | ValueExpr -abstract class ValueExpr extends AstNode { +export abstract class ValueExpr extends AstNode { abstract accept(visitor: VisitorValueExpr): T } /* Expressions that can appear on the left hand side of an assignment. */ -abstract class RefExpr extends AstNode { +export abstract class RefExpr extends AstNode { abstract accept(visitor: VisitorRefExpr): T } -interface VisitorValueExpr { +export interface VisitorValueExpr { visitAssignmentValueExpr(expr: AssignmentValueExpr): T visitNumberValueExpr(expr: NumberValueExpr): T visitStringValueExpr(expr: StringValueExpr): T @@ -24,7 +24,7 @@ interface VisitorValueExpr { visitCallValueExpr(expr: CallValueExpr): T } -class AssignmentValueExpr extends ValueExpr { +export class AssignmentValueExpr extends ValueExpr { constructor( readonly lhs: RefExpr, readonly rhs: Expr, loc: JisonLocator) { @@ -36,7 +36,7 @@ class AssignmentValueExpr extends ValueExpr { } } -class NumberValueExpr extends ValueExpr { +export class NumberValueExpr extends ValueExpr { readonly value: number constructor( readonly rawValue: string, loc: JisonLocator) { @@ -49,12 +49,11 @@ class NumberValueExpr extends ValueExpr { } } -class StringValueExpr extends ValueExpr { +export class StringValueExpr extends ValueExpr { readonly value: string constructor( readonly rawValue: string, loc: JisonLocator) { super(loc) - // TODO: Account for escaping. this.value = rawValue } @@ -63,7 +62,7 @@ class StringValueExpr extends ValueExpr { } } -class ArrayValueExpr extends ValueExpr { +export class ArrayValueExpr extends ValueExpr { constructor( readonly elements: Expr[], loc: JisonLocator) { super(loc) @@ -74,7 +73,7 @@ class ArrayValueExpr extends ValueExpr { } } -class NoneValueExpr extends ValueExpr { +export class NoneValueExpr extends ValueExpr { constructor(loc: JisonLocator) { super(loc) } @@ -84,7 +83,7 @@ class NoneValueExpr extends ValueExpr { } } -class UnaryPlusMinusValueExpr extends ValueExpr { +export class UnaryPlusMinusValueExpr extends ValueExpr { constructor( readonly operator: Operator, readonly right: Expr, loc: JisonLocator) { @@ -96,7 +95,7 @@ class UnaryPlusMinusValueExpr extends ValueExpr { } } -class UnaryNotValueExpr extends ValueExpr { +export class UnaryNotValueExpr extends ValueExpr { constructor(readonly right: Expr, loc: JisonLocator) { super(loc) } @@ -106,7 +105,7 @@ class UnaryNotValueExpr extends ValueExpr { } } -class BinaryValueExpr extends ValueExpr { +export class BinaryValueExpr extends ValueExpr { constructor( readonly operator: Operator, readonly left: Expr, @@ -121,7 +120,7 @@ class BinaryValueExpr extends ValueExpr { } } -class CallValueExpr extends ValueExpr { +export class CallValueExpr extends ValueExpr { constructor( readonly callee: Expr, readonly args: Expr[], loc: JisonLocator) { @@ -133,13 +132,13 @@ class CallValueExpr extends ValueExpr { } } -interface VisitorRefExpr { +export interface VisitorRefExpr { visitVariableRefExpr(expr: VariableRefExpr): T visitSquareAccessorRefExpr(expr: SquareAccessorRefExpr): T visitDotAccessorRefExpr(expr: DotAccessorRefExpr): T } -class VariableRefExpr extends RefExpr { +export class VariableRefExpr extends RefExpr { constructor(readonly name: string, loc: JisonLocator) { super(loc) } @@ -149,7 +148,7 @@ class VariableRefExpr extends RefExpr { } } -class SquareAccessorRefExpr extends RefExpr { +export class SquareAccessorRefExpr extends RefExpr { constructor( readonly object: Expr, readonly property: Expr, loc: JisonLocator) { @@ -161,7 +160,7 @@ class SquareAccessorRefExpr extends RefExpr { } } -class DotAccessorRefExpr extends RefExpr { +export class DotAccessorRefExpr extends RefExpr { constructor( readonly object: Expr, readonly property: string, loc: JisonLocator) { @@ -172,6 +171,3 @@ class DotAccessorRefExpr extends RefExpr { return visitor.visitDotAccessorRefExpr(this) } } - -export { VisitorValueExpr, VisitorRefExpr, Expr, ValueExpr, RefExpr, AssignmentValueExpr, NumberValueExpr, StringValueExpr, ArrayValueExpr, NoneValueExpr, VariableRefExpr, UnaryPlusMinusValueExpr, UnaryNotValueExpr, BinaryValueExpr, CallValueExpr, SquareAccessorRefExpr, DotAccessorRefExpr } - diff --git a/interpreter/ast/operator.ts b/interpreter/ast/operator.ts index 7ce7aff..3dcfc36 100644 --- a/interpreter/ast/operator.ts +++ b/interpreter/ast/operator.ts @@ -1,4 +1,4 @@ -enum Operator { +export enum Operator { PLUS = '+', MINUS = '-', MUL = '*', @@ -14,5 +14,3 @@ enum Operator { GE = '>=', GT = '>', } - -export { Operator } diff --git a/interpreter/ast/stmt.ts b/interpreter/ast/stmt.ts index e01cac5..55dac80 100644 --- a/interpreter/ast/stmt.ts +++ b/interpreter/ast/stmt.ts @@ -2,7 +2,7 @@ import { AstNode, JisonLocator } from './ast-node' import { CompletionType } from './completion' import { Expr } from './expr' -interface VisitorStmt { +export interface VisitorStmt { visitIfStmt(stmt: IfStmt): T visitBlockStmt(stmt: BlockStmt): T visitWhileStmt(stmt: WhileStmt): T @@ -15,11 +15,11 @@ interface VisitorStmt { visitExprStmt(stmt: ExprStmt): T } -abstract class Stmt extends AstNode { +export abstract class Stmt extends AstNode { abstract accept(visitor: VisitorStmt): T } -class IfStmt extends Stmt { +export class IfStmt extends Stmt { readonly consequent: BlockStmt readonly alternate: BlockStmt | null @@ -39,7 +39,7 @@ class IfStmt extends Stmt { } } -class BlockStmt extends Stmt { +export class BlockStmt extends Stmt { constructor(readonly stmts: Stmt[], loc: JisonLocator) { super(loc) } @@ -49,7 +49,7 @@ class BlockStmt extends Stmt { } } -class WhileStmt extends Stmt { +export class WhileStmt extends Stmt { readonly body: BlockStmt constructor( @@ -64,7 +64,7 @@ class WhileStmt extends Stmt { } } -class ForStmt extends Stmt { +export class ForStmt extends Stmt { readonly body: BlockStmt constructor( @@ -83,7 +83,7 @@ class ForStmt extends Stmt { } } -class FunctionDeclStmt extends Stmt { +export class FunctionDeclStmt extends Stmt { constructor( readonly name: string, readonly parameters: string[], @@ -98,7 +98,7 @@ class FunctionDeclStmt extends Stmt { } } -class ClassDefStmt extends Stmt { +export class ClassDefStmt extends Stmt { constructor( readonly name: string, readonly baseName: string | null, @@ -113,7 +113,7 @@ class ClassDefStmt extends Stmt { } } -class CompletionStmt extends Stmt { +export class CompletionStmt extends Stmt { constructor( readonly completionType: CompletionType, readonly right: Expr | null = null, @@ -127,7 +127,7 @@ class CompletionStmt extends Stmt { } } -class ProgramStmt extends Stmt { +export class ProgramStmt extends Stmt { constructor(readonly body: Stmt[], loc: JisonLocator) { super(loc) } @@ -137,13 +137,13 @@ class ProgramStmt extends Stmt { } } -class EmptyStmt extends Stmt { +export class EmptyStmt extends Stmt { accept(visitor: VisitorStmt): T { return visitor.visitEmptyStmt(this) } } -class ExprStmt extends Stmt { +export class ExprStmt extends Stmt { constructor(readonly expr: Expr, loc: JisonLocator) { super(loc) } @@ -159,6 +159,3 @@ function ensureIsBlockStmt(stmt: Stmt): BlockStmt { if (stmt instanceof BlockStmt) return stmt else return new BlockStmt([stmt], stmt.locator) } - -export { VisitorStmt, Stmt, IfStmt, BlockStmt, WhileStmt, ForStmt, FunctionDeclStmt, ClassDefStmt, CompletionStmt, ProgramStmt, EmptyStmt, ExprStmt } - diff --git a/interpreter/code-executer.ts b/interpreter/code-executer.ts index 65a73d8..85bd93f 100644 --- a/interpreter/code-executer.ts +++ b/interpreter/code-executer.ts @@ -3,11 +3,8 @@ import { Completion } from './ast/completion' import { Stmt } from './ast/stmt' import { Environment } from './environment' -interface CodeExecuter { +export interface CodeExecuter { executeInEnvironment(stmts: Stmt[], excEnv: Environment): Promise callStack: string[] locator: JisonLocator } - -export { CodeExecuter } - diff --git a/interpreter/environment.ts b/interpreter/environment.ts index 9725873..91051f4 100644 --- a/interpreter/environment.ts +++ b/interpreter/environment.ts @@ -1,6 +1,6 @@ import { Instance, ValueRef } from './object-model/instance' -class Environment { +export class Environment { private readonly values: Map constructor(private readonly enclosing: Environment | null) { @@ -53,6 +53,3 @@ class Environment { } } } - -export { Environment } - diff --git a/interpreter/interpreter.ts b/interpreter/interpreter.ts index a69cad7..e843a6c 100644 --- a/interpreter/interpreter.ts +++ b/interpreter/interpreter.ts @@ -21,7 +21,7 @@ export interface InterpreterSetup { onPrint: PrintFunction } -class Interpreter +export class Interpreter implements VisitorRefExpr>, VisitorValueExpr>, @@ -317,6 +317,3 @@ class Interpreter return (boolRes as BoolInstance).value } } - -export { Interpreter } - diff --git a/interpreter/object-model/class.ts b/interpreter/object-model/class.ts index a9f105c..90beb89 100644 --- a/interpreter/object-model/class.ts +++ b/interpreter/object-model/class.ts @@ -5,7 +5,7 @@ import { BoolInstance, Instance, ObjectInstance, StringInstance, TWrappedFunctio import { MagicMethod } from './magic-method' import { NumberInstance } from './std/number-instance' -function CheckArityEq(args: Instance[], n: number) { +export function CheckArityEq(args: Instance[], n: number) { // TODO: Add fcn name. if (args.length !== n) { throw Error( @@ -13,7 +13,7 @@ function CheckArityEq(args: Instance[], n: number) { } } -function CheckArityGe(args: Instance[], n: number) { +export function CheckArityGe(args: Instance[], n: number) { // TODO: Add fcn name. if (args.length < n) { throw Error( @@ -22,7 +22,7 @@ function CheckArityGe(args: Instance[], n: number) { } } -abstract class Class extends Instance { +export abstract class Class extends Instance { // Nulls for initial bootstrapping. constructor( klass: MetaClass | null, @@ -77,7 +77,7 @@ abstract class Class extends Instance { } } -class MetaClass extends Class { +export class MetaClass extends Class { // MetaClass's __call__ is responsible for creation of new objects. static async __call__( interpreter: CodeExecuter, @@ -122,7 +122,7 @@ class MetaClass extends Class { } } -class ObjectClass extends Class { +export class ObjectClass extends Class { static async __init__( _interpreter: CodeExecuter, args: Instance[] @@ -198,7 +198,7 @@ class ObjectClass extends Class { } } -class WrappedFunctionClass extends Class { +export class WrappedFunctionClass extends Class { static async __str__( _interpreter: CodeExecuter, args: Instance[], @@ -248,7 +248,7 @@ class WrappedFunctionClass extends Class { } } -class UserFunctionClass extends Class { +export class UserFunctionClass extends Class { static async __str__( _interpreter: CodeExecuter, args: Instance[], @@ -295,7 +295,7 @@ class UserFunctionClass extends Class { } } -class NoneClass extends Class { +export class NoneClass extends Class { static async __str__( _interpreter: CodeExecuter, args: Instance[], @@ -338,7 +338,7 @@ class NoneClass extends Class { } } -class BoolClass extends Class { +export class BoolClass extends Class { static async __str__( _interpreter: CodeExecuter, args: Instance[] @@ -389,7 +389,7 @@ class BoolClass extends Class { } } -class StringClass extends Class { +export class StringClass extends Class { static async __init__( _interpreter: CodeExecuter, args: Instance[] @@ -506,7 +506,7 @@ class StringClass extends Class { } } -class UserClass extends Class { +export class UserClass extends Class { constructor( name: string, base: Class, methods: FunctionDeclStmt[], env: Environment) { @@ -535,6 +535,3 @@ class UserClass extends Class { return this.base.createBlankUserInstance(klass) } } - -export { Class, MetaClass, CheckArityEq, UserClass, WrappedFunctionClass, UserFunctionClass, NoneClass, ObjectClass, StringClass, BoolClass } - diff --git a/interpreter/object-model/instance.ts b/interpreter/object-model/instance.ts index 79903bb..dbd60fd 100644 --- a/interpreter/object-model/instance.ts +++ b/interpreter/object-model/instance.ts @@ -4,12 +4,17 @@ import { Environment } from '../environment' import { BoolClass, CheckArityEq, Class, NoneClass, WrappedFunctionClass } from './class' import { MagicMethod } from './magic-method' -interface ValueRef { +export interface ValueRef { set(value: Instance): void get(): Promise } -class Instance { +export type TWrappedFunction = ( + interpreter: CodeExecuter, + args: Instance[], +) => Promise + +export class Instance { public fields: Map = new Map() private static nextId: number = 0 public id: number @@ -99,13 +104,13 @@ class Instance { } } -class ObjectInstance extends Instance { +export class ObjectInstance extends Instance { constructor(klass: Class) { super(klass) } } -class NoneInstance extends ObjectInstance { +export class NoneInstance extends ObjectInstance { private constructor(noneClass: NoneClass) { super(noneClass) } @@ -144,7 +149,7 @@ abstract class FunctionInstance extends ObjectInstance { } } -class UserFunctionInstance extends FunctionInstance { +export class UserFunctionInstance extends FunctionInstance { constructor( klass: Class, private readonly functionDef: FunctionDeclStmt, @@ -184,12 +189,7 @@ class UserFunctionInstance extends FunctionInstance { } } -type TWrappedFunction = ( - interpreter: CodeExecuter, - args: Instance[], -) => Promise - -class WrappedFunctionInstance extends FunctionInstance { +export class WrappedFunctionInstance extends FunctionInstance { constructor( klass: Class, public wrappedFunction: TWrappedFunction, @@ -211,7 +211,7 @@ class WrappedFunctionInstance extends FunctionInstance { } } -class BoolInstance extends ObjectInstance { +export class BoolInstance extends ObjectInstance { constructor(boolClass: BoolClass, readonly value: boolean) { super(boolClass) } @@ -231,11 +231,8 @@ class BoolInstance extends ObjectInstance { } } -class StringInstance extends ObjectInstance { +export class StringInstance extends ObjectInstance { constructor(klass: Class, public value: string) { super(klass) } } - -export { Instance, ValueRef, BoolInstance, WrappedFunctionInstance, FunctionInstance, NoneInstance, UserFunctionInstance, ObjectInstance, TWrappedFunction, StringInstance } - diff --git a/interpreter/object-model/magic-method.ts b/interpreter/object-model/magic-method.ts index f3fdc90..7553825 100644 --- a/interpreter/object-model/magic-method.ts +++ b/interpreter/object-model/magic-method.ts @@ -1,4 +1,4 @@ -enum MagicMethod { +export enum MagicMethod { __str__ = '__str__', __add__ = '__add__', @@ -23,5 +23,3 @@ enum MagicMethod { __call__ = '__call__', __init__ = '__INIT__', } -export { MagicMethod } - diff --git a/interpreter/object-model/std/array-class.ts b/interpreter/object-model/std/array-class.ts index 22d5565..5a5b674 100644 --- a/interpreter/object-model/std/array-class.ts +++ b/interpreter/object-model/std/array-class.ts @@ -7,7 +7,7 @@ import { Stringify } from './functions' import { NumberClass } from './number-class' import { NumberInstance } from './number-instance' -class ArrayClass extends Class { +export class ArrayClass extends Class { static async __init__( _interpreter: CodeExecuter, args: Instance[], @@ -147,6 +147,3 @@ class ArrayClass extends Class { return new ArrayInstance(klass, /* value = */[]) } } - -export { ArrayClass } - diff --git a/interpreter/object-model/std/array-instance.ts b/interpreter/object-model/std/array-instance.ts index 34aac30..616159b 100644 --- a/interpreter/object-model/std/array-instance.ts +++ b/interpreter/object-model/std/array-instance.ts @@ -1,11 +1,8 @@ import { Class } from '../class' import { Instance, ObjectInstance } from '../instance' -class ArrayInstance extends ObjectInstance { +export class ArrayInstance extends ObjectInstance { constructor(klass: Class, public values: Instance[]) { super(klass) } } - -export { ArrayInstance } - diff --git a/interpreter/object-model/std/functions.ts b/interpreter/object-model/std/functions.ts index e9d3c35..06fde39 100644 --- a/interpreter/object-model/std/functions.ts +++ b/interpreter/object-model/std/functions.ts @@ -2,7 +2,7 @@ import { CodeExecuter } from '../../code-executer' import { Instance, NoneInstance, StringInstance, TWrappedFunction } from '../instance' import { MagicMethod } from '../magic-method' -async function Stringify(interpreter: CodeExecuter, args: Instance[]) +export async function Stringify(interpreter: CodeExecuter, args: Instance[]) : Promise { return Promise.all( args.map(async (arg) => ((await arg.callMagicMethod( @@ -11,9 +11,9 @@ async function Stringify(interpreter: CodeExecuter, args: Instance[]) ))) } -type PrintFunction = (str: string) => void +export type PrintFunction = (str: string) => void -function GiflangPrint(print: PrintFunction) +export function GiflangPrint(print: PrintFunction) : TWrappedFunction { return async (interpreter: CodeExecuter, args: Instance[]) : Promise => { @@ -21,6 +21,3 @@ function GiflangPrint(print: PrintFunction) return NoneInstance.getInstance() } } - -export { GiflangPrint, PrintFunction, Stringify } - diff --git a/interpreter/object-model/std/number-class.ts b/interpreter/object-model/std/number-class.ts index 505c7f0..1c02b5b 100644 --- a/interpreter/object-model/std/number-class.ts +++ b/interpreter/object-model/std/number-class.ts @@ -4,7 +4,7 @@ import { BoolInstance, Instance, StringInstance } from '../instance' import { MagicMethod } from '../magic-method' import { NumberInstance } from './number-instance' -class NumberClass extends Class { +export class NumberClass extends Class { static async __str__( _interpreter: CodeExecuter, args: Instance[], @@ -107,6 +107,3 @@ class NumberClass extends Class { return new NumberInstance(klass, /* value = */ 0) } } - -export { NumberClass } - diff --git a/interpreter/object-model/std/number-instance.ts b/interpreter/object-model/std/number-instance.ts index 48fb194..696d9c6 100644 --- a/interpreter/object-model/std/number-instance.ts +++ b/interpreter/object-model/std/number-instance.ts @@ -1,11 +1,8 @@ import { Class } from '../class' import { ObjectInstance } from '../instance' -class NumberInstance extends ObjectInstance { +export class NumberInstance extends ObjectInstance { constructor(klass: Class, readonly value: number) { super(klass) } } - -export { NumberInstance } - diff --git a/interpreter/operations.ts b/interpreter/operations.ts deleted file mode 100644 index 3ddfd15..0000000 --- a/interpreter/operations.ts +++ /dev/null @@ -1,68 +0,0 @@ -// import { Operator } from './ast/operator' -// s -// function isLessThan(left: Value, right: Value): boolean { -// if ( -// (left.isNumber() && right.isNumber()) || -// (left.isString() && right.isString()) -// ) { -// return left.value < right.value -// } - -// throw new Error('Relational operator used for incompatible types.') -// } - -// // TODO: Objects. -// function isEqual(left: Value, right: Value): boolean { -// if ( -// (left.isNumber() && right.isNumber()) || -// (left.isString() && right.isString()) || -// (left.isBool() && right.isBool()) -// ) { -// return left.value === right.value -// } else if (left.isNone() && right.isNone()) { -// return true -// } else { -// return false -// } -// } - -// function isTruthy(right: Value): boolean { -// if (right.isBool()) { -// return right.value -// } else if (right.isNumber()) { -// return right.value !== 0 -// } else if (right.isString()) { -// return right.value !== '' -// } else if (right.isNone()) { -// return false -// } else { -// // TODO: Array & objects. -// throw new Error('internal') -// } -// } - -// function numbersOnlyOperation( -// left: Value, -// right: Value, -// operator: Operator, -// ): number { -// if (!left.isNumber() || !right.isNumber()) { -// // TODO: Add operator to the error. -// throw new Error('Operands must be two numbers.') -// } -// switch (operator) { -// case Operator.MINUS: -// return left.value - right.value -// case Operator.MUL: -// return left.value * right.value -// case Operator.DIV: -// return left.value / right.value -// case Operator.MOD: -// return left.value % right.value -// default: -// throw new Error('Internal.') -// } -// } - -// export { numbersOnlyOperation, isEqual, isTruthy, isLessThan } - diff --git a/interpreter/parser.ts b/interpreter/parser.ts index 30cf5cb..d568ea6 100644 --- a/interpreter/parser.ts +++ b/interpreter/parser.ts @@ -1,10 +1,10 @@ -import { parser } from './ast/giflang.jison' +import { CompletionType } from './ast/completion' import * as Expr from './ast/expr' -import * as Stmt from './ast/stmt' +import { parser } from './ast/giflang.jison' import { Operator } from './ast/operator' -import { CompletionType } from './ast/completion' +import * as Stmt from './ast/stmt' -function ParseGiflang(input: string): Stmt.ProgramStmt { +export function ParseGiflang(input: string): Stmt.ProgramStmt { parser.yy = { Expr, Stmt, @@ -13,5 +13,3 @@ function ParseGiflang(input: string): Stmt.ProgramStmt { } return parser.parse(input) } - -export { ParseGiflang }