diff --git a/compiler.c b/compiler.c index 6434932..7ef8322 100644 --- a/compiler.c +++ b/compiler.c @@ -3,6 +3,8 @@ #include "common.h" #include "tokenizer.h" +#include + Code* signedLEB128(size_t num) { @@ -229,12 +231,17 @@ compile(char* buffer) case TOKEN_TRUE: break; case TOKEN_PI: + append_byte(code, F64_CONST); + append_f64(code, M_PI); break; case TOKEN_E: + append_byte(code, F64_CONST); + append_f64(code, M_E); break; case TOKEN_EOF: break; case TOKEN_POP: + append_byte(code, DROP); break; case TOKEN_DUP: break; @@ -288,10 +295,13 @@ compile(char* buffer) append_byte(code, F64_ADD); break; case TOKEN_SUB: + append_byte(code, F64_SUB); break; case TOKEN_MUL: + append_byte(code, F64_MUL); break; case TOKEN_DIV: + append_byte(code, F64_DIV); break; case TOKEN_IDIV: break; @@ -300,10 +310,17 @@ compile(char* buffer) case TOKEN_POW: break; case TOKEN_SQRT: + append_byte(code, F64_SQRT); break; case TOKEN_ADD1: + append_byte(code, F64_CONST); + append_f64(code, 1.0); + append_byte(code, F64_ADD); break; case TOKEN_SUB1: + append_byte(code, F64_CONST); + append_f64(code, 1.0); + append_byte(code, F64_SUB); break; case TOKEN_SIN: break; diff --git a/main.c b/main.c index bf818cf..360b4e5 100644 --- a/main.c +++ b/main.c @@ -27,7 +27,8 @@ main(int argc, char** argv) 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 | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH); write(tp, prog->cells, prog->count); close(tp); diff --git a/tokenizer.c b/tokenizer.c index 31a0de9..0fd6159 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -360,14 +360,20 @@ checkKeyword(int start, int length, char* rest, TokenType type) return TOKEN_IDENTIFIER; } +static char* +currentTokenToS() +{ + int32_t size = tokenizer.current - tokenizer.start; + char* str = (char*)malloc(sizeof(size)); + strncpy(str, tokenizer.start, size); + str[size] = '\0'; + return str; +} + static TokenType identifierType() { - char* check; - int32_t size = tokenizer.current - tokenizer.start; - check = (char*)malloc(sizeof(size)); - strncpy(check, tokenizer.start, size); - check[size] = '\0'; + char* check = currentTokenToS(); TokenType t = get(check); free(check); return t; @@ -456,11 +462,7 @@ nextToken() void debug_printToken(Token t) { - char* str; - int32_t size = tokenizer.current - tokenizer.start; - str = (char*)malloc(sizeof(size)); - strncpy(str, tokenizer.start, size); - str[size] = '\0'; + char* str = currentTokenToS(); switch (t.type) { case TOKEN_LEFT_PAREN: