From 11a51542337fdff27941f05170455f54d8c9d112 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 25 Apr 2022 06:41:22 +0300 Subject: [PATCH] Added function call checks --- src/mihailris/runexp/ExpConstants.java | 6 ++---- src/mihailris/runexp/Parser.java | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/mihailris/runexp/ExpConstants.java b/src/mihailris/runexp/ExpConstants.java index 8d0c203..a4f8dbd 100644 --- a/src/mihailris/runexp/ExpConstants.java +++ b/src/mihailris/runexp/ExpConstants.java @@ -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 = "^"; } diff --git a/src/mihailris/runexp/Parser.java b/src/mihailris/runexp/Parser.java index 55fc0d1..a6cddc4 100644 --- a/src/mihailris/runexp/Parser.java +++ b/src/mihailris/runexp/Parser.java @@ -23,6 +23,9 @@ public static List performRawTokens(List 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); @@ -128,7 +131,7 @@ private static int parseBlocks(List source, List nodes, int in return index; } - private static void parseCalls(List source, List nodes){ + private static void parseCalls(List source, List nodes) { ExpNode prev; ExpNode node = null; for (ExpNode expNode : source) { @@ -151,7 +154,7 @@ private static void parseCalls(List source, List nodes){ } } - private static void parseArguments(List source, List nodes, boolean call) { + private static void parseArguments(List source, List nodes, boolean call) throws ExpCompileException { List argument = new ArrayList<>(); for (ExpNode node : source) { if (node.token != null && node.token.tag == Token.Tag.SEPARATOR) { @@ -164,7 +167,16 @@ private static void parseArguments(List source, List nodes, bo if (node.token == null) { List 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);