Redesigning the way the Symbol's represent string declaration (the .db msg 'Hello World' type of declarations).
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#ifndef SYMBOLSTABLE_H
|
#ifndef SYMBOLSTABLE_H
|
||||||
#define SYMBOLSTABLE_H
|
#define SYMBOLSTABLE_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -13,13 +14,19 @@ typedef enum {
|
|||||||
Address
|
Address
|
||||||
} SymbolType;
|
} SymbolType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* String;
|
||||||
|
uint8_t TerminatorByte;
|
||||||
|
int HasTerminatorByte;
|
||||||
|
} SymbolString;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* Name;
|
char* Name;
|
||||||
int Length;
|
int Length;
|
||||||
int Resolved;
|
int Resolved;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
char* Text;
|
SymbolString String;
|
||||||
int Number;
|
int Number;
|
||||||
//Instruction Instruction;
|
//Instruction Instruction;
|
||||||
} Value;
|
} Value;
|
||||||
@@ -33,7 +40,8 @@ typedef struct {
|
|||||||
|
|
||||||
SymbolTable* CreateSymbolTable(void);
|
SymbolTable* CreateSymbolTable(void);
|
||||||
Symbol* TryGetSymbol(char* name, SymbolTable* table);
|
Symbol* TryGetSymbol(char* name, SymbolTable* table);
|
||||||
Symbol* AddSymbolToTable(char* name, void* value, int length, SymbolTable* table);
|
Symbol* AddSymbolToTable(char* name, SymbolTable* table);
|
||||||
|
SymbolString* CreateSymbolString(char* text);
|
||||||
void FreeSymbolTable(SymbolTable* table);
|
void FreeSymbolTable(SymbolTable* table);
|
||||||
void FreeSymbol(Symbol* symbol);
|
void FreeSymbol(Symbol* symbol);
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -1,9 +1,9 @@
|
|||||||
.db video_start 0xF37F
|
.db video_start 0xF37F
|
||||||
.db msg "Hello, world!", 0
|
.db msg "Hello, world!", 0
|
||||||
|
|
||||||
load r1, [msg] ; Because the lod* instructions can't load an address
|
load r1, msg ; Because the lod* instructions can't load an address
|
||||||
; from anything but a register, this becomes laa r1, [msg]
|
; from anything but a register, this becomes laa r1, msg
|
||||||
|
load r8, [15]
|
||||||
;Routine: string_length
|
;Routine: string_length
|
||||||
;In: String address in r1
|
;In: String address in r1
|
||||||
;Out: Length in R2
|
;Out: Length in R2
|
||||||
|
|||||||
+72
-2
@@ -4,7 +4,12 @@ const List* TokensList;
|
|||||||
int CurrentToken = 0;
|
int CurrentToken = 0;
|
||||||
|
|
||||||
void PrintSymbols(void);
|
void PrintSymbols(void);
|
||||||
|
<<<<<<< Updated upstream
|
||||||
void HandleOperation(void);
|
void HandleOperation(void);
|
||||||
|
=======
|
||||||
|
SymbolString* ParseIdentifierParameter(void);
|
||||||
|
Instruction* HandleOperation(void);
|
||||||
|
>>>>>>> Stashed changes
|
||||||
void HandleAssemblerDirective(void);
|
void HandleAssemblerDirective(void);
|
||||||
void AdvanceParser(void);
|
void AdvanceParser(void);
|
||||||
void IgnoreParserLine(void);
|
void IgnoreParserLine(void);
|
||||||
@@ -31,9 +36,34 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
HandleOperation();
|
HandleOperation();
|
||||||
break;
|
break;
|
||||||
case LabelClass:
|
case LabelClass:
|
||||||
|
<<<<<<< Updated upstream
|
||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable)->Resolved = 1;
|
AddSymbolToTable(t->Lemexe, NULL, strlen(t->Lemexe), MachineState.SymbolsTable)->Resolved = 1;
|
||||||
AdvanceParser(); //Consume the NewLine
|
AdvanceParser(); //Consume the NewLine
|
||||||
|
=======
|
||||||
|
{
|
||||||
|
Symbol* symbol = TryGetSymbol(t->Lemexe, MachineState.SymbolsTable);
|
||||||
|
|
||||||
|
if (symbol && symbol->Resolved) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Redefinition of symbol '%s'\n", t->LineNumber, t->Lemexe);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
AdvanceParser(); //Consume the label token
|
||||||
|
|
||||||
|
if (!symbol) AddSymbolToTable(t->Lemexe, MachineState.SymbolsTable)->Resolved = 1;
|
||||||
|
else symbol->Resolved = 1;
|
||||||
|
|
||||||
|
AdvanceParser(); //Consume the NewLine
|
||||||
|
|
||||||
|
// Instruction* ins = HandleOperation();
|
||||||
|
|
||||||
|
// if (!ins) continue;
|
||||||
|
|
||||||
|
// symbol->Value.Instruction = ins;
|
||||||
|
// symbol->InstructionPointer = 1;
|
||||||
|
}
|
||||||
|
>>>>>>> Stashed changes
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "[Warning] Line %d: Syntax error, expected start of expression, got '%c' [%d].\n", t->LineNumber, t->Value.Punctuation, t->Class);
|
fprintf(stderr, "[Warning] Line %d: Syntax error, expected start of expression, got '%c' [%d].\n", t->LineNumber, t->Value.Punctuation, t->Class);
|
||||||
@@ -58,6 +88,43 @@ IRState* ParseTokens(List* tokens) {
|
|||||||
return &MachineState;
|
return &MachineState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SymbolString* ParseIdentifierParameter(void) {
|
||||||
|
Token* token = PeekToken();
|
||||||
|
|
||||||
|
if (token->Class != IdentifierClass) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Expected identifier\n", token->LineNumber);
|
||||||
|
IgnoreParserLine();
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
SymbolString* string = CreateSymbolString(token->Lemexe);
|
||||||
|
|
||||||
|
AdvanceParser();
|
||||||
|
|
||||||
|
if (token->Class == PunctuationClass && token->Value.Punctuation == NewLine) {
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
else if (token->Class == PunctuationClass && token->Value.Punctuation == Comma) {
|
||||||
|
AdvanceParser(); // Comsume the comma
|
||||||
|
|
||||||
|
token = PeekToken();
|
||||||
|
|
||||||
|
if (token->Class != NumberClass) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Expected string termination number.\n", token->LineNumber);
|
||||||
|
IgnoreParserLine();
|
||||||
|
free(string);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
string->HasTerminatorByte = 1;
|
||||||
|
string->TerminatorByte = token->Value.Number;
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
Parameter* GetParameterType() {
|
Parameter* GetParameterType() {
|
||||||
Token* token = PeekToken();
|
Token* token = PeekToken();
|
||||||
Symbol* symbol = NULL;
|
Symbol* symbol = NULL;
|
||||||
@@ -73,14 +140,17 @@ Parameter* GetParameterType() {
|
|||||||
AdvanceParser();
|
AdvanceParser();
|
||||||
|
|
||||||
return param;
|
return param;
|
||||||
|
<<<<<<< Updated upstream
|
||||||
case LabelClass:
|
case LabelClass:
|
||||||
|
=======
|
||||||
|
>>>>>>> Stashed changes
|
||||||
case IdentifierClass:
|
case IdentifierClass:
|
||||||
param->ParameterType = AddressParameter;
|
param->ParameterType = AddressParameter;
|
||||||
param->InterpretedAs = AddressParameter;
|
param->InterpretedAs = AddressParameter;
|
||||||
|
|
||||||
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
||||||
|
|
||||||
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, token->Lemexe, strlen(token->Lemexe), MachineState.SymbolsTable);
|
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, MachineState.SymbolsTable);
|
||||||
|
|
||||||
param->Value.Symbol = symbol;
|
param->Value.Symbol = symbol;
|
||||||
|
|
||||||
@@ -113,7 +183,7 @@ Parameter* GetParameterType() {
|
|||||||
|
|
||||||
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
symbol = TryGetSymbol(token->Lemexe, MachineState.SymbolsTable);
|
||||||
|
|
||||||
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, NULL, strlen(token->Lemexe), MachineState.SymbolsTable);
|
if (!symbol) symbol = AddSymbolToTable(token->Lemexe, MachineState.SymbolsTable);
|
||||||
|
|
||||||
param->Value.Symbol = symbol;
|
param->Value.Symbol = symbol;
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-5
@@ -25,7 +25,7 @@ SymbolTable* CreateSymbolTable(void){
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
Symbol* CreateSymbol(char* name, void* value, int length) {
|
Symbol* CreateSymbol(char* name) {
|
||||||
Symbol* symbol = calloc(1, sizeof(Symbol));
|
Symbol* symbol = calloc(1, sizeof(Symbol));
|
||||||
|
|
||||||
if (!symbol) {
|
if (!symbol) {
|
||||||
@@ -34,13 +34,24 @@ Symbol* CreateSymbol(char* name, void* value, int length) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
symbol->Length = length;
|
|
||||||
symbol->Name = name;
|
symbol->Name = name;
|
||||||
//symbol->Value = value;
|
|
||||||
|
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SymbolString* CreateSymbolString(char* text) {
|
||||||
|
SymbolString* string = calloc(1, sizeof(SymbolString));
|
||||||
|
|
||||||
|
if (!string) {
|
||||||
|
fprintf(stderr, "Failed to create string data for symbol. %s.\n", strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
string->String = text;
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
Symbol* TryGetSymbol(char* name, SymbolTable* table) {
|
Symbol* TryGetSymbol(char* name, SymbolTable* table) {
|
||||||
if (!name || !table) return NULL;
|
if (!name || !table) return NULL;
|
||||||
|
|
||||||
@@ -51,7 +62,7 @@ Symbol* TryGetSymbol(char* name, SymbolTable* table) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Symbol* AddSymbolToTable(char* name, void* value, int length, SymbolTable* table) {
|
Symbol* AddSymbolToTable(char* name, SymbolTable* table) {
|
||||||
//if (!name || !value || !table || length == 0) return NULL;
|
//if (!name || !value || !table || length == 0) return NULL;
|
||||||
|
|
||||||
for(int i = 0; i < table->Size; i++) {
|
for(int i = 0; i < table->Size; i++) {
|
||||||
@@ -75,7 +86,7 @@ Symbol* AddSymbolToTable(char* name, void* value, int length, SymbolTable* table
|
|||||||
table->Symbols = newBlock;
|
table->Symbols = newBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
Symbol* symbol = CreateSymbol(name, value, length);
|
Symbol* symbol = CreateSymbol(name);
|
||||||
|
|
||||||
table->Symbols[table->Size] = symbol;
|
table->Symbols[table->Size] = symbol;
|
||||||
table->Size++;
|
table->Size++;
|
||||||
|
|||||||
Reference in New Issue
Block a user