@@ -5,15 +5,15 @@ import { STEP_LIMIT } from './constants'
55import { RuntimeError } from './errors'
66import { evaluate } from './interpreter'
77import { 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'
99import { handleSequence } from './utils'
1010
1111export * from './components'
1212export * from './errors'
1313export * from './types'
1414export { 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-
8259export const createContext = ( code : string , ioCallbacks ?: IOCallbacks ) : Context => ( {
8360 errors : [ ] ,
8461
@@ -94,9 +71,21 @@ export const createContext = (code: string, ioCallbacks?: IOCallbacks): Context
9471const 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+ }
0 commit comments