diff --git a/source/assembler/assembler.d b/source/assembler/assembler.d index 1f0c108..bcecd05 100644 --- a/source/assembler/assembler.d +++ b/source/assembler/assembler.d @@ -526,7 +526,8 @@ int AssemblerCLI(string[] args) { auto assembler = new Assembler(); auto lexer = new Lexer(); - lexer.src = readText(inFile); + lexer.src = readText(inFile); + lexer.file = inFile; lexer.Lex(); if (debugLexer) { @@ -540,6 +541,10 @@ int AssemblerCLI(string[] args) { parser.tokens = lexer.tokens; parser.Parse(); + if (!parser.success) { + return 1; + } + if (debugParser) { parser.PrintNodes(); return 0; diff --git a/source/assembler/lexer.d b/source/assembler/lexer.d index 1fc1057..d30b5e6 100644 --- a/source/assembler/lexer.d +++ b/source/assembler/lexer.d @@ -74,14 +74,14 @@ class Lexer { void Lex() { reading = ""; inString = false; - line = 1; - col = 1; + line = 0; + col = 0; src ~= '\n'; for (i = 0; i < src.length; ++ i) { if (src[i] == '\n') { ++ line; - col = 1; + col = 0; } else { ++ col; diff --git a/source/assembler/parser.d b/source/assembler/parser.d index 16f91f5..df10f57 100644 --- a/source/assembler/parser.d +++ b/source/assembler/parser.d @@ -143,10 +143,17 @@ class StringNode : Node { } } +class ParserFail : Exception { + this(string msg = "", string file = __FILE__, size_t line = __LINE__) { + super(msg, file, line); + } +} + class Parser { size_t i; Node[] nodes; Token[] tokens; + bool success; this() { @@ -164,6 +171,11 @@ class Parser { } } + void Error(string str) { + ErrorBegin(CurrentError()); + stderr.writeln(str); + } + Node ParseParameter() { switch (tokens[i].type) { case TokenType.Identifier: { @@ -191,6 +203,10 @@ class Parser { to!int(tokens[i].contents[2 .. $], 2), CurrentError() ); } + case TokenType.Label: { + Error("Cannot use label statement as parameter"); + throw new ParserFail(); + } default: assert(0); } } @@ -236,7 +252,15 @@ class Parser { void Parse() { for (i = 0; i < tokens.length; ++ i) { - auto node = ParseStatement(); + Node node; + + try { + node = ParseStatement(); + } + catch (ParserFail) { + success = false; + return; + } if (node !is null) { nodes ~= node;