64 bits and others

This commit is contained in:
Adam D. Ruppe 2013-07-10 13:34:58 -04:00
parent c3789483cb
commit c38f20fd7b
2 changed files with 105 additions and 9 deletions

36
jsvar.d
View file

@ -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?) 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; 1 .. 10) -> for(var i = 1; i < 10; i++)
foreach(i; array) -> for(var i = 0; i < array.length; 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? 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 10) __FILE__ and __LINE__ as default function arguments should work like in D
16) stack traces on script exceptions 16) stack traces on script exceptions
17) an exception type that we can create in the script 17) an exception type that we can create in the script
@ -683,11 +683,33 @@ struct var {
return cast(int)(f - r); return cast(int)(f - r);
} }
/*
public bool opEquals(T)(T t) { 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 { public enum Type {
Object, Array, Integral, Floating, String, Function, Boolean Object, Array, Integral, Floating, String, Function, Boolean
@ -728,10 +750,10 @@ struct var {
} }
public var opSlice(var e1, var e2) { 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(this.payloadType() == Type.Array) {
if(e1 > _payload._array.length) if(e1 > _payload._array.length)
e1 = _payload._array.length; e1 = _payload._array.length;

View file

@ -161,7 +161,7 @@ class TokenStream(TextStream) {
int lineNumber = 1; int lineNumber = 1;
string scriptFilename; string scriptFilename;
void advance(int size) { void advance(ptrdiff_t size) {
foreach(i; 0 .. size) { foreach(i; 0 .. size) {
if(text.empty) if(text.empty)
break; break;
@ -363,8 +363,9 @@ TokenStream!TextStream lexScript(TextStream)(TextStream textStream, string scrip
struct InterpretResult { struct InterpretResult {
var value; var value;
PrototypeObject sc; PrototypeObject sc;
enum FlowControl { Normal, Return, Continue, Break } enum FlowControl { Normal, Return, Continue, Break, Goto }
FlowControl flowControl; FlowControl flowControl;
string flowControlDetails; // which label
} }
class Expression { class Expression {
@ -377,6 +378,8 @@ class MixinExpression : Expression {
this.e1 = e1; this.e1 = e1;
} }
override string toString() { return "mixin(" ~ e1.toString() ~ ")"; }
override InterpretResult interpret(PrototypeObject sc) { override InterpretResult interpret(PrototypeObject sc) {
return InterpretResult(.interpret(e1.interpret(sc).value.get!string ~ ";", sc), sc); return InterpretResult(.interpret(e1.interpret(sc).value.get!string ~ ";", sc), sc);
} }
@ -447,6 +450,16 @@ class NullLiteralExpression : Expression {
class ArrayLiteralExpression : Expression { class ArrayLiteralExpression : Expression {
this() {} this() {}
override string toString() {
string s = "[";
foreach(i, ele; elements) {
if(i) s ~= ", ";
s ~= ele.toString();
}
s ~= "]";
return s;
}
Expression[] elements; Expression[] elements;
override InterpretResult interpret(PrototypeObject sc) { override InterpretResult interpret(PrototypeObject sc) {
var n = var.emptyArray; var n = var.emptyArray;
@ -457,6 +470,24 @@ class ArrayLiteralExpression : Expression {
} }
class ObjectLiteralExpression : Expression { class ObjectLiteralExpression : Expression {
Expression[string] elements; 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) { override InterpretResult interpret(PrototypeObject sc) {
var n = var.emptyObject; var n = var.emptyObject;
foreach(k, v; elements) foreach(k, v; elements)
@ -478,6 +509,15 @@ class FunctionLiteralExpression : Expression {
this.functionBody = bod; this.functionBody = bod;
} }
override string toString() {
string s = "function (";
s ~= arguments.toString();
s ~= ") ";
s ~= functionBody.toString();
return s;
}
/* /*
function identifier (arg list) expression function identifier (arg list) expression
@ -530,6 +570,19 @@ class VariableDeclaration : Expression {
this() {} 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) { override InterpretResult interpret(PrototypeObject sc) {
var n; var n;
@ -624,6 +677,8 @@ class AssignExpression : Expression {
this.e2 = e2; this.e2 = e2;
} }
override string toString() { return e1.toString() ~ " = " ~ e2.toString(); }
override InterpretResult interpret(PrototypeObject sc) { override InterpretResult interpret(PrototypeObject sc) {
auto v = cast(VariableExpression) e1; auto v = cast(VariableExpression) e1;
if(v is null) if(v is null)
@ -779,6 +834,11 @@ class LoopControlExpression : Expression {
else assert(0, op); else assert(0, op);
} }
override string toString() {
import std.string;
return to!string(this.op).toLower();
}
override InterpretResult interpret(PrototypeObject sc) { override InterpretResult interpret(PrototypeObject sc) {
return InterpretResult(var(null), sc, op); return InterpretResult(var(null), sc, op);
} }
@ -792,6 +852,8 @@ class ReturnExpression : Expression {
value = v; value = v;
} }
override string toString() { return "return " ~ value.toString(); }
override InterpretResult interpret(PrototypeObject sc) { override InterpretResult interpret(PrototypeObject sc) {
return InterpretResult(value.interpret(sc).value, sc, InterpretResult.FlowControl.Return); return InterpretResult(value.interpret(sc).value, sc, InterpretResult.FlowControl.Return);
} }
@ -804,6 +866,18 @@ class ScopeExpression : Expression {
Expression[] expressions; 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) { override InterpretResult interpret(PrototypeObject sc) {
var ret; var ret;