Added start of compiler.

This commit is contained in:
Andrew Lalis 2023-06-23 08:55:56 +02:00
parent d9d1b539cd
commit b82bfae676
6 changed files with 101 additions and 3 deletions

View File

@ -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"
}
}

View File

@ -0,0 +1,6 @@
{
"fileVersion": 1,
"versions": {
"streams": "3.4.3"
}
}

View File

@ -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;
}

View File

@ -0,0 +1,6 @@
/**
* This module contains definitions for the components in NIMP's abstract
* syntax tree.
*/
module nimpc.ast;

View File

@ -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;
}

View File

@ -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;