jack-compiler/vm/vm-lines.c

76 lines
1.4 KiB
C
Raw Permalink Normal View History

2020-12-21 13:05:49 -05:00
#include <stdlib.h>
#include "vm-lines.h"
LINE* mkline(int size) {
LINE* ln = (LINE*)malloc(sizeof(LINE));
ln->tokens = (char**)malloc(sizeof(char*)*size);
ln->count = 0;
return ln;
}
void addtoken(LINE* ln, char* token) {
ln->tokens[ln->count] = token;
ln->count++;
}
void println(LINE* ln, FILE* stream) {
for(int i = 0; i < ln->count; i++) {
fprintf(stream, "%s", ln->tokens[i]);
if(i + 1 < ln->count)
fprintf(stream, " ");
}
fprintf(stream, "\n");
}
void printlns(LINE* lns, FILE* stream) {
while(lns != NULL) {
println(lns, stream);
lns = lns->next;
}
}
void freeln(LINE* ln) {
for(int i = 0; i < ln->count; i++)
free(ln->tokens[i]);
2021-01-03 14:08:54 -05:00
free(ln->tokens);
2020-12-21 13:05:49 -05:00
free(ln);
}
void freelns(LINE* lns) {
LINE* next = lns->next;
freeln(lns);
if(next != NULL)
freelns(next);
}
2021-01-03 14:08:54 -05:00
void freelnblk(LINEBLOCK* blk) {
freelns(blk->head);
free(blk);
}
2020-12-21 13:05:49 -05:00
LINEBLOCK* mklnblk(LINE* start) {
LINEBLOCK* blk = (LINEBLOCK*)malloc(sizeof(LINEBLOCK));
blk->head = start;
blk->tail = start;
return blk;
}
LINEBLOCK* mergelnblks(LINEBLOCK* head, LINEBLOCK* tail) {
if(head == NULL)
return tail;
head->tail->next = tail->head;
head->tail = tail->tail;
free(tail);
return head;
}
void appendln(LINEBLOCK* lnblk, LINE* ln) {
lnblk->tail->next = ln;
lnblk->tail = ln;
}
void appendlnbefore(LINEBLOCK* lnblk, LINE* ln) {
ln->next = lnblk->head;
lnblk->head = ln;
}