-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
698 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
build:clean | ||
mkdir .build/ | ||
cd .build/ && find ../src/ -type f -name "*.java" | xargs javac -cp ".:../lib/*" -d . | ||
|
||
clean: | ||
rm -rf .build |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @Author: turk | ||
* @Description: Vhodna točka prevajalnika. | ||
*/ | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
|
||
import cli.PINS; | ||
import cli.PINS.Phase; | ||
import compiler.lexer.Lexer; | ||
|
||
public class Main { | ||
/** | ||
* Metoda, ki izvede celotni proces prevajanja. | ||
* | ||
* @param args parametri ukazne vrstice. | ||
*/ | ||
public static void main(String[] args) throws Exception { | ||
var cli = PINS.parse(args); | ||
run(cli); | ||
} | ||
|
||
|
||
// ------------------------------------------------------------------- | ||
|
||
|
||
private static void run(PINS cli) throws IOException { | ||
var sourceCode = Files.readString(Paths.get(cli.sourceFile)); | ||
run(cli, sourceCode); | ||
} | ||
|
||
private static void run(PINS cli, String sourceCode) { | ||
/** | ||
* Izvedi leksikalno analizo. | ||
*/ | ||
var symbols = new Lexer(sourceCode).scan(); | ||
if (cli.dumpPhases.contains(Phase.LEX)) { | ||
for (var symbol : symbols) { | ||
System.out.println(symbol.toString()); | ||
} | ||
} | ||
if (cli.execPhase == Phase.LEX) { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @Author: turk | ||
* @Description: Implementacija vmesnika `Set`. | ||
*/ | ||
|
||
package cli; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
public class ForwardingSet<T> implements Set<T> { | ||
private Set<T> set; | ||
|
||
public ForwardingSet(Set<T> set) { | ||
this.set = set; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return set.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return set.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean contains(Object o) { | ||
return set.contains(o); | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return set.iterator(); | ||
} | ||
|
||
@Override | ||
public Object[] toArray() { | ||
return set.toArray(); | ||
} | ||
|
||
@Override | ||
public <R> R[] toArray(R[] a) { | ||
return set.toArray(a); | ||
} | ||
|
||
@Override | ||
public boolean add(T e) { | ||
return set.add(e); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object o) { | ||
return set.remove(o); | ||
} | ||
|
||
@Override | ||
public boolean containsAll(Collection<?> c) { | ||
return set.containsAll(c); | ||
} | ||
|
||
@Override | ||
public boolean addAll(Collection<? extends T> c) { | ||
return set.addAll(c); | ||
} | ||
|
||
@Override | ||
public boolean retainAll(Collection<?> c) { | ||
return set.retainAll(c); | ||
} | ||
|
||
@Override | ||
public boolean removeAll(Collection<?> c) { | ||
return set.removeAll(c); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
set.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/** | ||
* @Author: turk | ||
* @Description: Uporabniški vmesnik (CLI). | ||
*/ | ||
|
||
package cli; | ||
|
||
import java.util.EnumSet; | ||
|
||
import ArgPar.Annotation.ParsableArgument; | ||
import ArgPar.Annotation.ParsableCommand; | ||
import ArgPar.Annotation.ParsableOption; | ||
import ArgPar.Exception.ParseException; | ||
import ArgPar.Parser.ArgumentParser; | ||
|
||
@ParsableCommand(commandName = "PINS", description = "Prevajalnik za programski jezik PINS") | ||
public class PINS { | ||
/** | ||
* Pot do izvorne datoteke. | ||
*/ | ||
@ParsableArgument | ||
public String sourceFile; | ||
|
||
/** | ||
* Faze prevajanja, ki izpišejo vmesne rezultate. | ||
*/ | ||
@ParsableOption(name = "--dump") | ||
public PhasesEnumSet dumpPhases = PhasesEnumSet.empty(); | ||
|
||
/** | ||
* Faza, ki se bo izvedla nazadnje. | ||
*/ | ||
@ParsableOption(name = "--exec") | ||
public Phase execPhase = Phase.LEX; | ||
|
||
@ParsableOption(name = "--memory") | ||
public int memory = 1024; | ||
|
||
/** | ||
* Razčleni argumente. | ||
*/ | ||
public static PINS parse(String[] args) { | ||
try { | ||
var parser = new ArgumentParser<PINS>(PINS.class); | ||
return parser.parse(args); | ||
} catch (ParseException __) { | ||
System.exit(2); | ||
return null; | ||
} | ||
} | ||
|
||
// -------------------------------------------------------------- | ||
|
||
/** | ||
* Faze prevajanja. | ||
*/ | ||
public static enum Phase { | ||
LEX, SYN, AST, NAME, TYP, FRM, IMC, INT | ||
} | ||
|
||
/** | ||
* Razred, ki hrani faze prevajanja. | ||
*/ | ||
public static class PhasesEnumSet extends ForwardingSet<Phase> { | ||
PhasesEnumSet(EnumSet<Phase> set) { | ||
super(set); | ||
} | ||
|
||
/** | ||
* Ustvari prazno možico. | ||
*/ | ||
static PhasesEnumSet empty() { | ||
return new PhasesEnumSet(EnumSet.noneOf(Phase.class)); | ||
} | ||
|
||
/** | ||
* Razčleni argument in kreira novo množico. | ||
* | ||
* @param arg Argument, ki ga metoda razčleni v množico faz. | ||
*/ | ||
public static PhasesEnumSet valueOf(String arg) { | ||
var split = arg.split(","); | ||
var set = new PhasesEnumSet(EnumSet.noneOf(Phase.class)); | ||
for (var s : split) { | ||
var phase = Phase.valueOf(s.trim()); | ||
if (phase == null) { | ||
throw new IllegalArgumentException("Could not parse <Phase>!"); | ||
} | ||
set.add(phase); | ||
} | ||
return set; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (isEmpty()) { | ||
return "{}"; | ||
} | ||
var sb = new StringBuilder(); | ||
sb.append("{"); | ||
for (var phase : Phase.values()) { | ||
if (contains(phase)) { | ||
sb.append(phase.toString() + ","); | ||
} | ||
} | ||
sb.deleteCharAt(sb.length() - 1); | ||
sb.append("}"); | ||
return sb.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package common; | ||
|
||
public class Constants { | ||
private Constants() {} | ||
|
||
public static final int WordSize; | ||
public static final int x86 = 4; // 4B | ||
public static final int x64 = 8; // 8B | ||
|
||
// 'Standardna knjižnica' | ||
public static final String printStringLabel = "print_str"; | ||
public static final String printIntLabel = "print_int"; | ||
public static final String printLogLabel = "print_log"; | ||
public static final String randIntLabel = "rand_int"; | ||
public static final String seedLabel = "seed"; | ||
|
||
static { | ||
/** | ||
* Ciljna arhitektura je x86. | ||
*/ | ||
WordSize = x86; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @Author: turk | ||
* @Description: Razred, namenjen obveščanju o poteku prevajanja. | ||
*/ | ||
|
||
package common; | ||
|
||
import java.io.PrintStream; | ||
|
||
import compiler.lexer.Position; | ||
|
||
/** | ||
* Obveščanje o napakah. | ||
*/ | ||
public class Report { | ||
/** | ||
* NE SPREMINJAJ! | ||
*/ | ||
private static final int exitErrorCode = 99; | ||
|
||
/** | ||
* Izhodni tok, kamor se izpišejo napake. | ||
*/ | ||
public static PrintStream err = System.err; | ||
|
||
private Report() {} | ||
|
||
public static void error(String message) { | ||
err.println(message); | ||
System.exit(exitErrorCode); | ||
} | ||
|
||
public static void error(Position position, String message) { | ||
err.println(position.toString() + ": " + message); | ||
System.exit(exitErrorCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @ Author: turk | ||
* @ Description: | ||
*/ | ||
|
||
package common; | ||
|
||
import java.util.Objects; | ||
|
||
public class RequireNonNull { | ||
public static void requireNonNull(Object... objects) { | ||
for (var obj : objects) { | ||
Objects.requireNonNull(obj); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @ Author: turk | ||
* @ Description: | ||
*/ | ||
|
||
package common; | ||
|
||
import static common.RequireNonNull.requireNonNull; | ||
|
||
public class StringUtil { | ||
/** | ||
* Kreira nov indentiran niz. | ||
*/ | ||
public static String indented(String str, int indent) { | ||
requireNonNull(str); | ||
if (indent < 0) { throw new IllegalArgumentException("Indent must be at least 0!"); } | ||
var sb = new StringBuilder(indent); | ||
for (int i = 0; i < indent; i++) { | ||
sb.append(" "); | ||
} | ||
sb.append(str); | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @ Author: turk | ||
* @ Description: | ||
*/ | ||
|
||
package common; | ||
|
||
public interface VoidOperator { | ||
void apply(); | ||
} |
Oops, something went wrong.