Skip to content

Commit

Permalink
Improve error handling, support Assoc, List and Regex, set default va…
Browse files Browse the repository at this point in the history
…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
prantlf committed Jan 11, 2021
1 parent 70b8d5b commit 2127a71
Show file tree
Hide file tree
Showing 21 changed files with 606 additions and 168 deletions.
28 changes: 26 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
# Changelog

## Unreleased
## 0.2.5

### Bug Fixes
### Parser

* Allow dereferencing `this` without the `this` keyword (chain the dot operators).
* Correct typings for `SliceExpression`.
* Nest the binary expressions according to the binary operator precedence.

### Walker 0.1.0

If the recursive walker fails, include the latest visited node in the error.

### Interpreter 0.1.0

* 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.

```js
// old syntax
interpret(ast, { count: 1 })
// new syntax
interpret(ast, { globals: { count: 1 } })
```

## 0.2.4

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oscript-parser",
"version": "0.2.4",
"version": "0.2.5",
"description": "A parser for the OScript language written in JavaScript.",
"author": "Ferdinand Prantl <prantlf@gmail.com> (http://prantlf.tk/)",
"keywords": [
Expand Down
19 changes: 19 additions & 0 deletions pkg/interpreter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## 0.1.0

* 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.

```js
// old syntax
interpret(ast, { count: 1 })
// new syntax
interpret(ast, { globals: { count: 1 } })
```

## 0.0.1

Initial release.
Expand Down
2 changes: 1 addition & 1 deletion pkg/interpreter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ yarn add oscript-interpreter

## Interface

**interpret**`(ast, globals)` interprets the `ast` and performs operations described by the nodes of the [AST]. The optional `globals` object can contain variables and functions which will be inserted to the global scope. Keys of the object are supposed to be identifiers and values are supposed to be variable values or functions.
**interpret**`(ast, options)` interprets the `ast` and performs operations described by the nodes of the [AST]. The option `globals` can be an object with variables and functions which will be inserted to the global scope. Keys of the object are supposed to be identifiers and values are supposed to be variable values or functions. The option `warnings` can be a boolean flag to enable treating problems, which the interpreter can recover from as errors and fail the ecxecution.

```js
import { parseText } from 'oscript-parser'
Expand Down
24 changes: 22 additions & 2 deletions pkg/interpreter/dist/index.d.ts
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 {}
6 changes: 3 additions & 3 deletions pkg/interpreter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oscript-interpreter",
"version": "0.0.1",
"version": "0.1.0",
"description": "A interpreter of the OScript language using the abstract syntax tree (AST).",
"author": "Ferdinand Prantl <prantlf@gmail.com> (http://prantlf.tk/)",
"keywords": [
Expand Down Expand Up @@ -30,7 +30,7 @@
"browser": "dist/index.umd.min.js",
"types": "dist/index.d.ts",
"dependencies": {
"oscript-parser": "0.2.4",
"oscript-ast-walker": "0.0.3"
"oscript-parser": "0.2.5",
"oscript-ast-walker": "0.1.0"
}
}
17 changes: 17 additions & 0 deletions pkg/interpreter/src/defaults.js
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()
}
10 changes: 5 additions & 5 deletions pkg/interpreter/src/index.js
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)
}
Loading

0 comments on commit 2127a71

Please sign in to comment.