Add a few more ops, refactor.

This commit is contained in:
zongor 2023-03-04 13:37:34 -05:00
parent ef263d9cc4
commit e4ec0f5022
3 changed files with 31 additions and 11 deletions

View File

@ -3,6 +3,8 @@
#include "common.h" #include "common.h"
#include "tokenizer.h" #include "tokenizer.h"
#include <math.h>
Code* Code*
signedLEB128(size_t num) signedLEB128(size_t num)
{ {
@ -229,12 +231,17 @@ compile(char* buffer)
case TOKEN_TRUE: case TOKEN_TRUE:
break; break;
case TOKEN_PI: case TOKEN_PI:
append_byte(code, F64_CONST);
append_f64(code, M_PI);
break; break;
case TOKEN_E: case TOKEN_E:
append_byte(code, F64_CONST);
append_f64(code, M_E);
break; break;
case TOKEN_EOF: case TOKEN_EOF:
break; break;
case TOKEN_POP: case TOKEN_POP:
append_byte(code, DROP);
break; break;
case TOKEN_DUP: case TOKEN_DUP:
break; break;
@ -288,10 +295,13 @@ compile(char* buffer)
append_byte(code, F64_ADD); append_byte(code, F64_ADD);
break; break;
case TOKEN_SUB: case TOKEN_SUB:
append_byte(code, F64_SUB);
break; break;
case TOKEN_MUL: case TOKEN_MUL:
append_byte(code, F64_MUL);
break; break;
case TOKEN_DIV: case TOKEN_DIV:
append_byte(code, F64_DIV);
break; break;
case TOKEN_IDIV: case TOKEN_IDIV:
break; break;
@ -300,10 +310,17 @@ compile(char* buffer)
case TOKEN_POW: case TOKEN_POW:
break; break;
case TOKEN_SQRT: case TOKEN_SQRT:
append_byte(code, F64_SQRT);
break; break;
case TOKEN_ADD1: case TOKEN_ADD1:
append_byte(code, F64_CONST);
append_f64(code, 1.0);
append_byte(code, F64_ADD);
break; break;
case TOKEN_SUB1: case TOKEN_SUB1:
append_byte(code, F64_CONST);
append_f64(code, 1.0);
append_byte(code, F64_SUB);
break; break;
case TOKEN_SIN: case TOKEN_SIN:
break; break;

3
main.c
View File

@ -27,7 +27,8 @@ main(int argc, char** argv)
initMap(); initMap();
Code* prog = compile(buffer); 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); write(tp, prog->cells, prog->count);
close(tp); close(tp);

View File

@ -360,14 +360,20 @@ checkKeyword(int start, int length, char* rest, TokenType type)
return TOKEN_IDENTIFIER; 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 static TokenType
identifierType() identifierType()
{ {
char* check; char* check = currentTokenToS();
int32_t size = tokenizer.current - tokenizer.start;
check = (char*)malloc(sizeof(size));
strncpy(check, tokenizer.start, size);
check[size] = '\0';
TokenType t = get(check); TokenType t = get(check);
free(check); free(check);
return t; return t;
@ -456,11 +462,7 @@ nextToken()
void void
debug_printToken(Token t) debug_printToken(Token t)
{ {
char* str; char* str = currentTokenToS();
int32_t size = tokenizer.current - tokenizer.start;
str = (char*)malloc(sizeof(size));
strncpy(str, tokenizer.start, size);
str[size] = '\0';
switch (t.type) { switch (t.type) {
case TOKEN_LEFT_PAREN: case TOKEN_LEFT_PAREN: