Skip to content

Commit

Permalink
Added object member pointers and general fixes
Browse files Browse the repository at this point in the history
Added the ".&"-syntax to create pointers of object members, fixed copying of object function members
  • Loading branch information
schwalbe-t authored Jan 24, 2023
1 parent 4c17296 commit c993665
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
34 changes: 33 additions & 1 deletion currant.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ class CurrantNode {
CurrantStringNode,
CurrantMemberAccessNode,
CurrantJsReferenceNode,
CurrantMemberPointerNode,
CurrantPointerNode,
CurrantFunctionNode,
CurrantArrayNode,
Expand Down Expand Up @@ -1759,6 +1760,33 @@ class CurrantPointerNode extends CurrantNode {

}

class CurrantMemberPointerNode extends CurrantNode {

constructor() { super("member-pointer"); }

doParse() {
super.addChild(super.evalUntilLast(["dot"], true));
super.nextToken();
super.expectToken("ampersand");
super.nextToken();
super.expectToken("identifier");
this.refName = super.token().text;
super.expectEnd();
}

doExecute() {
let object = super.childValue(0);
if(object.type.constructor !== CurrantCustomType)
throw new Error(`unable to access member - object does not have member "${this.refName}"`);
object = object.get();
if(!object.variables.has(this.refName))
throw new Error(`unable to access member - object does not have member "${this.refName}"`);
this.ref = CurrantBlockNode.staticGetVariableWrapper(object.variables, object.block, this.refName);
return new CurrantPointerType().fromNode(this);
}

}

class CurrantPointerType extends CurrantType {
varStorage(size) { return new Array(size); }
instNode(node) {
Expand Down Expand Up @@ -2000,10 +2028,14 @@ class CurrantCustomObject {

constructor(data) {
this.variables = new Map();
this.block = data.block;
for(const keyName of data.variables.keys()) {
this.variables.set(keyName, new CurrantBlockVariableWrapperObject(data.variables.get(keyName).get().copy()));
if(this.variables.get(keyName).get().type.constructor === CurrantFunctionType) {
const functionBody = this.variables.get(keyName).get().get().body;
if(typeof functionBody !== "undefined") functionBody.block = this;
}
}
this.block = data.block;
}

}
Expand Down
1 change: 1 addition & 0 deletions currant/nodes/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CurrantNode {
CurrantStringNode,
CurrantMemberAccessNode,
CurrantJsReferenceNode,
CurrantMemberPointerNode,
CurrantPointerNode,
CurrantFunctionNode,
CurrantArrayNode,
Expand Down
6 changes: 5 additions & 1 deletion currant/types/customType.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ class CurrantCustomObject {

constructor(data) {
this.variables = new Map();
this.block = data.block;
for(const keyName of data.variables.keys()) {
this.variables.set(keyName, new CurrantBlockVariableWrapperObject(data.variables.get(keyName).get().copy()));
if(this.variables.get(keyName).get().type.constructor === CurrantFunctionType) {
const functionBody = this.variables.get(keyName).get().get().body;
if(typeof functionBody !== "undefined") functionBody.block = this;
}
}
this.block = data.block;
}

}
Expand Down
27 changes: 27 additions & 0 deletions currant/types/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ class CurrantPointerNode extends CurrantNode {

}

class CurrantMemberPointerNode extends CurrantNode {

constructor() { super("member-pointer"); }

doParse() {
super.addChild(super.evalUntilLast(["dot"], true));
super.nextToken();
super.expectToken("ampersand");
super.nextToken();
super.expectToken("identifier");
this.refName = super.token().text;
super.expectEnd();
}

doExecute() {
let object = super.childValue(0);
if(object.type.constructor !== CurrantCustomType)
throw new Error(`unable to access member - object does not have member "${this.refName}"`);
object = object.get();
if(!object.variables.has(this.refName))
throw new Error(`unable to access member - object does not have member "${this.refName}"`);
this.ref = CurrantBlockNode.staticGetVariableWrapper(object.variables, object.block, this.refName);
return new CurrantPointerType().fromNode(this);
}

}

class CurrantPointerType extends CurrantType {
varStorage(size) { return new Array(size); }
instNode(node) {
Expand Down
11 changes: 10 additions & 1 deletion tests/pointers.crn
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ assert(*b == 420i32);

*b = -10i32;

assert(a == -10i32);
assert(a == -10i32);


C: type = $(val: i32) {};

c: C = C(5i32);
d: ptr = c.&val;
*d = 10i32;

assert(c.val == 10i32);

0 comments on commit c993665

Please sign in to comment.