Updated the way string symbols are handled, allowing the programmer to terminate a string with any byte or none at all.
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
#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>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include "opcodes.h"
|
#include "opcodes.h"
|
||||||
|
|
||||||
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
#define SYMBOLSTABLE_DEFAULT_CAPACITY 128
|
||||||
@@ -16,12 +18,18 @@ typedef enum {
|
|||||||
UnresolvedSymbol
|
UnresolvedSymbol
|
||||||
} SymbolType;
|
} SymbolType;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char* String;
|
||||||
|
int Terminated;
|
||||||
|
u_int8_t TerminatingByte;
|
||||||
|
} SymbolString;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char* Name;
|
char* Name;
|
||||||
SymbolType Type;
|
SymbolType Type;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
char* Text;
|
SymbolString* String;
|
||||||
int Number;
|
int Number;
|
||||||
Instruction* Instruction;
|
Instruction* Instruction;
|
||||||
} Value;
|
} Value;
|
||||||
@@ -36,6 +44,7 @@ typedef struct {
|
|||||||
SymbolTable* CreateSymbolTable(void);
|
SymbolTable* CreateSymbolTable(void);
|
||||||
Symbol* TryGetSymbol(char* name, SymbolTable* table);
|
Symbol* TryGetSymbol(char* name, SymbolTable* table);
|
||||||
Symbol* AddSymbolToTable(char* name, SymbolType type, SymbolTable* table);
|
Symbol* AddSymbolToTable(char* name, SymbolType type, SymbolTable* table);
|
||||||
|
SymbolString* CreateSymbolString(char* string, int terminated);
|
||||||
void FreeSymbolTable(SymbolTable* table);
|
void FreeSymbolTable(SymbolTable* table);
|
||||||
void FreeSymbol(Symbol* symbol);
|
void FreeSymbol(Symbol* symbol);
|
||||||
|
|
||||||
|
|||||||
+29
-5
@@ -100,13 +100,34 @@ void HandleAssemblerDirective() {
|
|||||||
}
|
}
|
||||||
else if (token->Class == CharacterClass) {
|
else if (token->Class == CharacterClass) {
|
||||||
Symbol* s = AddSymbolToTable(symbolname, StringSymbol, MachineState.SymbolsTable);
|
Symbol* s = AddSymbolToTable(symbolname, StringSymbol, MachineState.SymbolsTable);
|
||||||
|
char* string = token->Lemexe;
|
||||||
|
|
||||||
s->Value.Text = token->Lemexe;
|
AdvanceParser();
|
||||||
|
|
||||||
IgnoreParserLine();
|
if (PeekToken()->Class == PunctuationClass && PeekToken()->Value.Punctuation == Comma) {
|
||||||
|
AdvanceParser();
|
||||||
|
|
||||||
|
token = PeekToken();
|
||||||
|
|
||||||
|
if (token->Class != NumberClass) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Expected terminating byte.\n", token->LineNumber);
|
||||||
|
IgnoreParserLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (token->Value.Number > 255) {
|
||||||
|
fprintf(stderr, "[Error] Line %d: Number must fit within a byte.\n", token->LineNumber);
|
||||||
|
IgnoreParserLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->Value.String = CreateSymbolString(string, 1);
|
||||||
|
s->Value.String->TerminatingByte = token->Value.Number;
|
||||||
|
}
|
||||||
|
else s->Value.String = CreateSymbolString(string, 0);
|
||||||
}
|
}
|
||||||
else IgnoreParserLine();
|
|
||||||
|
|
||||||
|
IgnoreParserLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
Parameter* GetParameterType() {
|
Parameter* GetParameterType() {
|
||||||
@@ -273,10 +294,13 @@ void PrintSymbols(void) {
|
|||||||
printf("%s -> %s", symbol->Name, mn);
|
printf("%s -> %s", symbol->Name, mn);
|
||||||
break;
|
break;
|
||||||
case NumericSymbol:
|
case NumericSymbol:
|
||||||
printf("[N] %s", symbol->Name);
|
printf("[N] %s Value: %d", symbol->Name, symbol->Value.Number);
|
||||||
break;
|
break;
|
||||||
case StringSymbol:
|
case StringSymbol:
|
||||||
printf("[S] %s", symbol->Name);
|
if (symbol->Value.String->Terminated)
|
||||||
|
printf("[S] %s, %d", symbol->Name, symbol->Value.String->TerminatingByte);
|
||||||
|
else
|
||||||
|
printf("[S] %s", symbol->Value.String->String);
|
||||||
break;
|
break;
|
||||||
case UnresolvedSymbol:
|
case UnresolvedSymbol:
|
||||||
printf("[U] %s", symbol->Name);
|
printf("[U] %s", symbol->Name);
|
||||||
|
|||||||
@@ -40,6 +40,23 @@ Symbol* CreateSymbol(char* name, SymbolType type) {
|
|||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SymbolString* CreateSymbolString(char* string, int terminated) {
|
||||||
|
if (!string) return NULL;
|
||||||
|
|
||||||
|
SymbolString* s = calloc(1, sizeof(SymbolString));
|
||||||
|
|
||||||
|
if (!s) {
|
||||||
|
fprintf(stderr, "Failed to calloc string for symbol. %s.\n", strerror(errno));
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->String = string;
|
||||||
|
s->Terminated = terminated;
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
Symbol* TryGetSymbol(char* name, SymbolTable* table) {
|
Symbol* TryGetSymbol(char* name, SymbolTable* table) {
|
||||||
if (!name || !table) return NULL;
|
if (!name || !table) return NULL;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user