diff --git a/compiler/dub.json b/compiler/dub.json index 8172f18..8e5c6ab 100644 --- a/compiler/dub.json +++ b/compiler/dub.json @@ -5,5 +5,8 @@ "copyright": "Copyright © 2023, Andrew Lalis", "description": "Compiler for the NIMP language.", "license": "MIT", - "name": "compiler" + "name": "compiler", + "dependencies": { + "streams": "~>3.4.3" + } } \ No newline at end of file diff --git a/compiler/dub.selections.json b/compiler/dub.selections.json new file mode 100644 index 0000000..163bdbb --- /dev/null +++ b/compiler/dub.selections.json @@ -0,0 +1,6 @@ +{ + "fileVersion": 1, + "versions": { + "streams": "3.4.3" + } +} diff --git a/compiler/source/app.d b/compiler/source/app.d index b02f56e..9256711 100644 --- a/compiler/source/app.d +++ b/compiler/source/app.d @@ -1,5 +1,18 @@ import std.stdio; +import std.string; +import nimpc; +import streams; -void main() { - writeln("Edit source/app.d to start your project."); +int main(string[] args) { + if (args.length < 2) { + writeln("Missing required file args."); + return 1; + } + string[] files = args[1 .. $]; + writefln!"Compiling %s"(files); + foreach (filename; files) { + auto sIn = FileInputStream(toStringz(filename)); + auto tokens = tokenize(sIn); + } + return 0; } diff --git a/compiler/source/nimpc/ast/package.d b/compiler/source/nimpc/ast/package.d new file mode 100644 index 0000000..338fb9c --- /dev/null +++ b/compiler/source/nimpc/ast/package.d @@ -0,0 +1,6 @@ +/** + * This module contains definitions for the components in NIMP's abstract + * syntax tree. + */ +module nimpc.ast; + diff --git a/compiler/source/nimpc/lexer.d b/compiler/source/nimpc/lexer.d new file mode 100644 index 0000000..18f279a --- /dev/null +++ b/compiler/source/nimpc/lexer.d @@ -0,0 +1,62 @@ +module nimpc.lexer; + +import streams; +import std.stdio; +import std.regex; +import std.array; + +enum TokenType { + KEYWORD, + DATA_TYPE, + SENTENCE_END, + LITERAL_INTEGER, + LITERAL_FLOAT, + LITERAL_BOOLEAN, + LITERAL_STRING +} + +struct Token { + TokenType type; + string content; + uint line; + uint column; +} + +class LexerException : Exception { + const uint sourceLine; + const uint sourceColumn; + + this(string msg, uint sourceLine, uint sourceColumn) { + super(msg); + this.sourceLine = sourceLine; + this.sourceColumn = sourceColumn; + } +} + +/** + * Parses a list of tokens from an input stream of lines of code. + * Params: + * inputStream = The lines of input to parse. + * Returns: A list of tokens. + */ +Token[] tokenize(S)(S inputStream) if (isInputStream!(S, char)) { + Appender!(Token[]) tokenApp; + uint line = 0; + uint col = 0; + + while (true) { + char c = readChar(inputStream, line, col); + writeln(c); + } + + + return tokenApp[]; +} + +private char readChar(S)(S stream, uint line, uint col) if (isInputStream!(S, char)) { + auto result = readOne(stream); + if (result.hasError) { + throw new LexerException("Failed to read one more char from stream.", line, col); + } + return result.element; +} diff --git a/compiler/source/nimpc/package.d b/compiler/source/nimpc/package.d new file mode 100644 index 0000000..95b7a58 --- /dev/null +++ b/compiler/source/nimpc/package.d @@ -0,0 +1,8 @@ +/** + * The NIMP compiler front-end module, which includes the language's lexer, + * parser, and syntax tree. + */ +module nimpc; + +public import nimpc.ast; +public import nimpc.lexer;