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 "tokenizer.h"
#include <math.h>
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;

3
main.c
View File

@ -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);

View File

@ -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: