diff --git a/.gitattributes b/.gitattributes index 82bdecf..af7ea12 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,7 +1,7 @@ # GitHub Linguist Coloring *.dm linguist-language=Darkmatter *.asm linguist-language=Assembly -*.ll linguist-language=Assembly +*.ll linguist-language=LLVM *.java linguist-language=Java *.lua linguist-language=Lua *.json linguist-language=JSON diff --git a/.gitignore b/.gitignore index 2201e48..f173b5b 100644 --- a/.gitignore +++ b/.gitignore @@ -164,5 +164,4 @@ Thumbs.db output/ *.bin *.o -*.ll /bin/ diff --git a/README.md b/README.md index f8a09fe..773b35f 100644 --- a/README.md +++ b/README.md @@ -52,31 +52,26 @@ This is the Darkmatter compiler project, its goal is to output LLVM-IR assembly ### Compiler Parts [Compiler Parts]: #compiler-parts -Darkmatter's compiler will be segmented into 3-parts. +Darkmatter's compiler is segmented into 2-parts. -- [ ] Compiler Front-End (lexer, parser, linter, AST) +- Front-End (lexer, parser, linter, AST) - [ ] Currently under discussion to be written in Rust or Java - [ ] Syntax Definitions & Structure - [ ] Parse compiler flags and arguments - [ ] Input Darkmatter (`.dm`) - [ ] Lexer / Parser / Tokenizer - [ ] AST - - [ ] LLVM primitives/library for Java/Rust - - [ ] 1:1 AST mappings to LLVM codegen() functions. + - [ ] LLVM codegen() for AST + - [ ] Optimize produced LLVM-IR (depending on optimization-level) - [ ] Output LLVM-IR (`.ll`) -- [ ] Compiler Middleware (analyze and optimize LLVM-IR) - - [ ] *TBA* - - [ ] Input LLVM-IR (`.ll`) - - [ ] Output LLVM-IR (`.ll`) -- [ ] Compiler Back-End (assemble IR) - Input IR / Output Native Binary or JVM byte-code. +- Back-End (assemble IR) - Input IR / Output Native Binary or JVM byte-code - [ ] *TBA* - [ ] Input LLVM-IR (`.ll`) + - [ ] Linker - https://lld.llvm.org/ - [ ] Output Native Binary Static Library (`.a`, `.lib`) - [ ] Output Native Binary Dynamic Library (`.so`, `.dll`) - [ ] Output Native Binary Executable (`.exec`, `.exe`) - [ ] Output JVM byte-code (`.class`) -- [ ] Darkmatter Standard Library - - [ ] *TBA* ### Syntax [Syntax]: #syntax @@ -94,10 +89,83 @@ The Darkmatter grammer and syntax requirements. ### Compiler [Compiler]: #compiler +```mermaid +flowchart LR; + + +%% Back End %%; + +H(LLVM-IR):::blue --> |*.ll| I(" + Optimization +"); + +subgraph Darkmatter Back-end; +I --> J(" + LLVM-AS + linker/assembler +"); +end; + +J--> |"*.{a,so,lib,dll,dylib}"| K[" + - Library - + ELF,PE,MACH-O + amd64,i386,arm64,armhf,mips +"]:::green; +J--> |"*.{,exe,exec}"| L[" + - Executable - + ELF,PE,MACH-O + amd64,i386,arm64,armhf,mips +"]:::red; + + + + + +%% Front End %%; + +A(Source Code):::cyan --> |*.dm| B(" + Tokenizer + lexical analysis +"):::yellow; + +subgraph Darkmatter Front-end; + B --> |Tokens| C(" + Parser + syntactic analysis + "); + C --> D(" + AST + "); + D --> E(" + Generator + "); +end; + +E --> G(" + LLVM-IR +"):::blue; +E --> F[" + JVM byte-code +"]:::orange; + + + + + +%% Styling %%; + +classDef red stroke:#FF0000; +classDef green stroke:#00FF00; +classDef blue stroke:#0000FF; +classDef yellow stroke:#FFFF00; +classDef orange stroke:#FFAA00; +classDef cyan stroke:#00FFFF; +``` + ##### Currently considering RAII (Resource Acquisition Is Initialization/Scope-Bound Resource Management) for memory management. -- [ ] Output JVM byte-code + - [ ] Output LLVM-IR assembly - [ ] Compile LLVM-IR to platform/architecture binary (supported ABI/ISAs): - [ ] Linux/amd64 (Intel/AMD 64-bit) @@ -108,12 +176,44 @@ The Darkmatter grammer and syntax requirements. - [ ] MacOS/aarch64 (RISC ARMv8 64-bit) - [ ] Windows/x86 (Intel/AMD 32-bit) - [ ] Windows/amd64 (RISC ARMv8 64-bit) -- [ ] Compiler flags/options/args - - [ ] Compiler can handle a variable # of src files (`ARGS`) - - [ ] Set output file (`-o`) - - [ ] Set threads (`-t`) - - [ ] Set log-level (`-l`) - - [ ] REPL/Interpreter mode (`-I`) +- [ ] Output JVM byte-code +- [ ] `dmc` - Compiler flags/options/args (`ARGS*` are the input Darkmatter source-files to compile) + - `-l`, `--log-level` - Specify the logger level (`TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`) (Default: `INFO`) + - `-j`, `--threads` - Specify threads for compilation/transpilation + - [ ] `compile` - Compile Darkmatter directly to a native binary (library/executable) + - `-o`, `--output` - Output destination of the library/executable + - `-t`, `--target` - Specify the target platform/architecture + - `-k`, `--library` - Specify the library type (`static`/`dynamic`) (Default: `static`) + - `-A`, `--emit` - Only emit LLVM-IR assembly, do not assemble. + - `-O`, `--optimize` - Optimization level (`0`=no optimization, `2`=aggressive optimization) (Default: `0`) + - [ ] `generate` - Compile Darkmatter to JVM byte-code + - `-o`, `--output` - Output file destination + - `-b`, `--byte-code` - The byte-code language to produce (`jvm`) (Default: `jvm`) + +Examples: +```sh +## AoT Examples + +# Compile `count_to_ten.dm` to a native executable binary named `count` +dmc compile --output count count_to_ten.dm +dmc compile -o count count_to_ten.dm + +# Compile `count_to_ten.dm` to a native `linux/amd64` executable binary (ELF) +dmc compile --output count --target linux/amd64 count_to_ten.dm +dmc compile -o count -t linux/amd64 count_to_ten.dm + + +## JIT Examples + +# Compile `my_class.dm` to a JVM byte-code class file +dmc generate --output MyClass.class --byte-code jvm my_class.dm +dmc generate -o MyClass.class my_class.dm + +# Compile `a.dm` and `b.dm` to a JVM byte-code jar file +dmc generate --output MyApp.jar --byte-code jvm a.dm b.dm +dmc generate -o MyApp.jar a.dm b.dm +``` + ### Standard Library [Standard Library]: #standard-library diff --git a/src/main/java/me/anthonyw/darkmatter/Keywords.java b/src/main/java/me/anthonyw/darkmatter/Keywords.java index ed9ecaa..90e5203 100644 --- a/src/main/java/me/anthonyw/darkmatter/Keywords.java +++ b/src/main/java/me/anthonyw/darkmatter/Keywords.java @@ -107,7 +107,9 @@ public enum Keywords { - + /** + * A compile-time assigned data-type variable (constant/immutable). + */ VAR(KeywordType.DATA_TYPE); private final KeywordType type; diff --git a/src/main/java/me/anthonyw/darkmatter/TokenType.java b/src/main/java/me/anthonyw/darkmatter/TokenType.java index 521e1dc..1f1478f 100644 --- a/src/main/java/me/anthonyw/darkmatter/TokenType.java +++ b/src/main/java/me/anthonyw/darkmatter/TokenType.java @@ -16,7 +16,7 @@ public enum TokenType { PERCENT("%"), - CIRCUMFLEX_ACCENT("^"), + CIRCUMFLEX("^"), AMPERSAND("&"), @@ -83,13 +83,22 @@ public enum TokenType { LITERAL_INTEGER, + LITERAL_FLOAT, + + LITERAL_DOUBLE, + + LITERAL_LONG, + LITERAL_CHARACTER, LITERAL_STRING, LITERAL_BOOLEAN, - LET("let"), + /** + * A compile-time assigned data-type variable (constant/immutable). + */ + VAR("var"), FUNCTION("function"), @@ -124,14 +133,27 @@ public enum TokenType { // Module/Container types + // Structs + STRUCT("struct"), + IMPLEMENTATION("implementation"), + + // Classes (OOP) + CLASS("class"), + /** + * Like Java 'extends' + */ + INHERITS("inherits"), + ENUM("enum"), INTERFACE("interface"), + ABSTRACT("abstract"), + STATEMENT, IF("if"), diff --git a/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java b/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java index 9c37e4a..34408b9 100644 --- a/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java +++ b/src/main/java/me/anthonyw/darkmatter/cli/AnsiColor.java @@ -1,133 +1,80 @@ package me.anthonyw.darkmatter.cli; -import java.util.regex.Pattern; +public class AnsiColor { + + // Prevent instantiation + private AnsiColor() {} + + // Reset + public static final String RESET = "\033[0m"; // Text Reset -/** * All supported color values for chat */ -public enum AnsiColor { - /** * Represents black */ - BLACK('0', 0x00), - /** * Represents dark blue */ - DARK_BLUE('1', 0x1), - /** * Represents dark green */ - DARK_GREEN('2', 0x2), - /** * Represents dark blue (aqua) */ - DARK_AQUA('3', 0x3), - /** * Represents dark red */ - DARK_RED('4', 0x4), - /** * Represents dark purple */ - PURPLE('5', 0x5), - /** * Represents gold */ - GOLD('6', 0x6), - /** * Represents gray */ - GRAY('7', 0x7), - /** * Represents dark gray */ - DARK_GRAY('8', 0x8), - /** * Represents blue */ - BLUE('9', 0x9), - /** * Represents green */ - GREEN('a', 0xA), - /** * Represents aqua */ - AQUA('b', 0xB), - /** * Represents red */ - RED('c', 0xC), - /** * Represents light purple */ - PINK('d', 0xD), - /** * Represents yellow */ - YELLOW('e', 0xE), - /** * Represents white */ - WHITE('f', 0xF), - /** * Represents magical characters that change around randomly */ - MAGIC('k', 0x10, true), - /** * Makes the text bold. */ - BOLD('l', 0x11, true), - /** * Makes a line appear through the text. */ - STRIKETHROUGH('m', 0x12, true), - /** * Makes the text appear underlined. */ - UNDERLINE('n', 0x13, true), - /** * Makes the text italic. */ - ITALIC('o', 0x14, true), - /** * Resets all previous chat colors or formats. */ - RESET('r', 0x15); - /** - * * The special character which prefixes all chat colour codes. Use this if * - * you need to dynamically convert colour codes from your custom format. - */ - public static final char COLOR_CHAR = '\u00A7'; - private static final Pattern STRIP_COLOR_PATTERN = Pattern - .compile("(?i)" + String.valueOf(COLOR_CHAR) + "[0-9A-FK-OR]"); - private final int intCode; - private final char code; - private final boolean isFormat; - private final String toString; + // Regular Colors + public static final String BLACK = "\033[0;30m"; // BLACK + public static final String RED = "\033[0;31m"; // RED + public static final String GREEN = "\033[0;32m"; // GREEN + public static final String YELLOW = "\033[0;33m"; // YELLOW + public static final String BLUE = "\033[0;34m"; // BLUE + public static final String PURPLE = "\033[0;35m"; // PURPLE + public static final String CYAN = "\033[0;36m"; // CYAN + public static final String WHITE = "\033[0;37m"; // WHITE - private AnsiColor(char code, int intCode) { - this(code, intCode, false); - } + // Bold + public static final String BLACK_BOLD = "\033[1;30m"; // BLACK + public static final String RED_BOLD = "\033[1;31m"; // RED + public static final String GREEN_BOLD = "\033[1;32m"; // GREEN + public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW + public static final String BLUE_BOLD = "\033[1;34m"; // BLUE + public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE + public static final String CYAN_BOLD = "\033[1;36m"; // CYAN + public static final String WHITE_BOLD = "\033[1;37m"; // WHITE - private AnsiColor(char code, int intCode, boolean isFormat) { - this.code = code; - this.intCode = intCode; - this.isFormat = isFormat; - this.toString = new String(new char[] { COLOR_CHAR, code }); - } + // Underline + public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK + public static final String RED_UNDERLINED = "\033[4;31m"; // RED + public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN + public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW + public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE + public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE + public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN + public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE - /** - * * Gets the char value associated with this color * * @return A char value of - * this color code - */ - public char getChar() { - return code; - } + // Background + public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK + public static final String RED_BACKGROUND = "\033[41m"; // RED + public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN + public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW + public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE + public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE + public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN + public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE - @Override - public String toString() { - return toString; - } + // High Intensity + public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK + public static final String RED_BRIGHT = "\033[0;91m"; // RED + public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN + public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW + public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE + public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE + public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN + public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE - /** * Checks if this code is a format code as opposed to a color code. */ - public boolean isFormat() { - return isFormat; - } + // Bold High Intensity + public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK + public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED + public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN + public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW + public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE + public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE + public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN + public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE - /** * Checks if this code is a color code as opposed to a format code. */ - public boolean isColor() { - return !isFormat && this != RESET; - } - - /** * Get intCode * @return */ - public int getCode() { - return intCode; - } - - /** - * * Strips the given message of all color codes * * @param input String to - * strip of color * @return A copy of the input string, without any coloring - */ - public static String stripColor(final String input) { - if (input == null) { - return null; - } - return STRIP_COLOR_PATTERN.matcher(input).replaceAll(""); - } - - /** - * Translates a string using an alternate color code character into a string - * that uses the internal ChatColor.COLOR_CODE color code character. The - * alternate color code character will only be replaced if it is immediately - * followed by 0-9, A-F, a-f, K-O, k-o, R or r. - * - * @param altColorChar The alternate color code character to replace. Ex: & - * @param textToTranslate Text containing the alternate color code character. - * @return Text containing the ChatColor.COLOR_CODE color code character. - */ - public static String translateAlternateColorCodes(char altColorChar, String textToTranslate) { - char[] b = textToTranslate.toCharArray(); - for (int i = 0; i < b.length - 1; i++) { - if (b[i] == altColorChar && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(b[i + 1]) > -1) { - b[i] = AnsiColor.COLOR_CHAR; - b[i + 1] = Character.toLowerCase(b[i + 1]); - } - } - return new String(b); - } -} + // High Intensity backgrounds + public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK + public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED + public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN + public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW + public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE + public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE + public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN + public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE +} \ No newline at end of file diff --git a/src/main/java/me/anthonyw/darkmatter/cli/ConsoleColors.java b/src/main/java/me/anthonyw/darkmatter/cli/ConsoleColors.java deleted file mode 100644 index 121db06..0000000 --- a/src/main/java/me/anthonyw/darkmatter/cli/ConsoleColors.java +++ /dev/null @@ -1,80 +0,0 @@ -package me.anthonyw.darkmatter.cli; - -public class ConsoleColors { - - // Prevent instantiation - private ConsoleColors() {} - - // Reset - public static final String RESET = "\033[0m"; // Text Reset - - // Regular Colors - public static final String BLACK = "\033[0;30m"; // BLACK - public static final String RED = "\033[0;31m"; // RED - public static final String GREEN = "\033[0;32m"; // GREEN - public static final String YELLOW = "\033[0;33m"; // YELLOW - public static final String BLUE = "\033[0;34m"; // BLUE - public static final String PURPLE = "\033[0;35m"; // PURPLE - public static final String CYAN = "\033[0;36m"; // CYAN - public static final String WHITE = "\033[0;37m"; // WHITE - - // Bold - public static final String BLACK_BOLD = "\033[1;30m"; // BLACK - public static final String RED_BOLD = "\033[1;31m"; // RED - public static final String GREEN_BOLD = "\033[1;32m"; // GREEN - public static final String YELLOW_BOLD = "\033[1;33m"; // YELLOW - public static final String BLUE_BOLD = "\033[1;34m"; // BLUE - public static final String PURPLE_BOLD = "\033[1;35m"; // PURPLE - public static final String CYAN_BOLD = "\033[1;36m"; // CYAN - public static final String WHITE_BOLD = "\033[1;37m"; // WHITE - - // Underline - public static final String BLACK_UNDERLINED = "\033[4;30m"; // BLACK - public static final String RED_UNDERLINED = "\033[4;31m"; // RED - public static final String GREEN_UNDERLINED = "\033[4;32m"; // GREEN - public static final String YELLOW_UNDERLINED = "\033[4;33m"; // YELLOW - public static final String BLUE_UNDERLINED = "\033[4;34m"; // BLUE - public static final String PURPLE_UNDERLINED = "\033[4;35m"; // PURPLE - public static final String CYAN_UNDERLINED = "\033[4;36m"; // CYAN - public static final String WHITE_UNDERLINED = "\033[4;37m"; // WHITE - - // Background - public static final String BLACK_BACKGROUND = "\033[40m"; // BLACK - public static final String RED_BACKGROUND = "\033[41m"; // RED - public static final String GREEN_BACKGROUND = "\033[42m"; // GREEN - public static final String YELLOW_BACKGROUND = "\033[43m"; // YELLOW - public static final String BLUE_BACKGROUND = "\033[44m"; // BLUE - public static final String PURPLE_BACKGROUND = "\033[45m"; // PURPLE - public static final String CYAN_BACKGROUND = "\033[46m"; // CYAN - public static final String WHITE_BACKGROUND = "\033[47m"; // WHITE - - // High Intensity - public static final String BLACK_BRIGHT = "\033[0;90m"; // BLACK - public static final String RED_BRIGHT = "\033[0;91m"; // RED - public static final String GREEN_BRIGHT = "\033[0;92m"; // GREEN - public static final String YELLOW_BRIGHT = "\033[0;93m"; // YELLOW - public static final String BLUE_BRIGHT = "\033[0;94m"; // BLUE - public static final String PURPLE_BRIGHT = "\033[0;95m"; // PURPLE - public static final String CYAN_BRIGHT = "\033[0;96m"; // CYAN - public static final String WHITE_BRIGHT = "\033[0;97m"; // WHITE - - // Bold High Intensity - public static final String BLACK_BOLD_BRIGHT = "\033[1;90m"; // BLACK - public static final String RED_BOLD_BRIGHT = "\033[1;91m"; // RED - public static final String GREEN_BOLD_BRIGHT = "\033[1;92m"; // GREEN - public static final String YELLOW_BOLD_BRIGHT = "\033[1;93m";// YELLOW - public static final String BLUE_BOLD_BRIGHT = "\033[1;94m"; // BLUE - public static final String PURPLE_BOLD_BRIGHT = "\033[1;95m";// PURPLE - public static final String CYAN_BOLD_BRIGHT = "\033[1;96m"; // CYAN - public static final String WHITE_BOLD_BRIGHT = "\033[1;97m"; // WHITE - - // High Intensity backgrounds - public static final String BLACK_BACKGROUND_BRIGHT = "\033[0;100m";// BLACK - public static final String RED_BACKGROUND_BRIGHT = "\033[0;101m";// RED - public static final String GREEN_BACKGROUND_BRIGHT = "\033[0;102m";// GREEN - public static final String YELLOW_BACKGROUND_BRIGHT = "\033[0;103m";// YELLOW - public static final String BLUE_BACKGROUND_BRIGHT = "\033[0;104m";// BLUE - public static final String PURPLE_BACKGROUND_BRIGHT = "\033[0;105m"; // PURPLE - public static final String CYAN_BACKGROUND_BRIGHT = "\033[0;106m"; // CYAN - public static final String WHITE_BACKGROUND_BRIGHT = "\033[0;107m"; // WHITE -} \ No newline at end of file diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/Architecture.java b/src/main/java/me/anthonyw/darkmatter/compiler/Architecture.java new file mode 100644 index 0000000..f4b58b0 --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/Architecture.java @@ -0,0 +1,43 @@ +package me.anthonyw.darkmatter.compiler; + +public enum Architecture { + + // CISC + + /** + * Modern 64-bit desktop processors. + * + * Also known as "x86_64". + */ + AMD64("amd64"), + + /** + * Older generation 32-bit desktop processors. + */ + I386("i386"), + + // RISC + + /** + * Modern 64-bit embedded systems. + */ + ARM64("aarch64"), + + /** + * Older generation embedded systems. + * + * Also known as "armv7h". + */ + ARM32("armhf"); + + private final String identifier; + + private Architecture(final String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return identifier; + } + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/Compilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/Compilable.java new file mode 100644 index 0000000..565f62d --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/Compilable.java @@ -0,0 +1,5 @@ +package me.anthonyw.darkmatter.compiler; + +public interface Compilable { + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java b/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java index feea2cc..6e8a8db 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/CompilerSeverity.java @@ -1,25 +1,32 @@ package me.anthonyw.darkmatter.compiler; +import me.anthonyw.darkmatter.cli.AnsiColor; + public enum CompilerSeverity { /** - * Debugging messages. - */ - DEBUG, - - /** - * Informational messages. + * Helpful suggestions. */ - INFO, + NOTE(AnsiColor.CYAN_BRIGHT), /** * Warning messages, including basic optimization checks. */ - WARN, + WARN(AnsiColor.YELLOW_BRIGHT), /** * Error messages, requires review, compilation halted. */ - ERROR; + ERROR(AnsiColor.RED_BRIGHT); + + private final String cliColor; + + private CompilerSeverity(final String redBright) { + this.cliColor = redBright; + } + + public String getAnsiColor() { + return cliColor; + } } diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/JvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/JvmCompilable.java deleted file mode 100644 index 7e10bfc..0000000 --- a/src/main/java/me/anthonyw/darkmatter/compiler/JvmCompilable.java +++ /dev/null @@ -1,11 +0,0 @@ -package me.anthonyw.darkmatter.compiler; - -public abstract class JvmCompilable { - - /** - * This will generate JVM byte-code - * @return - */ - public abstract String jvmCodeGen(); - -} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/LlvmCompilable.java deleted file mode 100644 index 29d811a..0000000 --- a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmCompilable.java +++ /dev/null @@ -1,7 +0,0 @@ -package me.anthonyw.darkmatter.compiler; - -public abstract class LlvmCompilable { - - public abstract String llvmCodeGen(); - -} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/Platform.java b/src/main/java/me/anthonyw/darkmatter/compiler/Platform.java new file mode 100644 index 0000000..30d3de6 --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/Platform.java @@ -0,0 +1,55 @@ +package me.anthonyw.darkmatter.compiler; + +public enum Platform { + + /** + * Universal OS + * + * https://github.com/universalos-io + */ + UNIVERSAL_OS("universalos"), + + /** + * GNU/Linux + * + * https://www.gnu.org/gnu/linux-and-gnu.en.html + * https://kernel.org/ + * https://en.wikipedia.org/wiki/Linux + */ + LINUX("linux"), + + /** + * Apple MacOS (XNU/Darwin) + * + * https://www.apple.com/macos/ + * https://en.wikipedia.org/wiki/MacOS + */ + MAC_OS("darwin"), + + /** + * Microsoft Windows + * + * https://www.microsoft.com/en-us/windows/ + * https://en.wikipedia.org/wiki/Microsoft_Windows + */ + WINDOWS("windows"), + + /** + * FreeBSD + * + * https://www.freebsd.org/ + * https://en.wikipedia.org/wiki/FreeBSD + */ + FREEBSD("freebsd"); + + private final String identifier; + + private Platform(final String identifier) { + this.identifier = identifier; + } + + public String getIdentifier() { + return identifier; + } + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmCompilable.java new file mode 100644 index 0000000..5f41b1a --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmCompilable.java @@ -0,0 +1,13 @@ +package me.anthonyw.darkmatter.compiler.jvm; + +import me.anthonyw.darkmatter.compiler.Compilable; + +public abstract class JvmCompilable implements Compilable { + + /** + * This will generate JVM byte-code. + * @return + */ + public abstract String jvmCodeGen(); + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java index c2e5bbd..702afec 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/jvm/JvmOpCode.java @@ -208,18 +208,18 @@ public enum JvmOpCode { IMPDEP2(0xFF, "impdep2"); private final int opcode; - private final String name; + private final String mnemonic; - JvmOpCode(int opcode, String name) { + private JvmOpCode(int opcode, String mnemonic) { this.opcode = opcode; - this.name = name; + this.mnemonic = mnemonic; } - public int getOpcode() { + public int getOpCode() { return opcode; } - public String getName() { - return name; + public String getMnemonic() { + return mnemonic; } } diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmAssembly.java b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmAssembly.java similarity index 76% rename from src/main/java/me/anthonyw/darkmatter/compiler/LlvmAssembly.java rename to src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmAssembly.java index 6f7d13a..a485964 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/LlvmAssembly.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmAssembly.java @@ -1,6 +1,4 @@ -package me.anthonyw.darkmatter.compiler; - -import me.anthonyw.darkmatter.compiler.llvm.LlvmOpCode; +package me.anthonyw.darkmatter.compiler.llvm; public class LlvmAssembly { diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmCompilable.java b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmCompilable.java new file mode 100644 index 0000000..8aa31c7 --- /dev/null +++ b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmCompilable.java @@ -0,0 +1,13 @@ +package me.anthonyw.darkmatter.compiler.llvm; + +import me.anthonyw.darkmatter.compiler.Compilable; + +public abstract class LlvmCompilable implements Compilable { + + /** + * This will generate LLVM-IR assembly. + * @return + */ + public abstract String llvmCodeGen(); + +} diff --git a/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java index c0a200c..8aa8482 100644 --- a/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java +++ b/src/main/java/me/anthonyw/darkmatter/compiler/llvm/LlvmOpCode.java @@ -2,92 +2,94 @@ public enum LlvmOpCode { - COMMENT(";"), - - RETURN("ret"), - - BR("br"), - - SWITCH("switch"), - - INVOKE("invoke"), - - UNWIND("unwind"), - - UNREACHABLE("unreachable"), - - - // BINARY - - - /** - * Addition - * - * Description: - *
- * The 'add' instruction returns the sum of its two operands. - *
- * - * Example: - *- * {result} = add {type} {var1} {var2} - * {result} = add i32 4, %var - *
- */ - ADD("add"), - - /** - * Subtraction - * - *- * The value produced is the integer or floating point difference of the two operands. - *
- * - *- * The value produced is the integer or floating point product of the two operands. - * Because the operands are the same width, the result of an integer multiplication is the same whether the operands should be deemed unsigned or signed. - *
- * - *- * The value produced is the unsigned integer quotient of the two operands. - * This instruction always performs an unsigned division operation, regardless of whether the arguments are unsigned or not. - *
- * - *