From 48c8d067ceb76458ec01579ac9cc5759287fac3e Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Sun, 3 Jul 2011 20:54:26 -0400 Subject: [PATCH] hacking in structs --- js-assembler/get-runtime.rkt | 5 + .../runtime-src/baselib_inspectors.js | 22 +++ js-assembler/runtime-src/baselib_structs.js | 14 +- js-assembler/runtime-src/runtime.js | 137 ++++++++++++++++-- 4 files changed, 157 insertions(+), 21 deletions(-) create mode 100644 js-assembler/runtime-src/baselib_inspectors.js diff --git a/js-assembler/get-runtime.rkt b/js-assembler/get-runtime.rkt index 6811fb6..158c47a 100644 --- a/js-assembler/get-runtime.rkt +++ b/js-assembler/get-runtime.rkt @@ -40,6 +40,8 @@ (define-runtime-path baselib_symbol.js "runtime-src/baselib_symbol.js") (define-runtime-path baselib_structs.js "runtime-src/baselib_structs.js") (define-runtime-path baselib_arity.js "runtime-src/baselib_arity.js") +(define-runtime-path baselib_inspectors.js "runtime-src/baselib_inspectors.js") +(define-runtime-path baselib_exceptions.js "runtime-src/baselib_exceptions.js") (define-runtime-path jshashtable.js "runtime-src/jshashtable-2.1_src.js") @@ -73,6 +75,9 @@ baselib_symbol.js baselib_structs.js baselib_arity.js + baselib_inspectors.js + baselib_exceptions.js + link.js diff --git a/js-assembler/runtime-src/baselib_inspectors.js b/js-assembler/runtime-src/baselib_inspectors.js new file mode 100644 index 0000000..280360b --- /dev/null +++ b/js-assembler/runtime-src/baselib_inspectors.js @@ -0,0 +1,22 @@ +// Structure types + +(function(baselib) { + var exports = {}; + baselib.inspectors = exports; + + + var Inspector = function() { + }; + var DEFAULT_INSPECTOR = new Inspector(); + + Inspector.prototype.toString = function() { + return "#"; + }; + + + + exports.Inspector = Inspector; + exports.DEFAULT_INSPECTOR = DEFAULT_INSPECTOR; + + +})(this['plt'].baselib); \ No newline at end of file diff --git a/js-assembler/runtime-src/baselib_structs.js b/js-assembler/runtime-src/baselib_structs.js index ca9342c..6c43add 100644 --- a/js-assembler/runtime-src/baselib_structs.js +++ b/js-assembler/runtime-src/baselib_structs.js @@ -163,16 +163,16 @@ return plt.helpers.toWrittenString(this, cache); }; - Struct.prototype.toDomNode = function(cache) { - cache.put(this, true); + Struct.prototype.toDomNode = function(params) { + params.put(this, true); var node = document.createElement("div"); - node.appendChild(document.createTextNode("(")); - node.appendChild(document.createTextNode(this._constructorName)); + $(node).append(document.createTextNode("(")); + $(node).append(document.createTextNode(this._constructorName)); for(var i = 0; i < this._fields.length; i++) { - node.appendChild(document.createTextNode(" ")); - appendChild(node, plt.helpers.toDomNode(this._fields[i], cache)); + $(node).append(document.createTextNode(" ")); + $(node).append(plt.helpers.toDomNode(this._fields[i], params)); } - node.appendChild(document.createTextNode(")")); + $(node).append(document.createTextNode(")")); return node; }; diff --git a/js-assembler/runtime-src/runtime.js b/js-assembler/runtime-src/runtime.js index 00b790a..75d48cd 100644 --- a/js-assembler/runtime-src/runtime.js +++ b/js-assembler/runtime-src/runtime.js @@ -88,8 +88,9 @@ if(this['plt'] === undefined) { this['plt'] = {}; } 'currentErrorDisplayer': function(MACHINE, domNode) { $(domNode).appendTo(document.body); }, - + 'currentInspector': plt.baselib.inspectors.DEFAULT_INSPECTOR, + 'currentOutputPort': new StandardOutputPort(), 'currentErrorPort': new StandardErrorPort(), 'currentSuccessHandler': function(MACHINE) {}, @@ -383,10 +384,10 @@ if(this['plt'] === undefined) { this['plt'] = {}; } var raise = function(MACHINE, e) { - if (typeof(window.console) !== 'undefined' && - typeof(console.log) === 'function') { + if (typeof(window['console']) !== 'undefined' && + typeof(console['log']) === 'function') { console.log(MACHINE); - if (e.stack) { console.log(e.stack); } + if (e['stack']) { console.log(e['stack']); } else { console.log(e); } } throw e; @@ -556,7 +557,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; } vs, f) { var args = []; - for (var i = 0; i < MACHINE.argcount.length; i++) { + for (var i = 0; i < MACHINE.argcount; i++) { if (i < mandatoryArgCount) { args.push(MACHINE.env[MACHINE.env.length - 1 - i]); } else { @@ -2078,16 +2079,74 @@ if(this['plt'] === undefined) { this['plt'] = {}; } procSpec, // FIXME: currently ignored immutables, // FIXME: currently ignored guard, // FIXME: currently ignored - constructorName // FIXME, currently ignored + constructorName ) { - // FIXME: we need to return those five values back. + // FIXME: typechecks. + + var structType = plt.baselib.structs.makeStructureType( + name, + superType, + initFieldCount, + autoFieldCount, + autoV, + //props, + //inspector, + //procSpec, + //immutables, + guard); + + console.log('constructor', name, superType, initFieldCount); + var constructorValue = + makePrimitiveProcedure( + constructorName, + jsnums.toFixnum(initFieldCount), + function(MACHINE) { + var args = []; + for(var i = 0; i < initFieldCount; i++) { + args.push(MACHINE.env[MACHINE.env.length - 1 - i]); + } + return structType.constructor.apply(null, args); + }); + + var predicateValue = + makePrimitiveProcedure( + String(name) + "?", + 1, + function(MACHINE) { + return structType.predicate(MACHINE.env[MACHINE.env.length - 1]); + }); + + var accessorValue = + makePrimitiveProcedure( + String(name) + "-accessor", + 2, + function(MACHINE) { + // FIXME: typechecks + return structType.accessor( + MACHINE.env[MACHINE.env.length - 1], + jsnums.toFixnum(MACHINE.env[MACHINE.env.length - 2])); + }); + + var mutatorValue = + makePrimitiveProcedure( + String(name) + "-mutator", + 3, + function(MACHINE) { + // FIXME: typechecks + return structType.mutator( + MACHINE.env[MACHINE.env.length - 1], + jsnums.toFixnum(MACHINE.env[MACHINE.env.length - 2]), + MACHINE.env[MACHINE.env.length - 3]); + }); + + finalizeClosureCall(MACHINE, - "type", - "constructor", - "predicate", - "accessor", - "mutator"); + structType, + constructorValue, + predicateValue, + accessorValue, + mutatorValue); }); }); @@ -2095,18 +2154,68 @@ if(this['plt'] === undefined) { this['plt'] = {}; } 'current-inspector', makeList(0, 1), function(MACHINE) { - return "inspector gadget"; + if (MACHINE.argcount === 1) { + MACHINE.params['currentInspector'] = MACHINE.env[MACHINE.env.length - 1]; + return VOID; + } else { + return MACHINE.params['currentInspector']; + } } ); + installPrimitiveProcedure( 'make-struct-field-accessor', makeList(2, 3), function(MACHINE){ - return 'a procedure'; + // FIXME: typechecks + var structType = MACHINE.env[MACHINE.env.length - 1]; + var index = MACHINE.env[MACHINE.env.length - 2]; + var name; + if (MACHINE.argcount === 3) { + name = String(MACHINE.env[MACHINE.env.length - 3]); + } else { + name = 'field' + index; + } + return makePrimitiveProcedure( + name, + 1, + function(MACHINE) { + return structType.accessor( + MACHINE.env[MACHINE.env.length - 1], + jsnums.toFixnum(index)); + }); + }); + installPrimitiveProcedure( + 'make-struct-field-mutator', + makeList(2, 3), + function(MACHINE){ + // FIXME: typechecks + var structType = MACHINE.env[MACHINE.env.length - 1]; + var index = MACHINE.env[MACHINE.env.length - 2]; + var name; + if (MACHINE.argcount === 3) { + name = String(MACHINE.env[MACHINE.env.length - 3]); + } else { + name = 'field' + index; + } + return makePrimitiveProcedure( + name, + 2, + function(MACHINE) { + return structType.mutator( + MACHINE.env[MACHINE.env.length - 1], + jsnums.toFixnum(index), + MACHINE.env[MACHINE.env.length - 2]); + }); + }); + + + +