Skip to content

Commit bd07203

Browse files
committed
integrate new CSEC evaluator
1 parent 2fca094 commit bd07203

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

src/ec-evaluator/index.ts

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { STEP_LIMIT } from './constants'
55
import { RuntimeError } from './errors'
66
import { evaluate } from './interpreter'
77
import { LFSR, libraryClasses } from './lib'
8-
import { Context, Error, Finished, Interfaces, IOCallbacks, Result } from './types'
8+
import { Class, Context, Error, Finished, Interfaces, IOCallbacks, Result } from './types'
99
import { handleSequence } from './utils'
1010

1111
export * from './components'
1212
export * from './errors'
1313
export * from './types'
1414
export { isInstr, isNode } from './utils'
1515

16-
export const runECEvaluatorInjected = (
16+
export const runECEvaluator = (
1717
code: string,
1818
targetStep: number = STEP_LIMIT,
1919
ioCallbacks: IOCallbacks
@@ -56,29 +56,6 @@ export const runECEvaluatorInjected = (
5656
}
5757
}
5858

59-
export const runECEvaluator = (code: string, targetStep: number = STEP_LIMIT): Promise<Result> => {
60-
const context = createContext(code)
61-
try {
62-
// parse() may throw SyntaxError.
63-
const compilationUnit = parse(code)
64-
65-
context.control.push(compilationUnit)
66-
// evaluate() may throw RuntimeError
67-
const value = evaluate(context, targetStep)
68-
69-
return new Promise((resolve, _) => {
70-
resolve({ status: 'finished', context, value } as Finished)
71-
})
72-
} catch (e) {
73-
// Possible interpreting language error thrown, so conversion to RuntimeError may be required.
74-
const error = e.type ? e : new RuntimeError(e.message)
75-
context.errors.push(error)
76-
return new Promise((resolve, _) => {
77-
resolve({ status: 'error', context } as Error)
78-
})
79-
}
80-
}
81-
8259
export const createContext = (code: string, ioCallbacks?: IOCallbacks): Context => ({
8360
errors: [],
8461

@@ -94,9 +71,21 @@ export const createContext = (code: string, ioCallbacks?: IOCallbacks): Context
9471
const initialiseInterfaces = (code: string, ioCallbacks?: IOCallbacks): Interfaces => {
9572
return {
9673
stdout: ioCallbacks?.stdout ?? console.log,
97-
stderr: ioCallbacks?.stderr ?? console.log,
74+
stderr: ioCallbacks?.stderr ?? console.error,
9875
statics: {
9976
lfsr: new LFSR(code)
10077
}
10178
}
10279
}
80+
81+
export const makeObjectClass = (): Class => {
82+
const context = createContext('')
83+
const objectClassDefinition = 'class Object {}'
84+
const libraryCompilationUnit = parse(objectClassDefinition)
85+
context.control.push(
86+
...handleSequence(libraryCompilationUnit.topLevelClassOrInterfaceDeclarations)
87+
)
88+
evaluate(context, STEP_LIMIT)
89+
90+
return context.environment.getClass('Object')
91+
}

src/ec-evaluator/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export interface Class {
202202
instanceFields: FieldDeclaration[]
203203
instanceMethods: (MethodDeclaration | NativeDeclaration)[]
204204
staticFields: FieldDeclaration[]
205-
staticMethods: MethodDeclaration[]
205+
staticMethods: (MethodDeclaration | NativeDeclaration)[]
206206
superclass?: Class
207207
}
208208

0 commit comments

Comments
 (0)