blob: 512682cd8472a784ce2619c9c4e02ab1441a77d1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package no.eliashaugsbakk.kompilator;
import java.util.Arrays;
import java.util.List;
import no.eliashaugsbakk.kompilator.IO.File;
import no.eliashaugsbakk.kompilator.IO.FileReaderWriter;
import no.eliashaugsbakk.kompilator.IO.FileReaderWriterException;
import no.eliashaugsbakk.kompilator.IRGeneration.IRGenerator;
import no.eliashaugsbakk.kompilator.IRGeneration.Instructions.Instruction;
import no.eliashaugsbakk.kompilator.asmGeneration.AssemblyBuilder;
import no.eliashaugsbakk.kompilator.assembleAndLink.AssemblerAndLinker;
import no.eliashaugsbakk.kompilator.parsing.AST;
import no.eliashaugsbakk.kompilator.parsing.Parser;
import no.eliashaugsbakk.kompilator.parsing.ParserException;
import no.eliashaugsbakk.kompilator.semanticAnalysis.Analyzer;
import no.eliashaugsbakk.kompilator.semanticAnalysis.SemanticException;
import no.eliashaugsbakk.kompilator.tokenization.Lexer;
import no.eliashaugsbakk.kompilator.tokenization.Token;
public class Main {
public static boolean VERBOSE = false;
static final String programFileExtension = "spå";
static void main(String[] args) {
if (args.length == 0) {
IO.println("err: File path must be specified.");
System.exit(1);
}
VERBOSE = Arrays.asList(args).contains("--verbose")
|| Arrays.asList(args).contains("-v");
String inputFileName = args[0];
if (!inputFileName.endsWith("." + programFileExtension)) {
IO.println("err: Input file must use the file extension: " + programFileExtension);
System.exit(1);
}
File inputProgram = null;
FileReaderWriter fileReaderWriter = new FileReaderWriter();
try {
inputProgram = fileReaderWriter.readFile(inputFileName);
} catch (FileReaderWriterException e) {
IO.println("err: Could not read input file: " + inputFileName + "\n\n" + e.getMessage());
System.exit(1);
}
List<Token> tokens = new Lexer(inputProgram.fileBody()).tokenize();
AST ast = null;
try {
ast = new Parser(tokens).parse();
} catch (ParserException e) {
IO.println(e.getMessage());
System.exit(1);
}
try {
new Analyzer(ast).analyze();
} catch (SemanticException e) {
IO.println(e.getMessage());
System.exit(1);
}
List<Instruction> IR = new IRGenerator(ast).generate();
String assembly = new AssemblyBuilder().createAssembly(IR);
new AssemblerAndLinker().assembleAndLink(inputProgram.fileName(), assembly);
System.exit(0);
}
}
|