Skip to content

Commit

Permalink
Merge pull request #127 from aya-lang/current-file-op
Browse files Browse the repository at this point in the history
Add operator for getting the path to the current file (M0)
  • Loading branch information
nick-paul authored Jan 1, 2025
2 parents a733b30 + ec22640 commit 8fa9b94
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/aya/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import aya.parser.tokens.BlockToken;
import aya.parser.tokens.CDictToken;
import aya.parser.tokens.CharToken;
import aya.parser.tokens.CurrentFileToken;
import aya.parser.tokens.DictToken;
import aya.parser.tokens.KeyVarToken;
import aya.parser.tokens.LambdaToken;
Expand Down Expand Up @@ -215,10 +216,15 @@ else if (in.peek() <= Ops.MAX_OP){

// Math Operators
else if (current == 'M') {
try {
tokens.add(new OperatorToken("" + in.next(), OperatorToken.MATH_OP, in.currentRef()));
} catch (EndOfInputError e) {
throw new SyntaxError("Expected op name after 'M'", in.currentRef());
if (in.peek() == '0') {
in.next(); // Skip 0
tokens.add(new CurrentFileToken(in.currentRef()));
} else {
try {
tokens.add(new OperatorToken("" + in.next(), OperatorToken.MATH_OP, in.currentRef()));
} catch (EndOfInputError e) {
throw new SyntaxError("Expected op name after 'M'", in.currentRef());
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/aya/parser/tokens/CurrentFileToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package aya.parser.tokens;

import aya.exceptions.parser.ParserException;
import aya.instruction.Instruction;
import aya.instruction.StringLiteralInstruction;
import aya.parser.SourceStringRef;

public class CurrentFileToken extends StdToken {

public CurrentFileToken(SourceStringRef source) {
super(source.getSource().getFilename(), Token.STRING, source);
}

@Override
public Instruction getInstruction() throws ParserException {
return new StringLiteralInstruction(source, this.source.getSource().getFilename());
}

@Override
public String typeString() {
return "constant_current_file";
}
}

0 comments on commit 8fa9b94

Please sign in to comment.