Merge branch 'assembler' into 'assembler-e'
# Conflicts: # src/parser.c
This commit is contained in:
commit
4df768f327
@ -1,5 +1,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@ -37,6 +37,7 @@ int writeBinaryFile(word instrs[], char outputFile[]){
|
||||
|
||||
//reads assembly file of "inputFile" name, and passes
|
||||
//each line into a parser
|
||||
//TODO: allocate whole file in memory, line-by-line
|
||||
int readAssemblyFile(char inputFile[]) {
|
||||
if (!isValidFileFormat(filename, "s")){
|
||||
exit(EXIT_FAILURE);
|
||||
@ -59,10 +59,10 @@ char *tokeniseOperands(char* str, int operandCount, char *operands[]){
|
||||
operand = strtok(NULL, OPERAND_DELIMITER);
|
||||
operands[operandCount] = operand;
|
||||
}
|
||||
return(operands);
|
||||
|
||||
}
|
||||
|
||||
//takes inputted assembly line and returns a
|
||||
//pointer to an abstract representation of the instruction
|
||||
a64inst_instruction *parser(char asmLine[]){
|
||||
a64inst_instruction *instr = malloc(sizeof(a64inst_instruction));
|
||||
if (instr == NULL){
|
||||
@ -80,6 +80,7 @@ a64inst_instruction *parser(char asmLine[]){
|
||||
}
|
||||
|
||||
//"opcode operand1, {operand2}, ..."
|
||||
//duplicated as strtok modifies the input string
|
||||
char *stringptr = strdup(asmLine);
|
||||
|
||||
char *opcode = strtok(stringptr, " ");
|
||||
|
||||
43
src/symboltable.c
Normal file
43
src/symboltable.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct st st;
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
const void* key;
|
||||
void* value;
|
||||
node* prev;
|
||||
node* next;
|
||||
} node;
|
||||
|
||||
struct st {
|
||||
node* head;
|
||||
node* tail;
|
||||
};
|
||||
|
||||
// add new node to the end
|
||||
void st_add(st table, void* key, void* value) {
|
||||
node n = {key, value, table.tail};
|
||||
(*(table.tail)).next = &n;
|
||||
table.tail = &n;
|
||||
}
|
||||
|
||||
// returns the pointer to key of the specified node, or null, if it does not exist
|
||||
void* st_search(st table, void* key) {
|
||||
return nodeSearch(table.head, key);
|
||||
}
|
||||
|
||||
void* nodeSearch(node* n, void* key) {
|
||||
if (n != NULL) {
|
||||
if ((*n).key == key) {
|
||||
return (*n).value;
|
||||
}
|
||||
else {
|
||||
return nodeSearch((*n).next, key);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user