Skip to content

Commit

Permalink
♻️ refactor Blockly hacks using new framework
Browse files Browse the repository at this point in the history
  • Loading branch information
FurryR committed Jan 23, 2024
1 parent 2e6d2bf commit 21a79e7
Show file tree
Hide file tree
Showing 22 changed files with 1,547 additions and 1,333 deletions.
37 changes: 9 additions & 28 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ export class LppReturn {
* Construct a new LppReturn instance.
* @param value Result.
*/
constructor(
/**
* Result.
*/
public value: LppValue
) {
constructor(public value: LppValue) {
this.value = value
}
}
Expand All @@ -35,16 +30,14 @@ export class LppException {
* Construct a new LppException instance.
* @param value Result.
*/
constructor(
/**
* Result.
*/
public value: LppValue
) {
constructor(public value: LppValue) {
this.stack = []
}
}
export namespace LppTraceback {
/**
* Abstract base class of traceback.
*/
export abstract class Base {
abstract toString(): string
}
Expand All @@ -59,11 +52,8 @@ export namespace LppTraceback {
* @param args Arguments.
*/
constructor(
/** Called function. */
public fn: LppFunction,
/** Self. */
public self: LppValue,
/** Arguments. */
public args: LppValue[]
) {
super()
Expand All @@ -73,6 +63,9 @@ export namespace LppTraceback {
}
}
}
/**
* Closure.
*/
export class LppClosure extends LppValue {
value: Map<string, LppValue> = new Map()
/**
Expand Down Expand Up @@ -127,7 +120,7 @@ export class LppClosure extends LppValue {
}
}
/**
* Lpp Scope.
* Context.
*/
export class LppContext {
/**
Expand Down Expand Up @@ -182,17 +175,8 @@ export class LppContext {
* @param exceptionCallback Callback if function throws.
*/
constructor(
/**
* Parent closure.
*/
public parent: LppContext | undefined,
/**
* Callback if function returns.
*/
private _returnCallback: (value: LppReturn) => void,
/**
* Callback if function throws.
*/
private _exceptionCallback: (value: LppException) => void
) {
this.closure = new LppClosure()
Expand All @@ -218,9 +202,6 @@ export class LppFunctionContext extends LppContext {
*/
constructor(
parent: LppContext | undefined,
/**
* @type Self object.
*/
public self: LppValue,
returnCallback: (value: LppReturn) => void,
exceptionCallback: (value: LppException) => void
Expand Down
30 changes: 30 additions & 0 deletions src/core/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ import {
*/
export const global = new Map<string, LppValue>()
export namespace Global {
/**
* lpp builtin `Boolean` -- constructor of boolean types.
*/
export const Boolean = LppFunction.native((_, args) => {
if (args.length < 1) return new LppReturn(new LppConstant(false))
return new LppReturn(new LppConstant(asBoolean(args[0])))
}, new LppObject(new Map()))
/**
* lpp builtin `Number` -- constructor of number types.
*/
export const Number = LppFunction.native((_, args) => {
/**
* Convert args to number.
Expand Down Expand Up @@ -51,6 +57,9 @@ export namespace Global {
}
return new LppReturn(convertToNumber(args))
}, new LppObject(new Map()))
/**
* lpp builtin `String` -- constructor of string types.
*/
export const String = LppFunction.native(
(_, args) => {
/**
Expand Down Expand Up @@ -85,6 +94,9 @@ export namespace Global {
])
)
)
/**
* lpp builtin `Array` -- constructor of array types.
*/
export const Array = LppFunction.native(
(_, args) => {
/**
Expand Down Expand Up @@ -121,6 +133,9 @@ export namespace Global {
])
)
)
/**
* lpp builtin `Object` -- constructor of object types.
*/
export const Object = LppFunction.native((_, args) => {
/**
* Convert args to object.
Expand All @@ -133,6 +148,9 @@ export namespace Global {
}
return new LppReturn(convertToObject(args))
}, new LppObject(new Map()))
/**
* lpp builtin `Function` -- constructor of function types.
*/
export const Function = LppFunction.native(
(_, args) => {
if (args.length < 1)
Expand Down Expand Up @@ -197,6 +215,9 @@ export namespace Global {
])
)
)
/**
* lpp builtin `Promise` -- represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
*/
export const Promise = LppFunction.native(
(self, args) => {
if (
Expand Down Expand Up @@ -276,6 +297,9 @@ export namespace Global {
])
)
)
/**
* lpp builtin `Error` -- `Error` objects are thrown when runtime errors occur.
*/
export const Error = LppFunction.native((self, args) => {
if (self.instanceof(Error)) {
self.set('value', args[0] ?? new LppConstant(null))
Expand All @@ -291,6 +315,9 @@ export namespace Global {
return new LppException(res.value)
}
}, new LppObject(new Map()))
/**
* Lpp builtin `IllegalInvocationError` -- represents an error when trying to called a function with illegal arguments / context.
*/
export const IllegalInvocationError = LppFunction.native(
(self, args) => {
if (self.instanceof(IllegalInvocationError)) {
Expand All @@ -313,6 +340,9 @@ export namespace Global {
},
new LppObject(new Map([['prototype', ensureValue(Error.get('prototype'))]]))
)
/**
* Lpp builtin `SyntaxError` -- represents an error when trying to interpret syntactically invalid code.
*/
export const SyntaxError = LppFunction.native(
(self, args) => {
if (self.instanceof(SyntaxError)) {
Expand Down
3 changes: 3 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Lpp core (standard) implementation.
*/
export { Global, global } from './global'
export {
LppClosure,
Expand Down
34 changes: 5 additions & 29 deletions src/core/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ export class LppError extends Error {
* Construct a new Lpp error.
* @param id Error ID.
*/
constructor(
/**
* Error ID.
*/
public id: string
) {
constructor(public id: string) {
super(`lpp: Error ${id}`)
}
}
Expand Down Expand Up @@ -189,13 +184,7 @@ export class LppReference implements LppValue {
*/
constructor(
parent: LppValue,
/**
* Key name.
*/
public name: string,
/**
* Current object.
*/
public value: LppValue
) {
this.parent = new WeakRef(parent)
Expand Down Expand Up @@ -512,15 +501,10 @@ export class LppConstant<T extends JSConstant = JSConstant> extends LppValue {
throw new Error('lpp: unknown operand')
}
/**
* Constructs a value.
* @param value The value.
* Construct a value.
* @param _value The stored value.
*/
constructor(
/**
* The stored value.
*/
private _value: T
) {
constructor(private _value: T) {
super()
}
}
Expand Down Expand Up @@ -896,12 +880,7 @@ export class LppArray extends LppValue {
* Construct an array object.
* @param value Array of values.
*/
constructor(
/**
* Array of values.
*/
public value: (LppValue | undefined)[] = []
) {
constructor(public value: (LppValue | undefined)[] = []) {
super()
}
}
Expand Down Expand Up @@ -1084,9 +1063,6 @@ export class LppFunction extends LppObject {
* @param prototype Function prototype.
*/
constructor(
/**
* Function to execute.
*/
private execute: (
self: LppValue,
args: LppValue[]
Expand Down
Loading

0 comments on commit 21a79e7

Please sign in to comment.