Skip to content

Commit

Permalink
Added function call checks
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Apr 25, 2022
1 parent 805666d commit 11a5154
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/mihailris/runexp/ExpConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ public class ExpConstants {
public static final int ERR_UNKNOWN_NAME = 4;
public static final int ERR_INVALID_BRACKET = 5;
public static final int ERR_EMPTY_EXPRESSION = 6;



public static final String BINARY_OPERATORS = "+-*^/%";
public static final int ERR_UNKNOWN_FUNCTION = 7;
public static final int ERR_WRONG_ARGS_COUNT = 8;
public static final String POW_OP = "^";
}
18 changes: 15 additions & 3 deletions src/mihailris/runexp/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static List<ExpNode> performRawTokens(List<RawToken> tokens, boolean cons
switch (rawToken.tag){
case NAME:{
if (i < tokens.size()-1 && tokens.get(i+1).tag == RawToken.Tag.OPEN){
if (RunExp.functions.get(rawToken.text) == null)
throw new ExpCompileException(
"unknown function '"+rawToken.text+"'", rawToken.pos, ERR_UNKNOWN_FUNCTION);
token = new Token(Token.Tag.FUNCTION, rawToken.text, rawToken.pos);
} else {
Float constantValue = RunExp.constants.get(rawToken.text);
Expand Down Expand Up @@ -128,7 +131,7 @@ private static int parseBlocks(List<ExpNode> source, List<ExpNode> nodes, int in
return index;
}

private static void parseCalls(List<ExpNode> source, List<ExpNode> nodes){
private static void parseCalls(List<ExpNode> source, List<ExpNode> nodes) {
ExpNode prev;
ExpNode node = null;
for (ExpNode expNode : source) {
Expand All @@ -151,7 +154,7 @@ private static void parseCalls(List<ExpNode> source, List<ExpNode> nodes){
}
}

private static void parseArguments(List<ExpNode> source, List<ExpNode> nodes, boolean call) {
private static void parseArguments(List<ExpNode> source, List<ExpNode> nodes, boolean call) throws ExpCompileException {
List<ExpNode> argument = new ArrayList<>();
for (ExpNode node : source) {
if (node.token != null && node.token.tag == Token.Tag.SEPARATOR) {
Expand All @@ -164,7 +167,16 @@ private static void parseArguments(List<ExpNode> source, List<ExpNode> nodes, bo
if (node.token == null) {
List<ExpNode> out = new ArrayList<>();
parseArguments(node.nodes, out, node.command != null);
argument.add(new ExpNode(node.command, out));
node = new ExpNode(node.command, out);

if (node.command != null && node.command.tag == Token.Tag.FUNCTION){
RunExpFunction function = RunExp.functions.get(node.command.string);
if (function.argCount != out.size())
throw new ExpCompileException(
"wrong arguments count passed to '"+function.name+"' "+out.size()+"/"+function.argCount,
node.command.pos, ERR_WRONG_ARGS_COUNT);
}
argument.add(node);
continue;
}
argument.add(node);
Expand Down

0 comments on commit 11a5154

Please sign in to comment.