diff options
author | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-10 16:02:29 +0200 |
---|---|---|
committer | Maël Gassmann <mael.gassmann@students.bfh.ch> | 2021-06-10 16:02:29 +0200 |
commit | ee369b50b14ffe662e05355459f57c076680cd58 (patch) | |
tree | d9ee2f9af7b30f238dd0e9edf1edeb969dd26428 /calculator-java | |
parent | a657a82b01d958cc8d8a30268b3febe1eb124a77 (diff) |
[+] Basic stucture of the java calculator
Diffstat (limited to 'calculator-java')
-rw-r--r-- | calculator-java/.gitignore | 3 | ||||
-rw-r--r-- | calculator-java/pom.xml | 16 | ||||
-rw-r--r-- | calculator-java/src/main/java/CalculatorLexer.java | 105 | ||||
-rw-r--r-- | calculator-java/src/main/java/Token.java | 21 | ||||
-rw-r--r-- | calculator-java/src/main/java/exceptions/LexerException.java | 5 |
5 files changed, 150 insertions, 0 deletions
diff --git a/calculator-java/.gitignore b/calculator-java/.gitignore new file mode 100644 index 0000000..6c0dce9 --- /dev/null +++ b/calculator-java/.gitignore @@ -0,0 +1,3 @@ +calculator-java.iml +target/ +.idea/
\ No newline at end of file diff --git a/calculator-java/pom.xml b/calculator-java/pom.xml new file mode 100644 index 0000000..1edc3ba --- /dev/null +++ b/calculator-java/pom.xml @@ -0,0 +1,16 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <groupId>ch.bfh</groupId> + <artifactId>calculator-java</artifactId> + <version>1.0-SNAPSHOT</version> + + <properties> + <maven.compiler.source>11</maven.compiler.source> + <maven.compiler.target>11</maven.compiler.target> + </properties> + +</project>
\ No newline at end of file diff --git a/calculator-java/src/main/java/CalculatorLexer.java b/calculator-java/src/main/java/CalculatorLexer.java new file mode 100644 index 0000000..61b906c --- /dev/null +++ b/calculator-java/src/main/java/CalculatorLexer.java @@ -0,0 +1,105 @@ +// Lexer for classical arithmetic expressions with identifiers and assignements. +// Scans a source string char by char. + +import exceptions.LexerException; + +public class CalculatorLexer { + + private String src; // source string for lexical analysis + private int idx; // current index in source + private int len; // length of source + + public CalculatorLexer() { } + + public void initLexer(String source) { + this.src = source; + idx = 0; + len = src.length(); + } + + // Consumes letters only and builds an identifier + private String identifier() { + StringBuffer s = new StringBuffer(); + + do { + s.append(src.charAt(idx)); + idx++; + } while (idx < len && Character.isLetter(src.charAt(idx))); + return s.toString(); + } + + // Consumes digits and convert integer part and decimal part + // Convert characters using the formula + // "3456.253" = [(((3+0)*10+4)*10+5)*10+6]+[0.1*2+0.01*5+0.001*3] + private double number() throws LexerException { + double v = 0; // accumulates the result + double factor = 0.1; // factor for decimal part + + do { // integer part + v = v * 10 + Character.digit(src.charAt(idx),30); + idx++; + } while (idx < len && Character.isDigit(src.charAt(idx))); + if (idx < len && src.charAt(idx) == '.') { // decimal point + idx++; + if (idx < len && Character.isDigit(src.charAt(idx))) { // decimal part + while (idx < len && Character.isDigit(src.charAt(idx))) { + v = v + (factor * Character.digit(src.charAt(idx),30)); + factor = factor * 0.1; + idx++; + } + } + else throw new LexerException("Illegal number: decimal part missing"); + } + return v; + } + + // Skips blanks, tabs, newlines + private void skip() { + char c; + while (idx < len) { + c = src.charAt(idx); + if (c==' ' || c=='\t' || c=='\n') idx++; + else break; + } + } + + // returns next token + public Token nextToken() throws LexerException { + Token tok = new Token(); + + skip(); + if (idx>=len) { + tok.str="EOL"; + tok.type=Token.EOL; + } + else + // is it a positive number? + if (Character.isDigit(src.charAt(idx))) { + tok.value = number(); + tok.type = Token.NUM; + tok.str = Double.toString(tok.value); + } + else + if (Character.isLetter(src.charAt(idx))) { + tok.value = 0; + tok.type = Token.ID; + tok.str = identifier(); + if (tok.str.compareTo("let")==0) tok.type = Token.LET; + if (tok.str.compareTo("exit")==0) tok.type = Token.END; + } + else { + switch (src.charAt(idx)) { + case '+': tok.type = Token.ADD; tok.str = "+"; break; + case '-': tok.type = Token.SUB; tok.str = "-"; break; + case '*': tok.type = Token.MUL; tok.str = "*"; break; + case '/': tok.type = Token.DIV; tok.str = "/"; break; + case '(': tok.type = Token.PAL; tok.str = "("; break; + case ')': tok.type = Token.PAR; tok.str = ")"; break; + case '=': tok.type = Token.EQU; tok.str = "="; break; + default : throw new LexerException("Illegal Token: '" + src.charAt(idx) + "'"); + } + idx++; + } + return tok; + } +}
\ No newline at end of file diff --git a/calculator-java/src/main/java/Token.java b/calculator-java/src/main/java/Token.java new file mode 100644 index 0000000..4fe8b23 --- /dev/null +++ b/calculator-java/src/main/java/Token.java @@ -0,0 +1,21 @@ +// Various Tokens for arithmetic expressions based on integers +// with identifiers and assignments + +public class Token { + public int type; // token type + public double value; // numerical value for NUM + public String str; // token string + + public static final int EOL=0; // End Of Line + public static final int PAL=1; // Left Parenthesis + public static final int PAR=2; // Right Parenthesis + public static final int ADD=3; // operators + public static final int SUB=4; + public static final int MUL=5; + public static final int DIV=6; + public static final int NUM=7; // number + public static final int EQU=8; // equal + public static final int LET=9; // let + public static final int ID=10; // identifier + public static final int END=11; // exit +} diff --git a/calculator-java/src/main/java/exceptions/LexerException.java b/calculator-java/src/main/java/exceptions/LexerException.java new file mode 100644 index 0000000..b7b1f24 --- /dev/null +++ b/calculator-java/src/main/java/exceptions/LexerException.java @@ -0,0 +1,5 @@ +package exceptions; + +public class LexerException extends RuntimeException { + public LexerException(String s) { super(s); } +}
\ No newline at end of file |