diff options
author | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-11 17:28:55 +0200 |
---|---|---|
committer | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-11 17:28:55 +0200 |
commit | 98ddff97ec1d092b18ef2d176e83bd92f9671e03 (patch) | |
tree | af1457e05063421dc2860e6c033e58b28f9a8e4e /calculator-java/src/main/java/ch/bfh/parser/StatementParser.java | |
parent | 49f30529d9cd2ed62902079a45d4d3c1f45afbda (diff) |
[+] Added StatementParser, [~] Restructured the Parsers
Diffstat (limited to 'calculator-java/src/main/java/ch/bfh/parser/StatementParser.java')
-rw-r--r-- | calculator-java/src/main/java/ch/bfh/parser/StatementParser.java | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java b/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java new file mode 100644 index 0000000..c85398b --- /dev/null +++ b/calculator-java/src/main/java/ch/bfh/parser/StatementParser.java @@ -0,0 +1,70 @@ +package ch.bfh.parser; + +import ch.bfh.CalculatorLexer; +import ch.bfh.Token; +import ch.bfh.exceptions.ParserException; + +public class StatementParser extends Parser{ + + String input; + ExpressionParser parsedExpression; + + public void parseStatement(String input){ + this.input = input; + cl = new CalculatorLexer(); + cl.initLexer(input); + parsedExpression = null; + parse(); + } + + @Override + protected void parse() { + Token token = cl.nextToken(); + String variableName = null; + lastToken = null; + loop: + while (token != null && token.type != Token.EOL) { + switch (token.type) { + case Token.LET: + if (lastToken != null) + throw new ParserException("The keyword 'let' cannot be placed anywhere else than at the beginning of an expression."); + break; + case Token.EQU: + if (lastToken == null || lastToken.type != Token.LET || variableName == null) + throw new ParserException("The inputted token '=' can only be placed in a variable declaration context (let variable = Expression)."); + break; + case Token.END: + if (lastToken != null) + throw new ParserException("The keyword 'exit' cannot be placed anywhere else than at the beginning of an expression."); + else + System.exit(0); + case Token.ID: + if (lastToken != null && lastToken.type == Token.LET && variableName == null) { // we are defining a new variable + variableName = token.str; + token = cl.nextToken(); + continue loop; // lastToken value will thus still be 'let' so that we could then parse the '=' token that is supposed to come next correctly. + }else if (variableName != null && lastToken != null && lastToken.type != Token.EQU) + throw new ParserException("Two consecutive variables ('"+variableName+"' and '"+token.str+"') were found in the declaration context."); + // the expression started with a variable -> no definition -> meaning it is an expression + default: + parsedExpression = new ExpressionParser(cl, token, false); + if (lastToken != null && lastToken.type == Token.EQU) // this still need to be put in the variables list + variables.put(variableName, parsedExpression); + break; + } + lastToken = token; + token = cl.nextToken(); + } + if (lastToken == null) + return; + if (lastToken.type == Token.LET || lastToken.type == Token.EQU) + throw new ParserException("Incomplete variable declaration"); + } + + @Override + public double getValue() { + if (parsedExpression == null) + return 0.0; + return parsedExpression.getValue(); + } +}
\ No newline at end of file |