From c38f20fd7bc7a0736286e37e945cf41fd01d9d87 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Wed, 10 Jul 2013 13:34:58 -0400 Subject: [PATCH] 64 bits and others --- jsvar.d | 36 +++++++++++++++++++++----- script.d | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 9 deletions(-) diff --git a/jsvar.d b/jsvar.d index f4036d9..89a89ee 100644 --- a/jsvar.d +++ b/jsvar.d @@ -60,9 +60,9 @@ import std.json; 3) loops (foreach - preferably about as well as D (int ranges, arrays, objects with opApply overloaded, and input ranges), do while?) foreach(i; 1 .. 10) -> for(var i = 1; i < 10; i++) foreach(i; array) -> for(var i = 0; i < array.length; i++) - foreach(i; object) -> for(var v = object.visit; !v.empty; v.popFront) { var i = v.front; / *...* / } + foreach(i; object) -> for(var v = new object.iterator; !v.empty(); v.popFront()) { var i = v.front(); / *...* / } 4) switches? - 6) explicit type conversions somehow + 6) explicit type conversions somehow (cast?) 10) __FILE__ and __LINE__ as default function arguments should work like in D 16) stack traces on script exceptions 17) an exception type that we can create in the script @@ -683,11 +683,33 @@ struct var { return cast(int)(f - r); } - /* public bool opEquals(T)(T t) { - + return this.opEquals(var(t)); + } + + + public bool opEquals(T:var)(T t) { + // FIXME: should this be == or === ? + if(this._type != t._type) + return false; + final switch(this._type) { + case Type.Object: + return _payload._object is t._payload._object; + case Type.Integral: + return _payload._integral == t._payload._integral; + case Type.Boolean: + return _payload._boolean == t._payload._boolean; + case Type.Floating: + return _payload._floating == t._payload._floating; // FIXME: approxEquals? + case Type.String: + return _payload._string == t._payload._string; + case Type.Function: + return _payload._function is t._payload._function; + case Type.Array: + return _payload._array == t._payload._array; + } + assert(0); } - */ public enum Type { Object, Array, Integral, Floating, String, Function, Boolean @@ -728,10 +750,10 @@ struct var { } public var opSlice(var e1, var e2) { - return this.opSlice(e1.get!int, e2.get!int); + return this.opSlice(e1.get!ptrdiff_t, e2.get!ptrdiff_t); } - public var opSlice(int e1, int e2) { + public var opSlice(ptrdiff_t e1, ptrdiff_t e2) { if(this.payloadType() == Type.Array) { if(e1 > _payload._array.length) e1 = _payload._array.length; diff --git a/script.d b/script.d index 67f5c9e..63dac19 100644 --- a/script.d +++ b/script.d @@ -161,7 +161,7 @@ class TokenStream(TextStream) { int lineNumber = 1; string scriptFilename; - void advance(int size) { + void advance(ptrdiff_t size) { foreach(i; 0 .. size) { if(text.empty) break; @@ -363,8 +363,9 @@ TokenStream!TextStream lexScript(TextStream)(TextStream textStream, string scrip struct InterpretResult { var value; PrototypeObject sc; - enum FlowControl { Normal, Return, Continue, Break } + enum FlowControl { Normal, Return, Continue, Break, Goto } FlowControl flowControl; + string flowControlDetails; // which label } class Expression { @@ -377,6 +378,8 @@ class MixinExpression : Expression { this.e1 = e1; } + override string toString() { return "mixin(" ~ e1.toString() ~ ")"; } + override InterpretResult interpret(PrototypeObject sc) { return InterpretResult(.interpret(e1.interpret(sc).value.get!string ~ ";", sc), sc); } @@ -447,6 +450,16 @@ class NullLiteralExpression : Expression { class ArrayLiteralExpression : Expression { this() {} + override string toString() { + string s = "["; + foreach(i, ele; elements) { + if(i) s ~= ", "; + s ~= ele.toString(); + } + s ~= "]"; + return s; + } + Expression[] elements; override InterpretResult interpret(PrototypeObject sc) { var n = var.emptyArray; @@ -457,6 +470,24 @@ class ArrayLiteralExpression : Expression { } class ObjectLiteralExpression : Expression { Expression[string] elements; + + override string toString() { + string s = "json!q{"; + bool first = true; + foreach(k, e; elements) { + if(first) + first = false; + else + s ~= ", "; + + s ~= "\"" ~ k ~ "\":"; // FIXME: escape if needed + s ~= e.toString(); + } + + s ~= "}"; + return s; + } + override InterpretResult interpret(PrototypeObject sc) { var n = var.emptyObject; foreach(k, v; elements) @@ -478,6 +509,15 @@ class FunctionLiteralExpression : Expression { this.functionBody = bod; } + override string toString() { + string s = "function ("; + s ~= arguments.toString(); + + s ~= ") "; + s ~= functionBody.toString(); + return s; + } + /* function identifier (arg list) expression @@ -530,6 +570,19 @@ class VariableDeclaration : Expression { this() {} + override string toString() { + string s = ""; + foreach(i, ident; identifiers) { + if(i) + s ~= ", "; + s ~= "var " ~ ident; + if(initializers[i] !is null) + s ~= " = " ~ initializers[i].toString(); + } + return s; + } + + override InterpretResult interpret(PrototypeObject sc) { var n; @@ -624,6 +677,8 @@ class AssignExpression : Expression { this.e2 = e2; } + override string toString() { return e1.toString() ~ " = " ~ e2.toString(); } + override InterpretResult interpret(PrototypeObject sc) { auto v = cast(VariableExpression) e1; if(v is null) @@ -779,6 +834,11 @@ class LoopControlExpression : Expression { else assert(0, op); } + override string toString() { + import std.string; + return to!string(this.op).toLower(); + } + override InterpretResult interpret(PrototypeObject sc) { return InterpretResult(var(null), sc, op); } @@ -792,6 +852,8 @@ class ReturnExpression : Expression { value = v; } + override string toString() { return "return " ~ value.toString(); } + override InterpretResult interpret(PrototypeObject sc) { return InterpretResult(value.interpret(sc).value, sc, InterpretResult.FlowControl.Return); } @@ -804,6 +866,18 @@ class ScopeExpression : Expression { Expression[] expressions; + override string toString() { + string s; + s = "{\n"; + foreach(expr; expressions) { + s ~= "\t"; + s ~= expr.toString(); + s ~= ";\n"; + } + s ~= "}"; + return s; + } + override InterpretResult interpret(PrototypeObject sc) { var ret;