add & float, init tok map

This commit is contained in:
zongor 2023-03-04 13:20:59 -05:00
parent 02d37d3bcc
commit ef263d9cc4
8 changed files with 924 additions and 676 deletions

View File

@ -1 +1 @@
BasedOnStyle: GNU BasedOnStyle: Mozilla

16
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/varaq",
"args": ["tests/add.vq", "public/module.wasm"],
"cwd": "${workspaceFolder}"
}
]
}

View File

@ -1,9 +1,7 @@
CC = gcc CC = gcc
CCFLAGS= -g CCFLAGS= -g
all: varaq all:
varaq:
$(CC) $(CCFLAGS) -o varaq main.c compiler.c tokenizer.c $(CC) $(CCFLAGS) -o varaq main.c compiler.c tokenizer.c
clean: clean:

145
compile_test.c Normal file
View File

@ -0,0 +1,145 @@
#include "compiler.h"
#include "common.h"
#include "tokenizer.h"
Code *
demo_function_compile ()
{
Code *add_args_code = (Code *)calloc (1, sizeof (Code));
append_byte (add_args_code, f32);
append_byte (add_args_code, f32);
Code *add_return_code = (Code *)calloc (1, sizeof (Code));
append_byte (add_return_code, f32);
Code *add_function_type = (Code *)calloc (1, sizeof (Code));
append_byte (add_function_type, FUNCTION);
append (add_function_type, encodeVector (add_args_code));
append (add_function_type, encodeVector (add_return_code));
add_function_type->override = true;
add_function_type->override_count = 1;
Code *type_section = createSection (TYPE, encodeVector (add_function_type));
Code *return_type_code = (Code *)calloc (1, sizeof (Code));
append_byte (return_type_code, 0x00);
return_type_code->override = true;
return_type_code->override_count = 1;
Code *func_section = createSection (FUNC, encodeVector (return_type_code));
Code *exp = encodeString ("main");
append_byte (exp, EXPORT_FUNC);
append_byte (exp, 0x00);
exp->override = true;
exp->override_count = 1;
Code *export_section = createSection (EXPORT, encodeVector (exp));
Code *code = (Code *)calloc (1, sizeof (Code));
append_byte (code, LOCAL_GET);
append (code, unsignedLEB128 (0));
append_byte (code, LOCAL_GET);
append (code, unsignedLEB128 (1));
append_byte (code, F32_ADD);
Code *body = (Code *)calloc (1, sizeof (Code));
append_byte (body, EMPTY_ARRAY);
append (body, code);
append_byte (body, END);
Code *function_body = (Code *)calloc (1, sizeof (Code));
append (function_body, encodeVector (body));
function_body->override = true;
function_body->override_count = 1;
Code *code_section = createSection (CODE, encodeVector (function_body));
Code *tape = (Code *)malloc (sizeof (Code));
tape->cells = calloc (8, sizeof (uint8_t));
tape->cells[0] = 0;
tape->cells[1] = 'a';
tape->cells[2] = 's';
tape->cells[3] = 'm';
tape->cells[4] = 1;
tape->cells[5] = 0;
tape->cells[6] = 0;
tape->cells[7] = 0;
tape->count = 8;
append (tape, type_section);
append (tape, func_section);
append (tape, export_section);
append (tape, code_section);
return tape;
}
Code *
demo_add_compile ()
{
Code *add_args_code = (Code *)calloc (1, sizeof (Code));
Code *add_return_code = (Code *)calloc (1, sizeof (Code));
append_byte (add_return_code, f64);
Code *add_function_type = (Code *)calloc (1, sizeof (Code));
append_byte (add_function_type, FUNCTION);
append (add_function_type, encodeVector (add_args_code));
append (add_function_type, encodeVector (add_return_code));
add_function_type->override = true;
add_function_type->override_count = 1;
Code *type_section = createSection (TYPE, encodeVector (add_function_type));
Code *return_type_code = (Code *)calloc (1, sizeof (Code));
append_byte (return_type_code, 0x00);
return_type_code->override = true;
return_type_code->override_count = 1;
Code *func_section = createSection (FUNC, encodeVector (return_type_code));
Code *exp = encodeString ("main");
append_byte (exp, EXPORT_FUNC);
append_byte (exp, 0x00);
exp->override = true;
exp->override_count = 1;
Code *export_section = createSection (EXPORT, encodeVector (exp));
Code *code = (Code *)calloc (1, sizeof (Code));
append_byte (code, F64_CONST);
append_f64 (code, 6.7);
append_byte (code, F64_CONST);
append_f64 (code, 8.5);
append_byte (code, F64_ADD);
Code *body = (Code *)calloc (1, sizeof (Code));
append_byte (body, EMPTY_ARRAY);
append (body, code);
append_byte (body, END);
Code *function_body = (Code *)calloc (1, sizeof (Code));
append (function_body, encodeVector (body));
function_body->override = true;
function_body->override_count = 1;
Code *code_section = createSection (CODE, encodeVector (function_body));
Code *tape = (Code *)malloc (sizeof (Code));
tape->cells = calloc (8, sizeof (uint8_t));
tape->cells[0] = 0;
tape->cells[1] = 'a';
tape->cells[2] = 's';
tape->cells[3] = 'm';
tape->cells[4] = 1;
tape->cells[5] = 0;
tape->cells[6] = 0;
tape->cells[7] = 0;
tape->count = 8;
append (tape, type_section);
append (tape, func_section);
append (tape, export_section);
append (tape, code_section);
return tape;
}

View File

@ -3,35 +3,30 @@
#include "common.h" #include "common.h"
#include "tokenizer.h" #include "tokenizer.h"
Code * Code*
signedLEB128 (size_t num) signedLEB128(size_t num)
{ {
bool more = true; bool more = true;
Code *buffer = (Code *)malloc (sizeof (Code)); Code* buffer = (Code*)malloc(sizeof(Code));
buffer->count = 0; buffer->count = 0;
int n = (int)num; int n = (int)num;
while (more) while (more) {
{
uint8_t byte = n & 0x7f; uint8_t byte = n & 0x7f;
n >>= 7; n >>= 7;
if ((n == 0 && (byte & 0x40) == 0) || (n == -1 && (byte & 0x40) != 0)) if ((n == 0 && (byte & 0x40) == 0) || (n == -1 && (byte & 0x40) != 0)) {
{
more = false; more = false;
} } else {
else
{
byte |= 0x80; byte |= 0x80;
} }
size_t old_count = buffer->count; size_t old_count = buffer->count;
uint8_t *tmp = (uint8_t *)calloc ( uint8_t* tmp =
(old_count + 1), (uint8_t*)calloc((old_count + 1),
sizeof (uint8_t)); // really slow and bad refactor later sizeof(uint8_t)); // really slow and bad refactor later
memcpy (tmp, buffer->cells, buffer->count * sizeof (uint8_t)); memcpy(tmp, buffer->cells, buffer->count * sizeof(uint8_t));
if (tmp) if (tmp) {
{
buffer->cells = tmp; buffer->cells = tmp;
} }
buffer->cells[old_count] = byte; buffer->cells[old_count] = byte;
@ -41,300 +36,383 @@ signedLEB128 (size_t num)
return buffer; return buffer;
} }
Code * Code*
unsignedLEB128 (size_t num) unsignedLEB128(size_t num)
{ {
Code *buffer = (Code *)malloc (sizeof (Code)); Code* buffer = (Code*)malloc(sizeof(Code));
buffer->count = 0; buffer->count = 0;
int n = (int)num; int n = (int)num;
do do {
{
uint8_t byte = n & 0x7f; uint8_t byte = n & 0x7f;
n >>= 7; n >>= 7;
if (n != 0) if (n != 0) {
{
byte |= 0x80; byte |= 0x80;
} }
size_t old_count = buffer->count; size_t old_count = buffer->count;
uint8_t *tmp = (uint8_t *)calloc ( uint8_t* tmp =
(old_count + 1), (uint8_t*)calloc((old_count + 1),
sizeof (uint8_t)); // really slow and bad refactor later sizeof(uint8_t)); // really slow and bad refactor later
memcpy (tmp, buffer->cells, buffer->count * sizeof (uint8_t)); memcpy(tmp, buffer->cells, buffer->count * sizeof(uint8_t));
if (tmp) if (tmp) {
{
buffer->cells = tmp; buffer->cells = tmp;
} }
buffer->cells[old_count] = byte; buffer->cells[old_count] = byte;
buffer->count += 1; buffer->count += 1;
} } while (n != 0);
while (n != 0);
return buffer; return buffer;
} }
Code * Code*
append_byte (Code *tape, uint8_t data) generic_append(Code* tape, void* data, size_t data_size)
{ {
size_t old_count = tape->count; size_t old_count = tape->count;
uint8_t *tmp = (uint8_t *)calloc ((old_count + 1), sizeof (uint8_t)); uint8_t* tmp = (uint8_t*)calloc((old_count + data_size), sizeof(uint8_t));
if (old_count > 0) if (old_count > 0) {
{ memcpy(tmp, tape->cells, tape->count * sizeof(uint8_t));
memcpy (tmp, tape->cells, tape->count * sizeof (uint8_t));
} }
if (tmp) if (tmp) {
{
tape->cells = tmp;
}
tape->cells[old_count] = data;
tape->count += 1;
return tape;
}
Code *
append_f64 (Code *tape, double data)
{
size_t old_count = tape->count;
uint8_t *tmp
= (uint8_t *)calloc ((old_count + sizeof (data)), sizeof (uint8_t));
if (old_count > 0)
{
memcpy (tmp, tape->cells, tape->count * sizeof (uint8_t));
}
if (tmp)
{
tape->cells = tmp; tape->cells = tmp;
} }
memcpy ((tape->cells + old_count), (unsigned char *)&data, sizeof (data)); memcpy((tape->cells + old_count), (unsigned char*)data, data_size);
tape->count += sizeof (data); tape->count += data_size;
return tape; return tape;
} }
Code * Code*
append (Code *tape, Code *data) append_byte(Code* tape, uint8_t data)
{
return generic_append(tape, &data, sizeof(uint8_t));
}
Code*
append_f64(Code* tape, double data)
{
return generic_append(tape, &data, sizeof(double));
}
Code*
append(Code* tape, Code* data)
{ {
size_t old_count = tape->count; size_t old_count = tape->count;
uint8_t *tmp uint8_t* tmp = (uint8_t*)calloc((old_count + data->count), sizeof(uint8_t));
= (uint8_t *)calloc ((old_count + data->count), sizeof (uint8_t)); memcpy(tmp, tape->cells, tape->count * sizeof(uint8_t));
memcpy (tmp, tape->cells, tape->count * sizeof (uint8_t)); if (tmp) {
if (tmp)
{
tape->cells = tmp; tape->cells = tmp;
} }
memcpy ((tape->cells + old_count), data->cells, memcpy((tape->cells + old_count), data->cells, data->count * sizeof(uint8_t));
data->count * sizeof (uint8_t));
tape->count += data->count; tape->count += data->count;
return tape; return tape;
} }
Code * Code*
encodeString (char *string) encodeString(char* string)
{ {
Code *buffer = (Code *)malloc (sizeof (Code)); Code* buffer = (Code*)malloc(sizeof(Code));
buffer->cells = (uint8_t *)malloc (sizeof (uint8_t)); buffer->cells = (uint8_t*)malloc(sizeof(uint8_t));
buffer->cells[0] = strlen (string); buffer->cells[0] = strlen(string);
buffer->count = 1; buffer->count = 1;
uint8_t *tmp = (uint8_t *)malloc ((1 + strlen (string)) * sizeof (char)); uint8_t* tmp = (uint8_t*)malloc((1 + strlen(string)) * sizeof(char));
memcpy (tmp, buffer->cells, buffer->count * sizeof (uint8_t)); memcpy(tmp, buffer->cells, buffer->count * sizeof(uint8_t));
if (tmp) if (tmp) {
{
buffer->cells = tmp; buffer->cells = tmp;
} }
memcpy ((buffer->cells + 1), string, strlen (string) * sizeof (char)); memcpy((buffer->cells + 1), string, strlen(string) * sizeof(char));
buffer->count += strlen (string); buffer->count += strlen(string);
return buffer; return buffer;
} }
Code * Code*
encodeVector (Code *data) encodeVector(Code* data)
{ {
size_t count = data->override ? data->override_count : data->count; size_t count = data->override ? data->override_count : data->count;
Code *buffer = unsignedLEB128 (count); Code* buffer = unsignedLEB128(count);
append (buffer, data); append(buffer, data);
return buffer; return buffer;
} }
Code * Code*
createSection (uint8_t section, Code *data) createSection(uint8_t section, Code* data)
{ {
Code *buffer = (Code *)malloc (sizeof (Code)); Code* buffer = (Code*)malloc(sizeof(Code));
buffer->cells = (uint8_t *)malloc (sizeof (uint8_t)); buffer->cells = (uint8_t*)malloc(sizeof(uint8_t));
buffer->cells[0] = section; buffer->cells[0] = section;
buffer->count = 1; buffer->count = 1;
return append (buffer, encodeVector (data)); return append(buffer, encodeVector(data));
} }
Code * Code*
demo_function_compile () compile(char* buffer)
{ {
Code *add_args_code = (Code *)calloc (1, sizeof (Code)); char number[100];
append_byte (add_args_code, f32);
append_byte (add_args_code, f32);
Code *add_return_code = (Code *)calloc (1, sizeof (Code)); Code* add_args_code = (Code*)calloc(1, sizeof(Code));
append_byte (add_return_code, f32); Code* add_return_code = (Code*)calloc(1, sizeof(Code));
append_byte(add_return_code, f64);
Code *add_function_type = (Code *)calloc (1, sizeof (Code)); Code* add_function_type = (Code*)calloc(1, sizeof(Code));
append_byte (add_function_type, FUNCTION); append_byte(add_function_type, FUNCTION);
append (add_function_type, encodeVector (add_args_code)); append(add_function_type, encodeVector(add_args_code));
append (add_function_type, encodeVector (add_return_code)); append(add_function_type, encodeVector(add_return_code));
add_function_type->override = true; add_function_type->override = true;
add_function_type->override_count = 1; add_function_type->override_count = 1;
Code *type_section = createSection (TYPE, encodeVector (add_function_type)); Code* type_section = createSection(TYPE, encodeVector(add_function_type));
Code *return_type_code = (Code *)calloc (1, sizeof (Code)); Code* return_type_code = (Code*)calloc(1, sizeof(Code));
append_byte (return_type_code, 0x00); append_byte(return_type_code, 0x00);
return_type_code->override = true; return_type_code->override = true;
return_type_code->override_count = 1; return_type_code->override_count = 1;
Code *func_section = createSection (FUNC, encodeVector (return_type_code)); Code* func_section = createSection(FUNC, encodeVector(return_type_code));
Code *exp = encodeString ("main"); Code* exp = encodeString("main");
append_byte (exp, EXPORT_FUNC); append_byte(exp, EXPORT_FUNC);
append_byte (exp, 0x00); append_byte(exp, 0x00);
exp->override = true; exp->override = true;
exp->override_count = 1; exp->override_count = 1;
Code *export_section = createSection (EXPORT, encodeVector (exp)); Code* export_section = createSection(EXPORT, encodeVector(exp));
Code *code = (Code *)calloc (1, sizeof (Code)); // Code* code = (Code*)calloc(1, sizeof(Code));
append_byte (code, LOCAL_GET); // append_byte(code, F64_CONST);
append (code, unsignedLEB128 (0)); // append_f64(code, 1.0);
append_byte (code, LOCAL_GET); // append_byte(code, F64_CONST);
append (code, unsignedLEB128 (1)); // append_f64(code, 2.0);
append_byte (code, F32_ADD); // append_byte(code, F64_ADD);
Code *body = (Code *)calloc (1, sizeof (Code)); Code* code = (Code*)calloc(1, sizeof(Code));
append_byte (body, EMPTY_ARRAY); initTokenizer(buffer);
append (body, code); Token t = nextToken();
append_byte (body, END); do {
// debug_printToken(t);
Code *function_body = (Code *)calloc (1, sizeof (Code)); switch (t.type) {
append (function_body, encodeVector (body)); case TOKEN_LEFT_PAREN:
function_body->override = true; break;
function_body->override_count = 1; case TOKEN_RIGHT_PAREN:
Code *code_section = createSection (CODE, encodeVector (function_body)); break;
case TOKEN_LEFT_BRACE:
Code *tape = (Code *)malloc (sizeof (Code)); break;
tape->cells = calloc (8, sizeof (uint8_t)); case TOKEN_RIGHT_BRACE:
tape->cells[0] = 0; break;
tape->cells[1] = 'a'; case TOKEN_TILDE:
tape->cells[2] = 's'; break;
tape->cells[3] = 'm'; case TOKEN_SLASH:
tape->cells[4] = 1; break;
tape->cells[5] = 0; case TOKEN_MINUS:
tape->cells[6] = 0; break;
tape->cells[7] = 0; case TOKEN_IDENTIFIER:
tape->count = 8; break;
case TOKEN_STRING:
append (tape, type_section); break;
append (tape, func_section); case TOKEN_FLOAT:
append (tape, export_section); strncpy(number, t.start, t.length);
append (tape, code_section); number[t.length + 1] = '\0';
append_byte(code, F64_CONST);
return tape; append_f64(code, strtod(number, NULL));
} break;
case TOKEN_LIST:
Code * break;
demo_add_compile () case TOKEN_ERROR:
{ break;
Code *add_args_code = (Code *)calloc (1, sizeof (Code)); case TOKEN_FALSE:
Code *add_return_code = (Code *)calloc (1, sizeof (Code)); break;
append_byte (add_return_code, f64); case TOKEN_TRUE:
break;
Code *add_function_type = (Code *)calloc (1, sizeof (Code)); case TOKEN_PI:
append_byte (add_function_type, FUNCTION); break;
case TOKEN_E:
append (add_function_type, encodeVector (add_args_code)); break;
append (add_function_type, encodeVector (add_return_code)); case TOKEN_EOF:
add_function_type->override = true; break;
add_function_type->override_count = 1; case TOKEN_POP:
break;
Code *type_section = createSection (TYPE, encodeVector (add_function_type)); case TOKEN_DUP:
break;
Code *return_type_code = (Code *)calloc (1, sizeof (Code)); case TOKEN_EXCH:
append_byte (return_type_code, 0x00); break;
return_type_code->override = true; case TOKEN_CLEAR:
return_type_code->override_count = 1; break;
case TOKEN_REMEMBER:
Code *func_section = createSection (FUNC, encodeVector (return_type_code)); break;
case TOKEN_FORGET:
Code *exp = encodeString ("main"); break;
append_byte (exp, EXPORT_FUNC); case TOKEN_DUMP:
append_byte (exp, 0x00); break;
exp->override = true; case TOKEN_NAME:
exp->override_count = 1; break;
Code *export_section = createSection (EXPORT, encodeVector (exp)); case TOKEN_SET:
break;
Code *code = (Code *)calloc (1, sizeof (Code)); case TOKEN_IFYES:
append_byte (code, F64_CONST); break;
append_f64 (code, 6.7); case TOKEN_IFNO:
append_byte (code, F64_CONST); break;
append_f64 (code, 8.5); case TOKEN_CHOOSE:
append_byte (code, F64_ADD); break;
case TOKEN_EVAL:
Code *body = (Code *)calloc (1, sizeof (Code)); break;
append_byte (body, EMPTY_ARRAY); case TOKEN_ESCAPE:
append (body, code); break;
append_byte (body, END); case TOKEN_REPEAT:
break;
Code *function_body = (Code *)calloc (1, sizeof (Code)); case TOKEN_SPLIT:
append (function_body, encodeVector (body)); break;
function_body->override = true; case TOKEN_CONS:
function_body->override_count = 1; break;
Code *code_section = createSection (CODE, encodeVector (function_body)); case TOKEN_SHATTER:
break;
Code *tape = (Code *)malloc (sizeof (Code)); case TOKEN_EMPTY:
tape->cells = calloc (8, sizeof (uint8_t)); break;
tape->cells[0] = 0; case TOKEN_COMPOSE:
tape->cells[1] = 'a'; break;
tape->cells[2] = 's'; case TOKEN_STREQ:
tape->cells[3] = 'm'; break;
tape->cells[4] = 1; case TOKEN_STRCUT:
tape->cells[5] = 0; break;
tape->cells[6] = 0; case TOKEN_STRMEASURE:
tape->cells[7] = 0; break;
tape->count = 8; case TOKEN_STRTIE:
break;
append (tape, type_section); case TOKEN_EXPLODE:
append (tape, func_section); break;
append (tape, export_section); case TOKEN_ADD:
append (tape, code_section); append_byte(code, F64_ADD);
break;
return tape; case TOKEN_SUB:
} break;
case TOKEN_MUL:
Code * break;
compile (char *buffer) case TOKEN_DIV:
{ break;
Code *tape = (Code *)malloc (sizeof (Code)); case TOKEN_IDIV:
tape->cells = calloc (8, sizeof (uint8_t)); break;
tape->cells[0] = 0; case TOKEN_MOD:
tape->cells[1] = 'a'; break;
tape->cells[2] = 's'; case TOKEN_POW:
tape->cells[3] = 'm'; break;
tape->cells[4] = 1; case TOKEN_SQRT:
tape->cells[5] = 0; break;
tape->cells[6] = 0; case TOKEN_ADD1:
tape->cells[7] = 0; break;
tape->count = 8; case TOKEN_SUB1:
break;
initTokenizer (buffer); case TOKEN_SIN:
Token t = nextToken (); break;
while (t.type != TOKEN_EOF) case TOKEN_COS:
{ break;
debug_printToken (t); case TOKEN_TAN:
t = nextToken (); break;
case TOKEN_ATAN:
break;
case TOKEN_LN:
break;
case TOKEN_LOG:
break;
case TOKEN_LOG3:
break;
case TOKEN_CLIP:
break;
case TOKEN_SMOOTH:
break;
case TOKEN_HOWMUCH:
break;
case TOKEN_SETRAND:
break;
case TOKEN_RAND:
break;
case TOKEN_INT:
break;
case TOKEN_NUMBERIZE:
break;
case TOKEN_ISOLATE:
break;
case TOKEN_MIX:
break;
case TOKEN_CONTRADICT:
break;
case TOKEN_COMPL:
break;
case TOKEN_SHIFTRIGHT:
break;
case TOKEN_SHIFTLEFT:
break;
case TOKEN_GT:
break;
case TOKEN_LT:
break;
case TOKEN_EQ:
break;
case TOKEN_GE:
break;
case TOKEN_LE:
break;
case TOKEN_NE:
break;
case TOKEN_NULL:
break;
case TOKEN_NEGATIVE:
break;
case TOKEN_ISNULL:
break;
case TOKEN_ISINT:
break;
case TOKEN_ISNUMBER:
break;
case TOKEN_AND:
break;
case TOKEN_OR:
break;
case TOKEN_XOR:
break;
case TOKEN_DISP:
break;
case TOKEN_LISTEN:
break;
case TOKEN_COMPLAIN:
break;
case TOKEN_TIME:
break;
case TOKEN_GARBAGE_COLLECT:
break;
} }
t = nextToken();
} while (t.type != TOKEN_EOF);
Code* body = (Code*)calloc(1, sizeof(Code));
append_byte(body, EMPTY_ARRAY);
append(body, code);
append_byte(body, END);
Code* function_body = (Code*)calloc(1, sizeof(Code));
append(function_body, encodeVector(body));
function_body->override = true;
function_body->override_count = 1;
Code* code_section = createSection(CODE, encodeVector(function_body));
Code* tape = (Code*)malloc(sizeof(Code));
tape->cells = calloc(8, sizeof(uint8_t));
tape->cells[0] = 0;
tape->cells[1] = 'a';
tape->cells[2] = 's';
tape->cells[3] = 'm';
tape->cells[4] = 1;
tape->cells[5] = 0;
tape->cells[6] = 0;
tape->cells[7] = 0;
tape->count = 8;
append(tape, type_section);
append(tape, func_section);
append(tape, export_section);
append(tape, code_section);
return tape; return tape;
} }

36
main.c
View File

@ -3,35 +3,33 @@
#include "tokenizer.h" #include "tokenizer.h"
int int
main (int argc, char **argv) main(int argc, char** argv)
{ {
if (argc != 3) if (argc != 3) {
{ printf("usage: varaq input.vq output.wasm");
printf ("usage: varaq input.vq output.wasm");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
char *buffer; char* buffer;
int length = 0; int length = 0;
int sp = open (argv[1], O_RDONLY); int sp = open(argv[1], O_RDONLY);
if (sp) if (sp) {
{ length = lseek(sp, (size_t)0, SEEK_END);
length = lseek (sp, (size_t)0, SEEK_END); lseek(sp, (size_t)0, SEEK_SET);
lseek (sp, (size_t)0, SEEK_SET); buffer = malloc(length);
buffer = malloc (length); if (buffer) {
if (buffer) read(sp, buffer, length * sizeof(char));
{
read (sp, buffer, length * sizeof (char));
} }
close (sp); close(sp);
} }
Code *prog = compile (buffer); initMap();
Code* prog = compile(buffer);
int tp = open (argv[2], O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH); int tp = open(argv[2], O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
write (tp, prog->cells, prog->count); write(tp, prog->cells, prog->count);
close (tp); close(tp);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

19
mkfile Normal file
View File

@ -0,0 +1,19 @@
</$objtype/mkfile
prog: main.$O compiler.$O tokenizer.$O
$LD $LDFLAGS -o $target $prereq
%.$O: %.c
$CC $CFLAGS $stem.c
install: $O.out
cp $O.out /$objtype/bin/varaq
installall:
for(objtype in $CPUS) mk install
clean:V:
rm -f [$OS].out *.[$OS] y.tab.?

File diff suppressed because it is too large Load Diff