Skip to content

Commit

Permalink
simplify support for new in stage 1:
Browse files Browse the repository at this point in the history
No new literal-string
No new with variables or string const
No functions called new
  • Loading branch information
NotFound committed May 27, 2012
1 parent 77eebb7 commit e355959
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 78 deletions.
2 changes: 1 addition & 1 deletion t/medium/02assignto.t
Expand Up @@ -9,7 +9,7 @@ function main()
using Test.More.is; using Test.More.is;
plan (2); plan (2);


var a= new "FixedStringArray"; var a= new [ "FixedStringArray" ];
a=: 42; a=: 42;
is (a instanceof "FixedStringArray", 1, "type unchanged"); is (a instanceof "FixedStringArray", 1, "type unchanged");
int i= a; int i= a;
Expand Down
2 changes: 1 addition & 1 deletion t/medium/04do.t
Expand Up @@ -8,7 +8,7 @@ function main()
{ {
plan(1); plan(1);


var c = new "Integer"; var c = new [ "Integer" ];
c =: 4; c =: 4;
int i = 0; int i = 0;
do { do {
Expand Down
2 changes: 1 addition & 1 deletion winxed.winxed
Expand Up @@ -125,7 +125,7 @@ function driver_main(var argv)
return; return;
} }


var env = new "Env"; var env = new [ "Env" ];
// Path for stage pbc files // Path for stage pbc files
string winxedpath = env["WINXED_PATH"]; string winxedpath = env["WINXED_PATH"];


Expand Down
91 changes: 16 additions & 75 deletions winxedst1.winxed
Expand Up @@ -5845,7 +5845,7 @@ class NewExpr : NewBaseExpr
{ {
self.Expr(owner, start); self.Expr(owner, start);


if (! (firstvalue.isstring() || firstvalue.isidentifier())) if (!firstvalue.isidentifier())
SyntaxError("Unimplemented", firstvalue); SyntaxError("Unimplemented", firstvalue);
self.value = firstvalue; self.value = firstvalue;
var t = tk.get(); var t = tk.get();
Expand All @@ -5858,17 +5858,6 @@ class NewExpr : NewBaseExpr
function optimize() function optimize()
{ {
var value = self.value; var value = self.value;
if (value.isidentifier()) {
// The identifier can be a string constant, check for that case
// and obtain the string value.
var desc = self.owner.getvar(value.getidentifier());
if (desc != null && desc.isconst()) {
value = desc.getvalue();
if (! (value instanceof StringLiteral))
SyntaxError("Constant value must evaluate to a string", value);
self.value = value.strval;
}
}
self.optimize_initializer(); self.optimize_initializer();
return self; return self;
} }
Expand All @@ -5880,81 +5869,38 @@ class NewExpr : NewBaseExpr
var value = self.value; var value = self.value;
int numinits = self.numargs(); int numinits = self.numargs();


const int BYNAME = 0, BYIDENT = 1;
int type = value.isstring() ? BYNAME :
value.isidentifier() ? BYIDENT : -1;

string reginit = ""; string reginit = "";
string regnew = result; string regnew = result;
string constructor; string constructor;
switch (numinits) { switch (numinits) {
case -1: case -1:
case 0: case 0:
break; break;
case 1:
if (type == BYIDENT) {
if (! is_init)
regnew = self.tempreg(REGvar);
}
else {
var initval = initializer.getfreearg(0);
reginit = initval.emit_get(e);
reginit = ", " + reginit;
}
break;
default: default:
if (type != BYIDENT)
SyntaxError("Multiple init arguments not allowed here", self);
if (! is_init) if (! is_init)
regnew = self.tempreg(REGvar); regnew = self.tempreg(REGvar);
} }


if (regnew == "") if (regnew == "")
regnew = self.tempreg(REGvar); regnew = self.tempreg(REGvar);


switch (type) { var cl = self.owner.checkclass(value);
case BYNAME: if (cl != null) {
// By name, usually a pmc. var key = cl.getpath();
string name = value.rawstring(); key.emit_new(e, self, regnew, reginit);
var aux = get_class(name); }
if (aux == null) else {
warn_class_unknown(e, name , value); var id = self.scopesearch( [value], SEARCH_CLASS);

if (id != null)
// Avoid using root_new here for a now. e.say(INDENT, "new ", regnew, ", ", id.getclasskey(), reginit);
//e.say("root_new ", regnew, ", ['parrot'; ", value, " ]", reginit);
e.say(INDENT, "new ", regnew, ", [ ", value, " ]", reginit);
if (numinits > 1) {
e.say(regnew, ".'", value, "'()");
}
break;
case BYIDENT:
var id = self.owner.getvar(value);
if (id == null) {
var cl = self.owner.checkclass(value);
if (cl != null) {
var key = cl.getpath();
key.emit_new(e, self, regnew, reginit);
}
else {
id = self.scopesearch( [value], SEARCH_CLASS);
if (id != null)
e.say(INDENT, "new ", regnew, ", ", id.getclasskey(), reginit);
else {
warn_class_unknown(e, value.getidentifier(), value);
e.say(INDENT, "new ", regnew, ", ['", value, "']", reginit);
}
}
constructor = value;
}
else { else {
//say("new with var"); warn_class_unknown(e, value.getidentifier(), value);
e.say(INDENT, "new ", regnew, ", ", id.getreg(), "", reginit); e.say(INDENT, "new ", regnew, ", ['", value, "']", reginit);
} }
break;
default:
InternalError("Unexpected type in new");
} }
if (numinits > 1 || (numinits >= 0 && type == BYIDENT)) { constructor = value;

if (numinits >= 0) {
self.emit_constructor(e, regnew, constructor); self.emit_constructor(e, regnew, constructor);
if (! is_init) if (! is_init)
e.emitset(result, regnew); e.emitset(result, regnew);
Expand Down Expand Up @@ -6050,16 +5996,11 @@ function parseNew(var tk, var owner, var start)
var t = tk.get(); var t = tk.get();


switch { switch {
case t.isop("("):
// Not a new operator, but a call to a function called new.
return new CallExpr(tk, owner, start,
new StringLiteral(owner, start));
case t.isop("["): case t.isop("["):
// Class specifier is a key // Class specifier is a key
return new NewIndexedExpr(tk, owner, start); return new NewIndexedExpr(tk, owner, start);
case t.isidentifier(): case t.isidentifier():
// Special case here. It can be an id, a qualified id or // Special case here. It can be an id or a qualified id
// a const string
var t2 = tk.get(); var t2 = tk.get();
tk.unget(t2); tk.unget(t2);
if (t2.isop(".")) { if (t2.isop(".")) {
Expand Down

0 comments on commit e355959

Please sign in to comment.