-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve error handling, support Assoc, List and Regex, set default va…
…lues in declarations * Ignore the imaginary operator `^^`, recognise the existing one `||`. * Fix built-in object method lookup. * Set default values to declared variables. * Introduce built-in objects `Assoc`, `List` and `Regex`. * Add runtime type checking to the built-in functions and object methods. * Include the last interpreted node in the runtime error report. * Inline the preparation of AST from an extra preprocessing step to the interpretation phase. **BREAKING CHANGE**: The second parameters of `interpret` is an object with options. The parameter `globals` has been moved to a property in the `options` object.
- Loading branch information
Showing
21 changed files
with
606 additions
and
168 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import { Node } from 'oscript-parser' | ||
|
||
// ============================================================ | ||
// Public API | ||
|
||
export function interpret (ast: Node, options?: Options): ValueType | ||
|
||
// ---------- Options | ||
|
||
export interface Options { | ||
globals?: Globals | ||
warnings?: boolean | ||
} | ||
|
||
export type Globals = { [key: string]: ValueType } | ||
|
||
export type PrimitiveType = undefined | boolean | number | string | ||
|
||
export type ValueType = PrimitiveType | ValueType[] | { [key: string]: ValueType } | ||
|
||
export type Globals = { [key: string]: ValueType } | ||
// ============================================================ | ||
// Error Handling | ||
|
||
export interface InterpreterError extends Error { | ||
node: Node | ||
} | ||
|
||
export function interpret (ast: Node, globals: Globals): void | ||
export interface NotImplementedError extends InterpreterError {} | ||
export interface RuntimeError extends InterpreterError {} |
This file contains 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 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,17 @@ | ||
import Regex from './library/regex' | ||
|
||
const defaults = { | ||
assoc () { return {} }, | ||
boolean () { return false }, | ||
integer () { return 0 }, | ||
list () { return [] }, | ||
long () { return 0 }, | ||
real () { return 0 }, | ||
regex () { return new Regex() }, | ||
string () { return '' } | ||
} | ||
|
||
export default function getDefaultValue (type) { | ||
const getter = defaults[type] | ||
return getter && getter() | ||
} |
This file contains 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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { recursive as recursiveWalk } from 'oscript-ast-walker' | ||
import interpreter from './interpreter' | ||
import optimize from './optimizer' | ||
import { visitors, setOptions } from './interpreter' | ||
import * as library from './library/index' | ||
import Scope from './scope' | ||
|
||
export function interpret (ast, globals = {}) { | ||
const scope = new Scope(null, Object.assign({}, library, globals)) | ||
recursiveWalk(optimize(ast), null, interpreter, scope) | ||
export function interpret (ast, options = {}) { | ||
setOptions(options) | ||
const scope = new Scope(null, Object.assign({}, library, options.globals)) | ||
return recursiveWalk(ast, null, visitors, scope) | ||
} |
Oops, something went wrong.