Skip to content

Commit

Permalink
Merge pull request #30 from merkrafter/development
Browse files Browse the repository at this point in the history
0.2.0 pre-release
  • Loading branch information
merkrafter authored Jan 7, 2020
2 parents a0d012c + d7907ce commit 13ae8c9
Show file tree
Hide file tree
Showing 21 changed files with 2,474 additions and 168 deletions.
35 changes: 0 additions & 35 deletions merkompiler.iml

This file was deleted.

11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>Merkompiler</groupId>
<artifactId>Merkompiler</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -32,9 +32,14 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.2</version>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.6.2</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
Expand Down Expand Up @@ -78,4 +83,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
18 changes: 14 additions & 4 deletions src/main/java/com/merkrafter/Merkompiler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.merkrafter;

import com.merkrafter.config.CompilerStage;
import com.merkrafter.config.Config;
import com.merkrafter.config.ErrorCode;
import com.merkrafter.lexing.Scanner;
import com.merkrafter.lexing.TokenType;
import com.merkrafter.parsing.Parser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;

import java.io.File;
Expand Down Expand Up @@ -58,9 +60,17 @@ static void run(final Config config) throws FileNotFoundException {
out = new PrintStream(config.getOutputFile());
}

do {
scanner.processToken();
out.println(scanner.getSym());
} while (scanner.getSym().getType() != TokenType.EOF);
if (config.getStage() == CompilerStage.SCANNING) {
// only print the tokens if the processing should stop after scanning
do {
scanner.processToken();
out.println(scanner.getSym());
} while (scanner.getSym().getType() != TokenType.EOF);
} else if (config.getStage() == CompilerStage.PARSING) {
final Parser parser = new Parser(scanner);
if (!parser.parse()) {
System.err.println("Parsing error!");
}
}
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/merkrafter/config/CompilerStage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.merkrafter.config;

/****
* This enum represents the steps the compiler goes through in order to convert a JavaSST source
* code file into actual byte code.
*
* @version v0.2.0
* @author merkrafter
***************************************************************/
public enum CompilerStage {
// CONSTANTS
//==============================================================
/**
* Only scan the input and output tokens.
*/
SCANNING,
/**
* Scan and parse the input and output whether this was successful.
*/
PARSING;


/**
* @return the lowercase name of this enum item
*/
@Override
public String toString() {
return name().toLowerCase();
}

/**
* Returns the latest stage this enum currently offers in terms of processing data.
*
* @return the latest available compiler stage
*/
public static CompilerStage latest() {
return PARSING;
}
}
72 changes: 61 additions & 11 deletions src/main/java/com/merkrafter/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
* This class holds configuration data for this program.
Expand All @@ -18,10 +25,14 @@ public class Config {

private final boolean verbose;

private Config(final String inputFile, final String outputFile, boolean verbose) {
private final CompilerStage stage;

private Config(final String inputFile, final String outputFile, boolean verbose,
final CompilerStage stage) {
this.inputFile = inputFile;
this.outputFile = outputFile;
this.verbose = verbose;
this.stage = stage;
}

public String getInputFile() {
Expand All @@ -36,22 +47,44 @@ public boolean isVerbose() {
return verbose;
}

public CompilerStage getStage() {
return stage;
}

public static Config fromArgs(final String args) throws ArgumentParserException {
return fromArgs(fromString(args));
}

public static Config fromArgs(final String[] args) throws ArgumentParserException {
// define the parser
final ArgumentParser parser =
ArgumentParsers.newFor("Merkompiler").build().defaultHelp(true)
.description("Compiles JavaSST files");

parser.addArgument("INPUT").required(true).type(String.class)
final ArgumentParser parser = ArgumentParsers.newFor("Merkompiler")
.build()
.defaultHelp(true)
.description("Compiles JavaSST files");
try {
parser.version("${prog} " + getVersion());
} catch (XmlPullParserException | IOException ignored) {
parser.version("No version information available.");
}
parser.addArgument("INPUT")
.required(true)
.type(String.class)
.help("JavaSST source code file");
parser.addArgument("-v", "--verbose").action(Arguments.storeTrue())
parser.addArgument("-v", "--verbose")
.action(Arguments.storeTrue())
.help("print more information (absolute paths instead of simple file names in error messages, for instance");
parser.addArgument("-o", "--output").type(String.class).metavar("OUTPUT")
parser.addArgument("-V", "--version")
.action(Arguments.version())
.help("print version information and exit");
parser.addArgument("-o", "--output")
.type(String.class)
.metavar("OUTPUT")
.help("output target; default is stdout");
parser.addArgument("--skip-after")
.type(Arguments.caseInsensitiveEnumType(CompilerStage.class))
.dest("compilerStage")
.setDefault(CompilerStage.latest())
.help("only process the input file up to the given stage (including)");


// parse the arguments
Expand All @@ -62,13 +95,16 @@ public static Config fromArgs(final String[] args) throws ArgumentParserExceptio
String inputFileName = null;
String outputFileName = null;
boolean verbose = false;
CompilerStage stage = CompilerStage.latest();

if (namespace != null) {
inputFileName = namespace.getString("INPUT");
outputFileName = namespace.getString("output");
verbose = namespace.getBoolean("verbose");
stage = namespace.get("compilerStage");
}
return new Config(inputFileName, outputFileName, verbose);

return new Config(inputFileName, outputFileName, verbose, stage);
}

/**
Expand All @@ -86,7 +122,21 @@ public static String[] fromString(final String argsAsString) {
*/
@Override
public String toString() {
return String
.format("Config(INPUT=%s, OUTPUT=%s, verbose=%b)", inputFile, outputFile, verbose);
return String.format("Config(INPUT=%s, OUTPUT=%s, verbose=%b, stage=%s)",
inputFile,
outputFile,
verbose,
stage);
}

/**
* Retrieve version information from the pom.xml file.
*
* @return a String containing the software version
*/
private static String getVersion() throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
return model.getVersion();
}
}
69 changes: 69 additions & 0 deletions src/main/java/com/merkrafter/lexing/IdentToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.merkrafter.lexing;

/****
* This class serves as a token and stores the identifier found.
*
* @version v0.2.0
* @author merkrafter
***************************************************************/
public class IdentToken extends Token {
// ATTRIBUTES
//==============================================================
/**
* the identifier this token stands for
*/
private final String ident;

// CONSTRUCTORS
//==============================================================

/****
* Creates a new IdentToken from an identifier and position data.
***************************************************************/
public IdentToken(final String ident, final String filename, final long line,
final int position) {
super(TokenType.IDENT, filename, line, position);
this.ident = ident;
}

// GETTER
//==============================================================

/**
* @return the identifier this token stands for
*/
String getIdent() {
return ident;
}

// METHODS
//==============================================================
// public methods
//--------------------------------------------------------------

/**
* Two IdentTokens are equal if both have the type IdentToken and their identifiers, line
* numbers, positions and filenames are equal.
*
* @param obj ideally a IdentToken to compare this with
* @return whether this is equal to obj
*/
@Override
public boolean equals(final Object obj) {
if (!super.equals(obj)) {
return false;
}
return obj instanceof IdentToken && ((IdentToken) obj).ident.equals(ident);
}

/**
* Creates a String representation of this IdentToken in the following format:
* FILENAME(LINE,POSITION): TYPE(IDENT)
*
* @return a String representation of this IdentToken
*/
@Override
public String toString() {
return super.toString() + String.format("(%s)", ident);
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/merkrafter/lexing/Keyword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.merkrafter.lexing;

/****
* This enum lists all keywords that can be encountered in JavaSST files.
*
* @since v0.2.0
* @author merkrafter
***************************************************************/
public enum Keyword {
CLASS,
ELSE,
FINAL,
IF,
INT,
PUBLIC,
RETURN,
VOID,
WHILE,
}
Loading

0 comments on commit 13ae8c9

Please sign in to comment.