Skip to content

Commit

Permalink
Add property write expressions to the EasyScript grammar.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinny85 committed Oct 22, 2023
1 parent e05ff78 commit d1b2045
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ subroutine_decl : name=ID '(' args=func_args ')' '{' stmt* '}' ;
func_args : (ID (',' ID)* )? ;

expr1 : ID '=' expr1 #AssignmentExpr1
| object=expr5 '.' ID '=' rvalue=expr1 #PropertyWriteExpr1
| arr=expr5 '[' index=expr1 ']' '=' rvalue=expr1 #ArrayIndexWriteExpr1
| expr2 #PrecedenceTwoExpr1
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.endoflineblog.truffle.part_13.nodes.exprs.objects;

import com.endoflineblog.truffle.part_13.nodes.exprs.EasyScriptExprNode;
import com.endoflineblog.truffle.part_13.runtime.Undefined;
import com.oracle.truffle.api.frame.VirtualFrame;

public class ThisExprNode extends EasyScriptExprNode {
public final class ThisExprNode extends EasyScriptExprNode {
@Override
public Object executeGeneric(VirtualFrame frame) {
// because of how we handle calls in FunctionDispatchNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Used in code like {@code t.myProp}.
* Identical to the class with the same name from part 11.
*/
@NodeChild("target")
@NodeChild("targetExpr")
@NodeField(name = "propertyName", type = String.class)
public abstract class PropertyReadExprNode extends EasyScriptExprNode {
protected abstract String getPropertyName();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.endoflineblog.truffle.part_13.nodes.exprs.properties;

import com.endoflineblog.truffle.part_13.exceptions.EasyScriptException;
import com.endoflineblog.truffle.part_13.nodes.exprs.EasyScriptExprNode;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeField;
import com.oracle.truffle.api.dsl.Specialization;

/**
* The Node for reading properties of objects.
* Used in code like {@code t.myProp = 3}.
*/
@NodeChild("targetExpr")
@NodeField(name = "propertyName", type = String.class)
@NodeChild("rvalueExpr")
public abstract class PropertyWriteExprNode extends EasyScriptExprNode {
protected abstract String getPropertyName();

@Specialization
protected Object writeProperty(Object target, Object rvalue) {
throw new EasyScriptException("PropertyWriteExprNode.writeProperty() not implemented yet");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.endoflineblog.truffle.part_13.nodes.exprs.objects.NewExprNodeGen;
import com.endoflineblog.truffle.part_13.nodes.exprs.objects.ThisExprNode;
import com.endoflineblog.truffle.part_13.nodes.exprs.properties.PropertyReadExprNodeGen;
import com.endoflineblog.truffle.part_13.nodes.exprs.properties.PropertyWriteExprNodeGen;
import com.endoflineblog.truffle.part_13.nodes.exprs.variables.GlobalVarAssignmentExprNodeGen;
import com.endoflineblog.truffle.part_13.nodes.exprs.variables.GlobalVarReferenceExprNodeGen;
import com.endoflineblog.truffle.part_13.nodes.exprs.variables.LocalVarAssignmentExprNode;
Expand Down Expand Up @@ -398,6 +399,8 @@ private EasyScriptExprNode parseExpr1(EasyScriptParser.Expr1Context expr1) {
}
if (expr1 instanceof EasyScriptParser.AssignmentExpr1Context) {
return parseAssignmentExpr((EasyScriptParser.AssignmentExpr1Context) expr1);
} else if (expr1 instanceof EasyScriptParser.PropertyWriteExpr1Context) {
return this.parsePropertyWriteExpr((EasyScriptParser.PropertyWriteExpr1Context) expr1);
} else if (expr1 instanceof EasyScriptParser.ArrayIndexWriteExpr1Context) {
return this.parseArrayIndexWriteExpr((EasyScriptParser.ArrayIndexWriteExpr1Context) expr1);
} else {
Expand All @@ -424,6 +427,13 @@ private EasyScriptExprNode parseAssignmentExpr(EasyScriptParser.AssignmentExpr1C
}
}

private EasyScriptExprNode parsePropertyWriteExpr(EasyScriptParser.PropertyWriteExpr1Context propertyWriteExpr) {
return PropertyWriteExprNodeGen.create(
this.parseExpr5(propertyWriteExpr.object),
this.parseExpr1(propertyWriteExpr.rvalue),
propertyWriteExpr.ID().getText());
}

private EasyScriptExprNode parseArrayIndexWriteExpr(EasyScriptParser.ArrayIndexWriteExpr1Context arrayIndexWriteExpr) {
return ArrayIndexWriteExprNodeGen.create(
this.parseExpr5(arrayIndexWriteExpr.arr),
Expand Down

0 comments on commit d1b2045

Please sign in to comment.