Reorganize project structure
This commit is contained in:
69
vm/vm-lines.c
Normal file
69
vm/vm-lines.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#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]);
|
||||
free(ln);
|
||||
}
|
||||
|
||||
void freelns(LINE* lns) {
|
||||
LINE* next = lns->next;
|
||||
freeln(lns);
|
||||
if(next != NULL)
|
||||
freelns(next);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
38
vm/vm-lines.h
Normal file
38
vm/vm-lines.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef VM_LINES_H
|
||||
#define VM_LINES_H
|
||||
#include <stdio.h>
|
||||
|
||||
/* vm-lines
|
||||
* Unified standard for the compiler's output and vm-translator's input.
|
||||
* It is also used by vm-parser when reading .vm files. */
|
||||
|
||||
// Data types
|
||||
typedef struct line {
|
||||
char** tokens;
|
||||
int count;
|
||||
struct line* next;
|
||||
} LINE;
|
||||
|
||||
typedef struct {
|
||||
LINE* head;
|
||||
LINE* tail;
|
||||
} LINEBLOCK;
|
||||
|
||||
// Line manipulation
|
||||
LINE* mkline(int size);
|
||||
void addtoken(LINE* ln, char* token);
|
||||
|
||||
// Line printing
|
||||
void println(LINE* ln, FILE* stream);
|
||||
void printlns(LINE* lns, FILE* stream);
|
||||
|
||||
// Line freeing
|
||||
void freeln(LINE* ln);
|
||||
void freelns(LINE* lns);
|
||||
|
||||
// Line block manipulation
|
||||
LINEBLOCK* mklnblk(LINE* start);
|
||||
LINEBLOCK* mergelnblks(LINEBLOCK* head, LINEBLOCK* tail);
|
||||
void appendln(LINEBLOCK* lnblk, LINE* ln);
|
||||
void appendlnbefore(LINEBLOCK* lnblk, LINE* ln);
|
||||
#endif
|
Reference in New Issue
Block a user