Skip to content

Commit

Permalink
finally fix assembler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MESYETI committed Nov 17, 2023
1 parent a6fae89 commit 23a29f1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 6 additions & 1 deletion source/assembler/assembler.d
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions source/assembler/lexer.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 25 additions & 1 deletion source/assembler/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -164,6 +171,11 @@ class Parser {
}
}

void Error(string str) {
ErrorBegin(CurrentError());
stderr.writeln(str);
}

Node ParseParameter() {
switch (tokens[i].type) {
case TokenType.Identifier: {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 23a29f1

Please sign in to comment.