Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Symtab.cpp
- // assembly
- //
- // Created by Ethan Laur on 3/11/14.
- // Copyright (c) 2014 Ethan Laur. All rights reserved.
- //
- #include "Symtab.h"
- #include <string.h>
- #include <stdio.h>
- int Symtab::findNodeByName_private(char *sym_name)
- {
- Symtab_struct *list = head;
- int i;
- for (i = 0; list != NULL; list = list->next, i++)
- if (strcmp(sym_name, list->sym_name) == 0)
- break;
- return list == NULL ? -1 : i;
- }
- Symtab_struct *Symtab::findNodeByNumber(int sym_id)
- {
- Symtab_struct *list = head;
- int i;
- if (sym_id < 0) return NULL;
- for (i = 0; i < sym_id && list != NULL; list = list->next, i++);
- return list;
- }
- void Symtab::insertNode(char *sym_name, int sym_location)
- {
- Symtab_struct *node = (Symtab_struct *)malloc(sizeof(Symtab_struct));
- strcpy(node->sym_name, sym_name);
- node->sym_location = sym_location;
- if (findNodeByName_private(sym_name) >= 0)
- {
- printf("[WARN in Symtab::insertNode] Redefinition of symbol %s is ignored\n", sym_name);
- return; //don't add it twice
- }
- if (head == NULL) //empty list
- {
- head = tail = node;
- }
- else
- {
- tail->next = node;
- tail = node;
- }
- tail->next = NULL;
- }
- void Symtab::deleteNode(char *sym_name)
- {
- Symtab_struct *parent, *child, *node;
- int nodenum = findNodeByName_private(sym_name);
- parent = findNodeByNumber(nodenum - 1);
- node = findNodeByNumber(nodenum);
- child = findNodeByNumber(nodenum + 1);
- if (nodenum == 0) //deleting the head
- {
- free(node);
- head = child;
- }
- else if (node == tail) //delete the tail
- {
- free(node);
- tail = parent;
- }
- else if (node == NULL) //node does not exist
- {
- printf("[WARN in Symtab::deleteNode] Attempting to delete a symbol %s that does not exist\n",
- sym_name);
- }
- else //somewhere in mid list
- {
- parent->next = child;
- free(node);
- }
- }
- Symtab_struct *Symtab::findNodeByName(char *sym_name)
- {
- return findNodeByNumber(findNodeByName_private(sym_name));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement