From 3d4ab57f05c55cf14f290ec71e27db8900319880 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 7 Feb 2022 15:33:58 +0000 Subject: [PATCH] Created the skeleton for the parser. --- includes/parser.h | 13 +++++++++++ misc/test.asm | 2 -- src/main.c | 5 +++++ src/opcodes.c | 2 +- src/parser.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 includes/parser.h create mode 100644 src/parser.c diff --git a/includes/parser.h b/includes/parser.h new file mode 100644 index 0000000..c059a4e --- /dev/null +++ b/includes/parser.h @@ -0,0 +1,13 @@ +#ifndef PARSER_H +#define PARSER_H + +#include "list.h" + +typedef struct { + char* name; + int address; +} Symbol; + +void ParseTokens(List*); + +#endif \ No newline at end of file diff --git a/misc/test.asm b/misc/test.asm index 1b71088..0e0c739 100644 --- a/misc/test.asm +++ b/misc/test.asm @@ -1,5 +1,3 @@ -; comments are ignored -; This is a very simple test file for an assemblier. .org 0x100 .db msg "Hello, world!", 0 diff --git a/src/main.c b/src/main.c index ee70969..f688bb2 100644 --- a/src/main.c +++ b/src/main.c @@ -3,6 +3,7 @@ #include #include "../includes/list.h" #include "../includes/lexer.h" +#include "../includes/parser.h" typedef struct { char *opcode; @@ -38,6 +39,10 @@ int main(int argc, char* args[]) { while((bytes_read = getline(&line, &len, file)) != -1) { List* tokens = GetTokensFromLine(line); + if (line_number == 1 || line_number == 2) { + ParseTokens(tokens); + } + for (int i = 0; i < tokens->size; i++) { Token* t = (Token*) tokens->content[i]; diff --git a/src/opcodes.c b/src/opcodes.c index b00c771..1f6d673 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -6,7 +6,7 @@ char *opcodes[OPCODECOUNT] = { "sub", "jz", "int", - "hlt", + "yld", "ret" }; diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..51411ad --- /dev/null +++ b/src/parser.c @@ -0,0 +1,55 @@ +#include "../includes/parser.h" +#include "../includes/lexer.h" +#include +#include + +static List* SymbolsTable; +static unsigned int program_counter = 0; +void ProcessDirective(List*); +void ProcessVariableDeclaration(List*); + +void ParseTokens(List* tokens) { + for(int i = 0; i < tokens->size; i++) { + if (((Token *) tokens->content[i])->type == TK_Directive){ + ProcessDirective(tokens); + } + } +} + +void ProcessDirective(List* tokens) { + if (tokens->size < 2) { + printf("Directives failed\n"); + return; + } + + Token* directive = (Token *) tokens->content[0]; + + if (strcmp(".org", directive->value) == 0) { + Token* address_start = (Token*) tokens->content[1]; + if (address_start->type == TK_Number) { + long address = strtol(address_start->value, NULL, 10); + printf("Address starting at '%ld'\n", address); + } + else if (address_start->type == TK_Hex) { + long address = strtol(address_start->value, NULL, 16); + printf("Setting address to '%ld'\n", address); + } + } + + if (strcmp(".db", directive->value) == 0) { + ProcessVariableDeclaration(tokens); + } +} + +void ProcessVariableDeclaration(List* tokens) { + if (tokens->size < 3) { + printf("Invalid variable delcaration\n"); + return; + } + + Token* symbol_token = (Token*) tokens->content[1]; + Token* string_literal = (Token*) tokens->content[2]; + + printf("Found string literal.\n"); + printf("Name '%s' value: '%s'\n", symbol_token->value, string_literal->value); +} \ No newline at end of file