Skip to content

Commit

Permalink
test lambda ext
Browse files Browse the repository at this point in the history
  • Loading branch information
jwklong authored Jan 15, 2025
1 parent 97dcfd3 commit 78bd635
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/extension-support/extension-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ const defaultBuiltinExtensions = {
jwTargets: () => require("../extensions/jwTargets"),
// cool new physics extension
jwPsychic: () => require("../extensions/jwPsychic"),
// test ext for lambda functions or something
jwLambda: () => require("../extensions/jwLambda"),

// jw: They'll think its made by jwklong >:)
// (but it's not (yet (maybe (probably not (but its made by ianyourgod)))))
Expand Down
136 changes: 136 additions & 0 deletions src/extensions/jwLambda/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const BlockType = require('../../extension-support/block-type')
const BlockShape = require('../../extension-support/block-shape')
const ArgumentType = require('../../extension-support/argument-type')
const Cast = require('../../util/cast')

/**
* @param {number} x
* @returns {string}
*/
function formatNumber(x) {
if (x >= 1e6) {
return x.toExponential(4)
} else {
x = Math.floor(x * 1000) / 1000
return x.toFixed(Math.min(3, (String(x).split('.')[1] || '').length))
}
}

function span(text) {
let el = document.createElement('span')
el.innerHTML = text
el.style.display = 'hidden'
el.style.whiteSpace = 'nowrap'
el.style.width = '100%'
el.style.textAlign = 'center'
return el
}

class LambdaType {
customId = "jwLambda"

constructor(func) {
this.func = typeof func == 'function' ? func : () => func
}

static toLambda(x) {
if (x instanceof LambdaType) return x
return new LambdaType(x)
}

jwArrayHandler() {
return 'Lambda'
}

toString() {
return `Lambda`
}
toMonitorContent = () => span(this.toString())
toReporterContent = () => span(this.toString())
}

const Lambda = {
Type: LambdaType,
Block: {
blockType: BlockType.REPORTER,
blockShape: BlockShape.SQUARE,
forceOutputType: "Lambda",
disableMonitor: true
},
Argument: {
shape: BlockShape.SQUARE,
check: ["Lambda"]
}
}

class Extension {
constructor() {
vm.jwLambda = Lambda
vm.runtime.registerSerializer(
"jwLambda",
v => null,
v => new Lambda.Type("")
);
}

getInfo() {
return {
id: "jwLambda",
name: "Lambda",
color1: "#f04a87",
blocks: [
{
opcode: 'arg',
text: 'argument',
blockType: BlockType.REPORTER,
hideFromPalette: true,
allowDropAnywhere: true,
canDragDuplicate: true
},
{
opcode: 'newLambda',
text: 'new lambda [ARG]',
branchCount: 1,
arguments: {
ARG: {
fillIn: 'arg'
}
},
...Lambda.Block
},
{
opcode: 'execute',
text: 'execute [LAMBDA] with [ARG]',
arguments: {
LAMBDA: Lambda.Argument,
ARG: {
type: ArgumentType.String,
defaultValue: "foo",
exemptFromNormalization: true
}
}
}
]
};
}

arg({}, util) {
let lambda = util.thread.stackFrames[0].jwLambda
return lambda ?? ""
}

newLambda({}, util) {
return new Lambda.Type((arg) => {
util.thread.stackFrames[0].jwLambda = arg;
util.startBranch(1, false)
})
}

execute({LAMBDA, ARG}, util) {
LAMBDA = Lambda.Type.toLambda(LAMBDA)

LAMBDA.func(ARG)
}
}

module.exports = Extension

0 comments on commit 78bd635

Please sign in to comment.