Skip to content

Commit

Permalink
"undef" operator
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Aug 19, 2024
1 parent 0229270 commit c7f3ba9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/perlonjava/EmitterVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ private void handleUnaryBuiltin(UnaryOperatorNode node, String operator) throws
// Unary operator with optional arguments, called without arguments
// example: undef()
ctx.mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/perlonjava/RuntimeScalar", operator, "()Lorg/perlonjava/RuntimeScalar;", false);
} else if (operator.equals("undef")) {
operator = "undefine";
node.operand.accept(this.with(RuntimeContextType.RUNTIME));
ctx.mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/perlonjava/RuntimeList", operator, "()Lorg/perlonjava/RuntimeList;", false);
} else {
node.operand.accept(this.with(RuntimeContextType.SCALAR));
ctx.mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/perlonjava/RuntimeScalar", operator, "()Lorg/perlonjava/RuntimeScalar;", false);
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/org/perlonjava/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,26 +319,29 @@ private Node parsePrimary() {
return new StringNode(token.text, tokenIndex);
}
switch (token.text) {
case "undef":
// Handle 'undef' keyword as a unary operator with no operand
return new UnaryOperatorNode("undef", null, tokenIndex);
case "not":
// Handle 'not' keyword as a unary operator with an operand
operand = parseExpression(getPrecedence(token.text) + 1);
return new UnaryOperatorNode("not", operand, tokenIndex);
case "abs":
case "log":
case "rand":
case "undef":
String text = token.text;
operand = parseZeroOrOneList(0);
if (((ListNode) operand).elements.isEmpty()) {
if (text.equals("rand")) {
// create "1"
operand = new NumberNode("1", tokenIndex);
} else {
// create `$_` variable
operand = new UnaryOperatorNode(
switch (text) {
case "undef":
break; // leave it empty
case "rand":
// create "1"
operand = new NumberNode("1", tokenIndex);
break;
default:
// create `$_` variable
operand = new UnaryOperatorNode(
"$", new IdentifierNode("_", tokenIndex), tokenIndex);
break;
}
}
return new UnaryOperatorNode(text, operand, tokenIndex);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/perlonjava/RuntimeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ public boolean getBooleanRef() {
return true;
}

public RuntimeArray undefine() {
this.elements.clear();
return this;
}

// Convert the array to a string (for debugging purposes)
@Override
public String toString() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/perlonjava/RuntimeHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ public boolean getBooleanRef() {
return true;
}

public RuntimeHash undefine() {
this.elements.clear();
return this;
}

// Convert the hash to a string (for debugging purposes)
@Override
public String toString() {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/perlonjava/RuntimeList.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ public Iterator<RuntimeScalar> iterator() {

// Operators

// undefine the elements of the list
public RuntimeList undefine() {
for (RuntimeBaseEntity elem : elements) {
if (elem instanceof RuntimeScalar) {
((RuntimeScalar) elem).undefine();
} else if (elem instanceof RuntimeArray) {
((RuntimeArray) elem).undefine();
} else if (elem instanceof RuntimeHash) {
((RuntimeHash) elem).undefine();
}
}
return this;
}

public RuntimeScalar print() {
StringBuilder sb = new StringBuilder();
for (RuntimeBaseEntity element : elements) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/perlonjava/RuntimeScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ public static RuntimeScalar undef() {
return new RuntimeScalar();
}

public RuntimeScalar undefine() {
this.type = RuntimeScalarType.UNDEF;
return this;
}

public RuntimeScalar stringConcat(RuntimeScalar b) {
return new RuntimeScalar(this + b.toString());
}
Expand Down

0 comments on commit c7f3ba9

Please sign in to comment.