Skip to content

Commit

Permalink
- Use some native code in python
Browse files Browse the repository at this point in the history
- Remove useless options menu
  • Loading branch information
basicer committed Oct 27, 2019
1 parent 7395be4 commit 6c72ecf
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions contrib/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@
</ul>
</div>
</li>
<!--
<li class="active"><a href="#" data-toggle="modal" data-target="#options">Options</a></li>
-->
<li ><a href="test">Tests</a></li>
</ul>

Expand Down
8 changes: 8 additions & 0 deletions plugins/lang-python/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const skulpty = require('skulpty');
const PythonRuntime = require('./runtime');
let esper;

function parser(code, options) {
Expand Down Expand Up @@ -75,6 +76,13 @@ let plugin = module.exports = {
realm.pythonStringBaseInstance.properties[k] = realm.StringPrototype.properties[map[k]];
}
realm.loadLangaugeStartupCode(startupCodeAST);
realm.PythonRuntime = new PythonRuntime.PythonRuntime(realm);
realm.PythonListProto = new PythonRuntime.PythonListProto(realm);
realm.PythonList = new PythonRuntime.PythonList(realm);
let pr = realm.globalScope.get("__pythonRuntime");
let copy = (x,y,z) => pr.getImmediate(x).setImmediate(y, realm.PythonRuntime.getImmediate(z));
//copy("ops","subscriptIndex", "subscriptIndex");
copy("functions", "str", "str");
},
startupCode: () => startupCodeAST,
makeLiteralValue: function(v, realm, n) {
Expand Down
144 changes: 144 additions & 0 deletions plugins/lang-python/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
'use strict';

const EasyObjectValue = require('../../src/values/EasyObjectValue');
const ObjectValue = require('../../src/values/ObjectValue');
const ArrayValue = require('../../src/values/ArrayValue');
const PrimitiveValue = require('../../src/values/PrimitiveValue');
const CompletionRecord = require('../../src/CompletionRecord');
const Value = require('../../src/Value');
const _g = require('../../src/GenDash');



class PythonRuntime extends EasyObjectValue {
constructor(realm) {
super(realm);

let functions = new ObjectValue(realm);
functions.setImmediate("str", this.getImmediate("str"));
functions.setImmediate("range", this.getImmediate("range"));
functions.setImmediate("round", this.getImmediate("round"));
functions.setImmediate("abs", realm.Math.getImmediate("abs"));

this.setImmediate("functions", functions);

let objects = new ObjectValue(realm);
objects.setImmediate("list", new PythonList(realm));
objects.setImmediate("tuple", new PythonList(realm));
this.setImmediate("objects", objects);

let ops = new ObjectValue(realm);
ops.setImmediate("multiply", this.getImmediate("multiply"));
ops.setImmediate("add", this.getImmediate("add"));
ops.setImmediate("subscriptIndex", this.getImmediate("subscriptIndex"));
ops.setImmediate("in", this.getImmediate("in"));
this.setImmediate("ops", ops);

}
static *list$e(thiz, args, s) {
return new PythonList(s.realm);
}

static *makeList$e(thiz, args, s) {
return new PythonList(s.realm);
}

static *str$e(thiz, args, s) {
return yield * args[0].toStringValue();
}

static *range(thiz, args, s) {
let result = [];
let start = 0;
let step = 1;
let end = yield * args[0].toIntNative();
if ( args.length > 1 ) {
start = end;
end = yield * args[1].toIntNative();
}
if ( args.length > 2 ) {
step = yield * args[2].toIntNative();
}
console.log(start, step, end);

let i = start;
while (i < end && step > 0 || i > end && step < 0) {
result.push(Value.fromNative(i));
i += step;
}

return yield * s.realm.PythonList.call(null, result, s);
}

static *round$e(thiz, args, s) {
if ( args.length > 0 ) {
let n = yield * args[0].toNumberNative();
let digits = yield * args[1].toIntNative();
let extra = Math.pow(10, digits);
return Value.fromNative(Math.round(n * extra) / extra);
}
return Value.fromNative(Math.round(yield * args[0].toNumberNative()));
}


static *in$e(thiz, args, s) {
let t = args[0];
let o = args[1];
if ( o.has("indexOf") ) {
let result = yield * (yield * o.get("indexOf")).call(Value.undef, [t], s);
return yiled * result.toBoolean();
}
return yield * o.inOperator(t);
}

//Ops

static *add$e(thiz, args, s) {
return yield * args[0].add(args[1]);
}

static *multiply$e(thiz, args, s) {
return yield * args[0].multiply(args[1]);
}

static *subscriptIndex$e(thiz, args, s) {
return yield * args[0].get(args[1].toNative());
}
}


class PythonList extends EasyObjectValue {
*call(thiz, args, s) {
let result = new ArrayValue(s.realm);
result.setPrototype(s.realm.PythonListProto);

result.setImmediate('length', Value.fromNative(args.length));
result.properties.length.enumerable = false;

for ( let i = 0; i < args.length; ++i ) {
let v = args[i];
if ( !(v instanceof Value) ) v = realm.fromNative(v);
result.setImmediate(i, v);
}
console.log(result.debugString);
return result;
}

callPrototype(realm) { return realm.PythonListProto; }
constructorFor(realm) { return realm.PythonListProto; }
//objPrototype(realm) { return realm.Function; }
}


class PythonListProto extends EasyObjectValue {
constructor(realm) {
super(realm);
this.setImmediate("push", realm.ArrayPrototype.getImmediate("push"));
this.setImmediate("pop", realm.ArrayPrototype.getImmediate("pop"));
this.setImmediate("sort", realm.ArrayPrototype.getImmediate("sort"));
}

}
PythonListProto.prototype.wellKnownName = '%PythonList%';

module.exports = {PythonRuntime, PythonList, PythonListProto};

0 comments on commit 6c72ecf

Please sign in to comment.