Compare commits

...

10 Commits

Author SHA1 Message Date
cba9cdca33 error.c: add missing space to error format 2026-06-10 22:28:28 +03:00
71b0c7bc81 merge upstream 2026-06-10 21:26:16 +02:00
c29c2796d7 compiler: start work on binary expressions & stuff
Reviewed-on: NeoFlock/noom#7
Co-authored-by: tema5002 <tema5002@tuta.io>
Co-committed-by: tema5002 <tema5002@tuta.io>
2026-06-09 20:11:29 +02:00
28e3508052 remove something i forgot already 2026-06-09 21:04:25 +03:00
efe86cebb4 surpass the non void function not having return compiler warning 2026-06-09 21:04:02 +03:00
ee989fafa3 compiler.c and other minor tweaks 2026-06-09 20:38:54 +03:00
8709efda7d error: program name parameter for noom_format_error
Reviewed-on: NeoFlock/noom#6
Co-authored-by: tema5002 <tema5002@tuta.io>
Co-committed-by: tema5002 <tema5002@tuta.io>
2026-06-09 13:04:05 +02:00
5ff0594791 compiler: basic code in place + all: rename streql to memeq
Reviewed-on: NeoFlock/noom#5
Co-authored-by: tema5002 <tema5002@tuta.io>
Co-committed-by: tema5002 <tema5002@tuta.io>
2026-06-08 22:13:05 +02:00
be75ccd6f8 parser: apparently i don't know how realloc works 2026-06-02 21:04:34 +02:00
269d2c8ae9 documented the vm more 2026-05-27 15:43:13 +02:00
14 changed files with 647 additions and 182 deletions

View File

@@ -22,6 +22,7 @@ pub fn build(b: *std.Build) void {
"src/parser.c", "src/parser.c",
"src/compiler.c", "src/compiler.c",
"src/vm.c", "src/vm.c",
"src/error.c",
"src/main.c", "src/main.c",
} }
}); });

View File

@@ -1 +0,0 @@
local t <close> = {6; 2}

View File

@@ -0,0 +1,365 @@
#include "noom.h"
#include "compiler.h"
#include "helper.h"
// COMPILATION!!!!!!!!!!!!!!!!!!!
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣴⢲⣞⣭⣟⣿⣻⣷⢶⢦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⣤⣞⣵⢯⣿⣞⣷⣿⣷⣿⣿⣿⣿⣜⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⣤⣶⢿⣾⣽⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣯⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⡤⢿⣿⣿⣿⣿⣯⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⢫⡀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⢆⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣯⡗⡇⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⢠⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣻⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⢰⣾⣿⣿⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡑⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠋⠉⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡷⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠻⠿⢿⣿⠟⠛⠛⠻⣿⣿⣿⣿⡿⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣿⣇⠀⠀⠀⠉⢿⡟⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⠀⠀⠀⠀⣼⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠁⠀⠀⠀⢨⣿⡷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⣿⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⡀⠀⢀⠀⠀⠀⠀⠀⠀⠀⠀⢼⣿⣧⡴⢤⣶⡴⢻⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠘⠛⠿⠻⠷⠾⠶⠷⠾⠶⡷⣿⣿⣀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⣀⠴⠮⠃⠉⠛⢷⣦⣠⡄⠀⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⣠⠶⠘⠁⠀⠀⠀⠀⠀⠙⢮⡿⣧⢾⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⣤⠓⠈⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣈⣽⡿⢿⣤⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⢀⣠⣠⣴⣮⠾⠟⠋⠉⢹⠌⠈⠙⠻⢷⣶⣤⣤⣀⡀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠙⠛⠛⠉⠁⠀⠀⠀⠀⠀⠀⡘⠂⠀⠀⠀⠀⠈⠉⠛⠛⠻⠿⠷⠦
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡘⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
void noomC_compiler_init(noomC_Compiler *compiler) {
compiler->parent = 0;
compiler->localc = 0;
compiler->localcap = 0;
compiler->locals = 0;
compiler->constidx = 0;
compiler->constc = 0;
compiler->constcap = 0;
}
static noom_Exit noomC_emit(noomV_Function *func, const noomV_Inst inst) {
func->code = noom_realloc(func->code, sizeof(noomV_Inst) * (func->codesize + 1));
if (func->code == 0) return NOOM_ENOMEM;
func->code[func->codesize++] = inst;
return NOOM_OK;
}
static noom_Exit noomC_emit_ABC(noomV_Function *func, const noomV_Opcode op, const unsigned char a, const unsigned char b, const unsigned char c) {
return noomC_emit(func, (noomV_Inst){.op = op, .a = a, .b = b, .c = c});
}
static noom_Exit noomC_emit_Aus(noomV_Function *func, const noomV_Opcode op, const unsigned char a, const unsigned short us) {
return noomC_emit(func, (noomV_Inst){.op = op, .a = a, .us = us});
}
static noom_Exit noomC_addconst(noomV_Function *func, const noomV_Value val) {
if (func->constsize == NOOM_USHORT_MAX) return NOOM_PLEASEHELPMEIAMSCARED;
func->consts = noom_realloc(func->consts, sizeof(noomV_Value) * (func->constsize + 1));
if (func->consts == 0) return NOOM_ENOMEM;
func->consts[func->constsize++] = val;
return NOOM_OK;
}
// TODO get rid of these two vvvv
static noom_Exit noomC_addconst_nil(noomV_Function *func) {
return noomC_addconst(func, (noomV_Value){.tag = NOOMV_VNIL, .autoclose = 0, .isptr = 0});
}
static noom_Exit noomC_addconst_bool(noomV_Function *func, const noom_bool_t b) {
return noomC_addconst(func, (noomV_Value){.tag = NOOMV_VBOOL, .autoclose = 0, .isptr = 0, .boolean = b});
}
// yeah ^^^^^^^^^
static noom_Exit noomC_addconst_str(noomV_Function *func, noom_LuaVM *vm, const char *str, const noom_uint_t len) {
noomV_String *s = noomV_allocStr(vm, str, len);
if (s == 0) return NOOM_ENOMEM;
return noomC_addconst(func, (noomV_Value){.tag = NOOMV_VOBJ, .autoclose = 0, .isptr = 0, .obj = (noomV_Object *)s});
}
static noomL_Token noomC_token_at(const noomP_Parser *parser, noom_uint_t offset) {
noomL_Token token;
noomL_lex(parser->code, offset, &token, parser->version);
return token;
}
// There is a high chance I forgot something here
static noomV_Opcode noomC_what_bop_is_this(const noomP_Parser *parser, noom_uint_t offset) {
// I AM THE LEXER NOW!!!!!!!!!
if (noom_startswith(parser->code + offset, "==")) return NOOMV_INSTR_EQ;
if (noom_startswith(parser->code + offset, "~=")) return NOOMV_INSTR_NEQ;
if (noom_startswith(parser->code + offset, "~=")) return NOOMV_INSTR_LTE;
if (noom_startswith(parser->code + offset, "~=")) return NOOMV_INSTR_GTE;
if (noom_startswith(parser->code + offset, "..")) return NOOMV_INSTR_CONCAT;
if (parser->version >= NOOM_VERSION_53) {
// Fucking atom forgot to put make an instruction
// update no i am unable to read
if (noom_startswith(parser->code + offset, "//")) return NOOMV_INSTR_FLOOR_DIV;
if (noom_startswith(parser->code + offset, ">>")) return NOOMV_INSTR_RSHIFT;
if (noom_startswith(parser->code + offset, "<<")) return NOOMV_INSTR_LSHIFT;
if (noom_startswith(parser->code + offset, "&")) return NOOMV_INSTR_BAND;
if (noom_startswith(parser->code + offset, "|")) return NOOMV_INSTR_BOR;
if (noom_startswith(parser->code + offset, "~")) return NOOMV_INSTR_BXOR;
}
if (noom_startswith(parser->code + offset, "+")) return NOOMV_INSTR_ADD;
if (noom_startswith(parser->code + offset, "-")) return NOOMV_INSTR_SUB;
if (noom_startswith(parser->code + offset, "*")) return NOOMV_INSTR_MUL;
if (noom_startswith(parser->code + offset, "/")) return NOOMV_INSTR_DIV;
if (noom_startswith(parser->code + offset, "%")) return NOOMV_INSTR_MOD;
if (noom_startswith(parser->code + offset, "^")) return NOOMV_INSTR_POW;
if (noom_startswith(parser->code + offset, "<")) return NOOMV_INSTR_LT;
if (noom_startswith(parser->code + offset, ">")) return NOOMV_INSTR_GT;
return 0;
}
// mmap ahh function 🥀🥀🥀🥀🥀🥀🥀
// What 6 argument syscalls there even are other than mmap and clone and is there anything longer
static noom_Exit noomC_compile_expr(
noom_LuaVM *vm,
noomC_Compiler *compiler,
const noomP_Parser *parser,
noomV_Function *func,
const noomP_Node *node
) {
// Baba is You OST is a very cool soundtrack to code to Can recommend
if (node->type == NOOMP_NODE_NILLITERAL) {
unsigned char fucking_destination = compiler->curstack++;
unsigned short fucking_constant = func->constsize;
noom_Exit result = noomC_addconst_nil(func);
if (result != NOOM_OK) return result;
return noomC_emit_Aus(func, NOOMV_INSTR_LOADC, fucking_destination, fucking_constant);
}
if (node->type == NOOMP_NODE_BOOLEANLITERAL) {
unsigned char fucking_destination = compiler->curstack++;
unsigned short fucking_constant = func->constsize;
noomL_Token bool_token = noomC_token_at(parser, node->source_offset);
noom_bool_t val = noom_memeq(parser->code + bool_token.offset, bool_token.length, "true", 4);
noom_Exit result = noomC_addconst_bool(func, val);
if (result != NOOM_OK) return result;
return noomC_emit_Aus(func, NOOMV_INSTR_LOADC, fucking_destination, fucking_constant);
}
if (node->type == NOOMP_NODE_NUMBERLITERAL) {
unsigned char fucking_destination = compiler->curstack++;
return NOOM_PLEASEHELPMEIAMSCARED; // 😭😭😭😭😭😭😭😭
}
if (node->type == NOOMP_NODE_STRINGLITERAL) {
unsigned char fucking_destination = compiler->curstack++;
noom_Exit result = noomC_addconst_str(func, vm, "but DID YOU KNOW", 16);
return result;
}
if (node->type == NOOMP_NODE_BINARYOPERATOR) {
unsigned char fucking_destination = compiler->curstack++;
if (node->subnodec != 2) return NOOM_EINTERNAL;
const unsigned char B = compiler->curstack++;
noom_Exit result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]);
if (result != NOOM_OK) return result;
const unsigned char C = compiler->curstack++;
result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[1]);
if (result != NOOM_OK) return result;
return noomC_emit_ABC(func, noomC_what_bop_is_this(parser, node->source_offset), fucking_destination, B, C);
}
if (node->type == NOOMP_NODE_CALL) {
unsigned char fucking_destination = compiler->curstack;
// Here we don't fucking increment this fucking destination
// Because the fucking function call fucking increments it fucking anyway
for (int i = 1; i < node->subnodec; i++) {
// push node->subnodes[i] to stack or something
}
return noomC_emit_Aus(func, NOOMV_INSTR_JMP, 0, /* where do i start......... */ 0);
}
if (node->type == NOOMP_NODE_METHODCALL) {
unsigned char fucking_destination = compiler->curstack;
// Here we don't fucking increment this fucking destination
// Because the fucking function call fucking increments it fucking anyway
// push parent value... probably already processed below if you make it i = 0
for (int i = 1; i < node->subnodec; i++) {
// push node->subnodes[i] to stack or something
}
return noomC_emit_Aus(func, NOOMV_INSTR_JMP, 0, /* where do i start......... */ 0);
}
if (node->type == NOOMP_NODE_GETFIELD) {
unsigned char fucking_destination = compiler->curstack++;
if (node->subnodec != 2) return NOOM_EINTERNAL;
unsigned char B = compiler->curstack;
noom_Exit result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]);
if (result != NOOM_OK) return result;
unsigned char C = compiler->curstack;
unsigned short C_constant = func->constsize;
noomL_Token field_token = noomC_token_at(parser, node->subnodes[1]->source_offset);
result = noomC_addconst_str(func, vm, parser->code + field_token.offset, field_token.length);
if (result != NOOM_OK) return result;
result = noomC_emit_Aus(func, NOOMV_INSTR_LOADC, C, C_constant);
if (result != NOOM_OK) return result;
return noomC_emit_ABC(func, NOOMV_INSTR_GET, fucking_destination, B, C);
}
return NOOM_EINTERNAL;
}
static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *compiler, const noomP_Parser *parser, noomV_Function *func, const noomP_Node *node);
static noom_Exit noomC_compile_block(noom_LuaVM *vm, noomC_Compiler *compiler, const noomP_Parser *parser, noomV_Function *func, const noomP_Node *node) {
noom_Exit r = NOOM_OK;
for (noom_uint_t i = 0; i < node->subnodec; i++) {
r = noomC_add_stuff_to_function(vm, compiler, parser, func, node->subnodes[i]);
if (r != NOOM_OK) break;
}
return r;
}
// They say you use only 10% of your brain
// Keep it
// If you think too much you will get headaches
// I remember atom said there is a memory leak somewhere but I don't rememeememember where uhhhhhhhhhhhhhhhhh
static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *compiler, const noomP_Parser *parser, noomV_Function *func, const noomP_Node *node) {
// local name <attr> = expression i think
if (node->type == NOOMP_NODE_LOCALDECLARATION) {
if (node->subnodec != 1 && node->subnodec != 2) return NOOM_EINTERNAL;
const noomP_Node *varname_node = node->subnodes[0];
if (varname_node->type != NOOMP_NODE_ASSIGNPLACE) return NOOM_EINTERNAL;
noomC_Local local;
local.startpc = func->codesize;
local.endpc = 0;
local.dropped = 0; // FIXME?
local.close = 0;
local.constant = 0;
local.name = noom_strndup(parser->code + varname_node->source_offset, varname_node->source_offset);
for (noom_uint_t i = 0; i < varname_node->subnodec; i++) {
if (varname_node->subnodes[i]->type != NOOMP_NODE_ATTRIBUTE) return NOOM_EINTERNAL;
noomL_Token attr_token = noomC_token_at(parser, varname_node->subnodes[i]->source_offset);
const char *ap = parser->code + attr_token.offset;
noom_uint_t al = attr_token.length;
if (noom_memeq(ap, al, "close", 5)) {
if (local.close) return NOOM_EINTERNAL;
local.close = 1;
}
else if (noom_memeq(ap, al, "const", 5)) {
if (local.constant) return NOOM_EINTERNAL;
local.constant = 1;
}
else {
return NOOM_EINTERNAL;
}
}
const unsigned destination = compiler->curstack;
if (compiler->curstack++ == NOOM_MAXSTACK) return NOOM_ENOSTACK;
if (node->subnodec == 1) {
// according to atom this should be nil
const unsigned short constptr = func->constsize;
noom_Exit result = noomC_addconst_nil(func);
if (result != NOOM_OK) return result;
result = noomC_emit_Aus(func, NOOMV_INSTR_LOADC, destination, constptr);
if (result != NOOM_OK) return result;
}
else {
const noomP_Node *value_node = node->subnodes[1];
const unsigned saved = compiler->curstack;
compiler->curstack = destination;
const noom_Exit r = noomC_compile_expr(vm, compiler, parser, func, value_node);
if (r != NOOM_OK) return r;
compiler->curstack = saved;
}
compiler->locals = noom_realloc(compiler->locals, sizeof(noomC_Local) * (compiler->localc + 1));
if (compiler->locals == 0) return NOOM_ENOMEM;
compiler->locals[compiler->localc++] = local;
// I guess???????
if (compiler->localc > compiler->localcap) compiler->localcap = compiler->localc;
return NOOM_OK;
}
// local function name(...) ... end
if (node->type == NOOMP_NODE_LOCALFUNCTIONDECLARATION) {
if (node->subnodec != 3) return NOOM_EINTERNAL;
// Funny staircase huh
const noomP_Node *varname_node = node->subnodes[0];
const noomP_Node *params_node = node->subnodes[1];
const noomP_Node *block_node = node->subnodes[2];
if (varname_node->type != NOOMP_NODE_VARNAME) return NOOM_EINTERNAL;
if (params_node->type != NOOMP_NODE_FUNCTIONPARAMETERS) return NOOM_EINTERNAL;
if (block_node->type != NOOMP_NODE_BLOCK) return NOOM_EINTERNAL;
const noomL_Token name_token = noomC_token_at(parser, varname_node->source_offset);
noomV_Function *proto = noomV_allocFunc(vm, parser->code + name_token.offset, name_token.length);
if (proto == 0) return NOOM_ENOMEM;
proto->argc = (unsigned char)params_node->subnodec;
noomC_Compiler child;
noomC_compiler_init(&child);
child.parent = compiler; // Is this correct????? Is this correct????????????????? It probably is
child.curstack = proto->argc;
const noom_Exit result = noomC_compile_block(vm, &child, parser, proto, block_node);
if (result != NOOM_OK) return result;
func->protos = noom_realloc(func->protos, sizeof(noomV_Function *) * (func->protosize + 1));
if (func->protos == 0) return NOOM_ENOMEM;
func->protos[func->protosize++] = proto;
noomC_Local local;
local.startpc = proto->codesize;
local.endpc = 0;
local.dropped = 0; // FIXME?
local.close = 0;
local.constant = 0;
local.name = noom_strndup(parser->code + varname_node->source_offset, varname_node->source_offset);
compiler->locals = noom_realloc(compiler->locals, sizeof(noomC_Local) * (compiler->localc + 1));
if (compiler->locals == 0) return NOOM_ENOMEM;
compiler->locals[compiler->localc++] = local;
// I guess???????
if (compiler->localc > compiler->localcap) compiler->localcap = compiler->localc;
return NOOM_OK;
}
// TODO
if (node->type == NOOMP_NODE_RETURN) {
noomC_emit_Aus(func, NOOMV_INSTR_NOP, 0, 0);
return NOOM_OK;
}
if (node->type == NOOMP_NODE_DOBLOCK || node->type == NOOMP_NODE_BLOCK) {
return noomC_compile_block(vm, compiler, parser, func, node);
}
return NOOM_EINTERNAL;
}
noom_Exit noomC_compile(noom_LuaVM *vm, const noomP_Parser *parser, const noomP_Node *node) {
if (node->type != NOOMP_NODE_PROGRAM) return NOOM_EINTERNAL;
noomV_Function* program = noomV_allocFunc(vm, "main", 4);
noomC_Compiler compiler;
noomC_compiler_init(&compiler);
noom_Exit status = noomC_compile_block(vm, &compiler, parser, program, node);
if (status != NOOM_OK) return status;
if (vm->mainThread->stacklen == vm->mainThread->stackcap) {
vm->mainThread->stackcap = vm->mainThread->stackcap ? vm->mainThread->stackcap * 2 : 16;
vm->mainThread->stack = noom_realloc(vm->mainThread->stack, sizeof(noomV_Value) * vm->mainThread->stackcap);
}
vm->mainThread->stack[vm->mainThread->stacklen++] =
(noomV_Value){.tag = NOOMV_VOBJ, .autoclose = 0, .isptr = 0, .obj = (noomV_Object*)program};
return NOOM_OK;
}

View File

@@ -5,10 +5,10 @@
typedef struct noomC_Local { typedef struct noomC_Local {
unsigned int startpc; unsigned int startpc;
unsigned int endpc; unsigned int endpc;
char *name; const char *name;
bool dropped; noom_bool_t dropped;
bool constant; noom_bool_t constant;
bool close; noom_bool_t close;
} noomC_Local; } noomC_Local;
typedef struct noomC_Compiler { typedef struct noomC_Compiler {
@@ -23,5 +23,7 @@ typedef struct noomC_Compiler {
noomC_Local *locals; noomC_Local *locals;
} noomC_Compiler; } noomC_Compiler;
void noomC_compiler_init(noomC_Compiler* compiler);
// pushes the compiled function on the stack, or just crashes lol // pushes the compiled function on the stack, or just crashes lol
noom_Exit noomC_compile(noom_LuaVM *vm, noomP_Node *node); noom_Exit noomC_compile(noom_LuaVM *vm, const noomP_Parser *parser, const noomP_Node *node);

View File

@@ -1,7 +1,7 @@
#include "error.h" #include "error.h"
#include "helper.h" #include "helper.h"
noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uint_t buffer_size) { noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_name, char* buffer, noom_uint_t buffer_size) {
struct noom_error { struct noom_error {
const char* s; const char* s;
int near; // 0:none 1: near '%s'\n 2: '%s'\n int near; // 0:none 1: near '%s'\n 2: '%s'\n
@@ -96,28 +96,31 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin
noom_uint_t space = 0; noom_uint_t space = 0;
space = if (program_name) space +=
sizeof("noom: ") - 1 + noom_strlen(program_name) +
sizeof(": ") - 1;
space +=
noom_strlen(parser->filename) + noom_strlen(parser->filename) +
sizeof(":") - 1 + sizeof(":") - 1 +
linedig + linedig +
sizeof(":") - 1 + sizeof(": ") - 1 +
noom_strlen(err.s) + noom_strlen(err.s) +
+ 1; // \0; + 1; // \0;
if (err.near) { if (err.near) space +=
space += ( (err.near == 1 ? sizeof(" near") - 1 : 0) +
(err.near == 1 ? sizeof(" near") - 1 : 0) + sizeof(" '") - 1 +
sizeof(" '") - 1 + parser->last_token_length +
parser->last_token_length + sizeof("'\n") - 1;
sizeof("'\n") - 1
);
}
return space; return space;
} }
noom_safe_strcpy(buffer, &pos, buffer_size, "noom: "); if (program_name) {
noom_safe_strcpy(buffer, &pos, buffer_size, program_name);
noom_safe_strcpy(buffer, &pos, buffer_size, ": ");
}
noom_safe_strcpy(buffer, &pos, buffer_size, parser->filename); noom_safe_strcpy(buffer, &pos, buffer_size, parser->filename);
noom_safe_strcpy(buffer, &pos, buffer_size, ":"); noom_safe_strcpy(buffer, &pos, buffer_size, ":");
@@ -141,7 +144,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin
buffer[pos++] = num_buf[i]; buffer[pos++] = num_buf[i];
} }
noom_safe_strcpy(buffer, &pos, buffer_size, ":"); noom_safe_strcpy(buffer, &pos, buffer_size, ": ");
noom_safe_strcpy(buffer, &pos, buffer_size, err.s); noom_safe_strcpy(buffer, &pos, buffer_size, err.s);
if (err.near) { if (err.near) {
if (err.near == 1) { if (err.near == 1) {

View File

@@ -3,4 +3,4 @@
#include "types.h" #include "types.h"
#include "parser.h" #include "parser.h"
noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uint_t buffer_size); noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_name, char* buffer, noom_uint_t buffer_size);

View File

@@ -1,4 +1,5 @@
#include "helper.h" #include "helper.h"
#include "types.h" #include "types.h"
int noom_startswith(const char* str, const char* compare) { int noom_startswith(const char* str, const char* compare) {
@@ -16,7 +17,7 @@ int noom_startswith(const char* str, const char* compare) {
return 0; // unreachable but whatevs return 0; // unreachable but whatevs
} }
int noom_streql(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t lenb) { int noom_memeq(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t lenb) {
if (lena != lenb) return 0; if (lena != lenb) return 0;
for (noom_uint_t i = 0; i < lena; i++) { for (noom_uint_t i = 0; i < lena; i++) {
@@ -53,6 +54,29 @@ void noom_safe_strcpy(char* buffer, noom_uint_t* pos, noom_uint_t buffer_size, c
} }
} }
char *noom_strndup(const char *s, const noom_uint_t len) {
char *whar = noom_alloc(len + 1);
if (whar == 0) return 0;
noom_memcpy(whar, s, len);
whar[len] = '\0';
return whar;
}
void noom_memcpy(void* dst, const void* src, noom_uint_t n) {
#ifdef __has_builtin
#if __has_builtin(__builtin_memcpy)
__builtin_memcpy(dst, src, n);
return;
#endif
#endif
unsigned char *d = dst;
const unsigned char *s = src;
while (n--) {
*d++ = *s++;
}
}
#include <stdlib.h> // TODO: remove #include <stdlib.h> // TODO: remove
void* noom_alloc(noom_uint_t size) { void* noom_alloc(noom_uint_t size) {

View File

@@ -1,10 +1,15 @@
#include "types.h" #include "types.h"
#define NOOM_USHORT_MAX 0xffff
int noom_startswith(const char* str, const char* compare); int noom_startswith(const char* str, const char* compare);
int noom_streql(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t lenb); // rename to something better? int noom_memeq(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t lenb);
noom_uint_t noom_strlen(const char *s); noom_uint_t noom_strlen(const char *s);
int noom_strcmp(const char *a, const char* b); int noom_strcmp(const char *a, const char* b);
void noom_safe_strcpy(char* buffer, noom_uint_t* pos, noom_uint_t buffer_size, const char* src); void noom_safe_strcpy(char* buffer, noom_uint_t* pos, noom_uint_t buffer_size, const char* src);
char *noom_strndup(const char *s, noom_uint_t len);
void noom_memcpy(void* dst, const void* src, noom_uint_t n);
void* noom_alloc(noom_uint_t size); void* noom_alloc(noom_uint_t size);
void noom_free(void* ptr); void noom_free(void* ptr);

View File

@@ -433,34 +433,34 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
} }
int noomL_iskeyword(const char* s, noom_uint_t len, noom_LuaVersion version) { int noomL_iskeyword(const char* s, noom_uint_t len, noom_LuaVersion version) {
if (noom_streql(s, len, "true", 4)) return 1; if (noom_memeq(s, len, "true", 4)) return 1;
if (noom_streql(s, len, "false", 5)) return 1; if (noom_memeq(s, len, "false", 5)) return 1;
if (noom_streql(s, len, "nil", 3)) return 1; if (noom_memeq(s, len, "nil", 3)) return 1;
if (noom_streql(s, len, "if", 2)) return 1; if (noom_memeq(s, len, "if", 2)) return 1;
if (noom_streql(s, len, "then", 4)) return 1; if (noom_memeq(s, len, "then", 4)) return 1;
if (noom_streql(s, len, "else", 4)) return 1; if (noom_memeq(s, len, "else", 4)) return 1;
if (noom_streql(s, len, "elseif", 6)) return 1; if (noom_memeq(s, len, "elseif", 6)) return 1;
if (noom_streql(s, len, "and", 3)) return 1; if (noom_memeq(s, len, "and", 3)) return 1;
if (noom_streql(s, len, "or", 2)) return 1; if (noom_memeq(s, len, "or", 2)) return 1;
if (noom_streql(s, len, "not", 3)) return 1; if (noom_memeq(s, len, "not", 3)) return 1;
if (noom_streql(s, len, "local", 5)) return 1; if (noom_memeq(s, len, "local", 5)) return 1;
if (noom_streql(s, len, "for", 3)) return 1; if (noom_memeq(s, len, "for", 3)) return 1;
if (noom_streql(s, len, "function", 8)) return 1; if (noom_memeq(s, len, "function", 8)) return 1;
if (noom_streql(s, len, "do", 2)) return 1; if (noom_memeq(s, len, "do", 2)) return 1;
if (noom_streql(s, len, "until", 5)) return 1; if (noom_memeq(s, len, "until", 5)) return 1;
if (noom_streql(s, len, "while", 5)) return 1; if (noom_memeq(s, len, "while", 5)) return 1;
if (noom_streql(s, len, "repeat", 6)) return 1; if (noom_memeq(s, len, "repeat", 6)) return 1;
if (noom_streql(s, len, "end", 3)) return 1; if (noom_memeq(s, len, "end", 3)) return 1;
if (noom_streql(s, len, "in", 2)) return 1; if (noom_memeq(s, len, "in", 2)) return 1;
if (noom_streql(s, len, "return", 6)) return 1; if (noom_memeq(s, len, "return", 6)) return 1;
if (noom_streql(s, len, "break", 5)) return 1; if (noom_memeq(s, len, "break", 5)) return 1;
if (version >= NOOM_VERSION_52) { if (version >= NOOM_VERSION_52) {
if (noom_streql(s, len, "goto", 4)) return 1; if (noom_memeq(s, len, "goto", 4)) return 1;
} }
return 0; return 0;
@@ -580,7 +580,6 @@ noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token,
} }
} }
// god damn it we errorrreed // god damn it we errorrreed
return NOOML_ERROR_UNKNOWN; return NOOML_ERROR_UNKNOWN;
} }

View File

@@ -7,6 +7,9 @@ void tab(noom_uint_t amount) {
amount *= 2; amount *= 2;
for (noom_uint_t i = 0; i < amount; i++) { for (noom_uint_t i = 0; i < amount; i++) {
putchar(' '); putchar(' ');
putchar(' ');
putchar(' ');
putchar(' ');
} }
} }
@@ -31,7 +34,7 @@ void print_node(const noomP_Node *node, noom_uint_t depth) {
printf("}\n"); printf("}\n");
} }
int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(const char *code, const char *filename) { int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(const char *code, const char *program_name, const char *filename) {
noomP_Parser parser; noomP_Parser parser;
noomP_Node *program; noomP_Node *program;
@@ -81,9 +84,9 @@ int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(
print_node(program, 0); print_node(program, 0);
} }
else { else {
noom_uint_t bleh = noom_format_error(&parser, NULL, 0); noom_uint_t bleh = noom_format_error(&parser, program_name, NULL, 0);
char* buf = noom_alloc(bleh); char* buf = noom_alloc(bleh);
noom_format_error(&parser, buf, bleh); noom_format_error(&parser, program_name, buf, bleh);
fputs(buf, stdout); fputs(buf, stdout);
noom_free(buf); noom_free(buf);
} }
@@ -234,23 +237,25 @@ int main(int argc, char **argv) {
} }
if (params.script_exec || params.script_path) { if (params.script_exec || params.script_path) {
if (params.script_exec) { if (params.script_exec) {
return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(params.script_exec, "(command line)"); return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(params.script_exec, argv[0], "(command line)");
} }
char* code = read_file(params.script_path); char* code = read_file(params.script_path);
if (code == 0) return 1; if (code == 0) return 1;
return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, params.script_path); int offset = 0;
if (code[0] == '#' && code[1] == '!') for (offset = 2; code[offset] && code[offset] != '\n'; offset++);
return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code + offset, argv[0], params.script_path);
} }
if (params.use_stdin) { if (params.use_stdin) {
char* code = read_stdin(); char* code = read_stdin();
if (code == 0) return 1; if (code == 0) return 1;
return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, "stdin"); return the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, argv[0], "stdin");
} }
if (params.enter_repl) { if (params.enter_repl) {
puts(NOOM_VERSION_TEXT); puts(NOOM_VERSION_TEXT);
for (;;) { for (;;) {
char code[4096]; char code[4096];
if (read_prompt(code, sizeof(code), "> ", 1)) return 0; if (read_prompt(code, sizeof(code), "> ", 1)) return 0;
the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, "(noom input)"); the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code, 0, "(noom input)");
} }
} }
die: die:

View File

@@ -38,6 +38,10 @@ typedef enum noom_LuaType {
typedef enum noom_Exit { typedef enum noom_Exit {
NOOM_OK = 0, NOOM_OK = 0,
NOOM_PLEASEHELPMEIAMSCARED,
NOOM_IAMNOTSCAREDJUSTLAZYTHISTIME,
// unknown internal error
NOOM_EINTERNAL,
NOOM_YIELD, NOOM_YIELD,
// out of memory // out of memory
NOOM_ENOMEM, NOOM_ENOMEM,

View File

@@ -149,13 +149,15 @@ noomP_Node* noomP_allocNode(noomP_Parser* parser) {
int noomP_addSubnode(noomP_Parser* parser, noomP_Node* node, noomP_Node* subnode) { int noomP_addSubnode(noomP_Parser* parser, noomP_Node* node, noomP_Node* subnode) {
if (node->subnodec == node->subnode_cap) { if (node->subnodec == node->subnode_cap) {
node->subnode_cap = node->subnode_cap * 2; noomP_Node** new = noom_realloc(node->subnodes, sizeof(noomP_Node*) * node->subnode_cap * 2);
node->subnodes = noom_realloc(node->subnodes, sizeof(noomP_Node*) * node->subnode_cap);
if (node->subnodes == 0) { if (new == 0) {
parser->error_state = NOOMP_ERROR_OOM; parser->error_state = NOOMP_ERROR_OOM; // well fuck
return 1; return 1;
} }
node->subnodes = new;
node->subnode_cap = node->subnode_cap * 2;
} }
node->subnodes[node->subnodec++] = subnode; node->subnodes[node->subnodec++] = subnode;
@@ -167,7 +169,7 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
noomL_Token token; noomL_Token token;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "{", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, "{", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_LCURLY; parser->error_state = NOOMP_ERROR_EXPECTED_LCURLY;
return 0; return 0;
} }
@@ -183,14 +185,14 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
while (1) { while (1) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "}", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "}", 1)) {
// noomP_skip(parser, &token); // noomP_skip(parser, &token);
break; break;
} }
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "[", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "[", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noom_uint_t bloc = token.offset; noom_uint_t bloc = token.offset;
@@ -198,14 +200,14 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
if (idx == 0) return 0; if (idx == 0) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "]", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, "]", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_KEY; parser->error_state = NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_KEY;
return 0; return 0;
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "=", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, "=", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_EQUALS_AFTER_KEY; parser->error_state = NOOMP_ERROR_EXPECTED_EQUALS_AFTER_KEY;
return 0; return 0;
} }
@@ -231,7 +233,7 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "=", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "=", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
// conveniently opos is also the thingamajig // conveniently opos is also the thingamajig
@@ -272,9 +274,9 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
// check for , // check for ,
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ";", 1)) { } else if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ";", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -282,7 +284,7 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
} }
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "}", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, "}", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_RCURLY; parser->error_state = NOOMP_ERROR_EXPECTED_RCURLY;
return 0; return 0;
} }
@@ -299,7 +301,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL) { if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, ".", 1)) { // field if (noom_memeq(parser->code + token.offset, token.length, ".", 1)) { // field
noomP_skip(parser, &token); // skip the . noomP_skip(parser, &token); // skip the .
noom_uint_t dotLoc = token.offset; noom_uint_t dotLoc = token.offset;
@@ -326,7 +328,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_addSubnode(parser, new, fname)) return 0; if (noomP_addSubnode(parser, new, fname)) return 0;
node = new; node = new;
} else if (noom_streql(parser->code + token.offset, token.length, "[", 1)) { // index } else if (noom_memeq(parser->code + token.offset, token.length, "[", 1)) { // index
noomP_skip(parser, &token); // skip the [ noomP_skip(parser, &token); // skip the [
noom_uint_t brackLoc = token.offset; noom_uint_t brackLoc = token.offset;
@@ -334,7 +336,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (expr == 0) return 0; if (expr == 0) return 0;
if (noomP_peek(parser, &token)) return 0; // look for ] if (noomP_peek(parser, &token)) return 0; // look for ]
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_streql(parser->code + token.offset, token.length, "]", 1))) { if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, "]", 1))) {
// damn it :( // damn it :(
parser->error_state = NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_INDEX; parser->error_state = NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_INDEX;
return 0; return 0;
@@ -351,7 +353,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_addSubnode(parser, new, expr)) return 0; if (noomP_addSubnode(parser, new, expr)) return 0;
node = new; node = new;
} else if (noom_streql(parser->code + token.offset, token.length, "(", 1)) { } else if (noom_memeq(parser->code + token.offset, token.length, "(", 1)) {
noomP_skip(parser, &token); // bye ( noomP_skip(parser, &token); // bye (
noom_uint_t parenLoc = token.offset; noom_uint_t parenLoc = token.offset;
@@ -365,7 +367,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_streql(parser->code + token.offset, token.length, ")", 1))) { if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, ")", 1))) {
while (1) { while (1) {
noomP_Node* expr = noomP_parseExpression(parser); noomP_Node* expr = noomP_parseExpression(parser);
if (expr == 0) return 0; if (expr == 0) return 0;
@@ -373,7 +375,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_addSubnode(parser, new, expr)) return 0; if (noomP_addSubnode(parser, new, expr)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_streql(parser->code + token.offset, token.length, ",", 1))) { if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, ",", 1))) {
break; break;
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
@@ -382,7 +384,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
// check for ) // check for )
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_streql(parser->code + token.offset, token.length, ")", 1))) { if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, ")", 1))) {
parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION; parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION;
return 0; return 0;
} }
@@ -390,7 +392,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
noomP_skip(parser, &token); noomP_skip(parser, &token);
node = new; node = new;
} else if (noom_streql(parser->code + token.offset, token.length, "{", 1)) { // table call } else if (noom_memeq(parser->code + token.offset, token.length, "{", 1)) { // table call
noomP_Node* new = noomP_allocNode(parser); noomP_Node* new = noomP_allocNode(parser);
if (new == 0) return 0; if (new == 0) return 0;
@@ -404,7 +406,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_addSubnode(parser, new, table)) return 0; if (noomP_addSubnode(parser, new, table)) return 0;
node = new; node = new;
} else if (noom_streql(parser->code + token.offset, token.length, ":", 1)) { // method call } else if (noom_memeq(parser->code + token.offset, token.length, ":", 1)) { // method call
noomP_skip(parser, &token); noomP_skip(parser, &token);
noom_uint_t sym_loc = token.offset; noom_uint_t sym_loc = token.offset;
@@ -430,12 +432,12 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "(", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "(", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
// new->type = NOOMP_NODE_METHODCALL; // new->type = NOOMP_NODE_METHODCALL;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_streql(parser->code + token.offset, token.length, ")", 1))) { if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, ")", 1))) {
while (1) { while (1) {
noomP_Node* expr = noomP_parseExpression(parser); noomP_Node* expr = noomP_parseExpression(parser);
if (expr == 0) return 0; if (expr == 0) return 0;
@@ -443,7 +445,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
if (noomP_addSubnode(parser, new, expr)) return 0; if (noomP_addSubnode(parser, new, expr)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_streql(parser->code + token.offset, token.length, ",", 1))) { if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, ",", 1))) {
break; break;
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
@@ -452,12 +454,12 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
// check for ) // check for )
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_streql(parser->code + token.offset, token.length, ")", 1))) { if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, ")", 1))) {
parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION; parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION;
return 0; return 0;
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "{", 1)) { } else if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "{", 1)) {
noomP_Node* table = noomP_parseTableLiteral(parser); noomP_Node* table = noomP_parseTableLiteral(parser);
if (table == 0) return 0; if (table == 0) return 0;
@@ -539,7 +541,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
return varNode; return varNode;
} else if (token.type == NOOML_TOKEN_KEYWORD) { } else if (token.type == NOOML_TOKEN_KEYWORD) {
if (noom_streql(parser->code + token.offset, token.length, "true", 4)) { if (noom_memeq(parser->code + token.offset, token.length, "true", 4)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* litNode = noomP_allocNode(parser); noomP_Node* litNode = noomP_allocNode(parser);
@@ -549,7 +551,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
litNode->source_offset = token.offset; litNode->source_offset = token.offset;
return litNode; return litNode;
} else if (noom_streql(parser->code + token.offset, token.length, "false", 5)) { } else if (noom_memeq(parser->code + token.offset, token.length, "false", 5)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* litNode = noomP_allocNode(parser); noomP_Node* litNode = noomP_allocNode(parser);
@@ -559,7 +561,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
litNode->source_offset = token.offset; litNode->source_offset = token.offset;
return litNode; return litNode;
} else if (noom_streql(parser->code + token.offset, token.length, "nil", 3)) { } else if (noom_memeq(parser->code + token.offset, token.length, "nil", 3)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* litNode = noomP_allocNode(parser); noomP_Node* litNode = noomP_allocNode(parser);
@@ -569,7 +571,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
litNode->source_offset = token.offset; litNode->source_offset = token.offset;
return litNode; return litNode;
} else if (noom_streql(parser->code + token.offset, token.length, "function", 8)) { } else if (noom_memeq(parser->code + token.offset, token.length, "function", 8)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* funcNode = noomP_allocNode(parser); noomP_Node* funcNode = noomP_allocNode(parser);
@@ -587,7 +589,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
if (noomP_addSubnode(parser, funcNode, block)) return 0; if (noomP_addSubnode(parser, funcNode, block)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
return 0; return 0;
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
@@ -595,7 +597,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
return funcNode; return funcNode;
} }
} else if (token.type == NOOML_TOKEN_SYMBOL) { } else if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, "(", 1)) { // parenthesized if (noom_memeq(parser->code + token.offset, token.length, "(", 1)) { // parenthesized
noomP_skip(parser, &token); noomP_skip(parser, &token);
noom_uint_t sym_loc = token.offset; noom_uint_t sym_loc = token.offset;
@@ -605,7 +607,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
// now to close the parentheses // now to close the parentheses
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, ")", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, ")", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION; parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION;
return 0; // unexpected return 0; // unexpected
} }
@@ -622,12 +624,12 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
// buttt we're not done YET! it could still go :dsg().dsdh():dsh() // buttt we're not done YET! it could still go :dsg().dsdh():dsh()
return noomP_parseComplexExpression(parser, paren); // thank you for being a function :heart: return noomP_parseComplexExpression(parser, paren); // thank you for being a function :heart:
} else if (noom_streql(parser->code + token.offset, token.length, "{", 1)) { } else if (noom_memeq(parser->code + token.offset, token.length, "{", 1)) {
noomP_Node* table = noomP_parseTableLiteral(parser); noomP_Node* table = noomP_parseTableLiteral(parser);
if (table == 0) return 0; if (table == 0) return 0;
return table; return table;
} else if (noom_streql(parser->code + token.offset, token.length, "...", 3)) { } else if (noom_memeq(parser->code + token.offset, token.length, "...", 3)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* new = noomP_allocNode(parser); noomP_Node* new = noomP_allocNode(parser);
@@ -645,97 +647,97 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
int noomP_infixOperatorBP(noomP_Parser* parser, noomL_Token* token, noom_uint_t* a, noom_uint_t* b) { // todo: maybe make this not pointer? we'll see int noomP_infixOperatorBP(noomP_Parser* parser, noomL_Token* token, noom_uint_t* a, noom_uint_t* b) { // todo: maybe make this not pointer? we'll see
if (token->type == NOOML_TOKEN_SYMBOL) { if (token->type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token->offset, token->length, "+", 1)) { if (noom_memeq(parser->code + token->offset, token->length, "+", 1)) {
*a = 90; *a = 90;
*b = 100; *b = 100;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "-", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, "-", 1)) {
*a = 90; *a = 90;
*b = 100; *b = 100;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "*", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, "*", 1)) {
*a = 110; *a = 110;
*b = 120; *b = 120;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "/", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, "/", 1)) {
*a = 110; *a = 110;
*b = 120; *b = 120;
return 1; return 1;
} else if (parser->version >= NOOM_VERSION_53 && noom_streql(parser->code + token->offset, token->length, "//", 2)) { } else if (parser->version >= NOOM_VERSION_53 && noom_memeq(parser->code + token->offset, token->length, "//", 2)) {
*a = 110; *a = 110;
*b = 120; *b = 120;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "%", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, "%", 1)) {
*a = 110; *a = 110;
*b = 120; *b = 120;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "^", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, "^", 1)) {
*a = 140; *a = 140;
*b = 130; // right associative *b = 130; // right associative
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "..", 2)) { } else if (noom_memeq(parser->code + token->offset, token->length, "..", 2)) {
*a = 80; *a = 80;
*b = 70; // right ass. *b = 70; // right ass.
return 1; return 1;
} else if (parser->version >= NOOM_VERSION_53 && noom_streql(parser->code + token->offset, token->length, ">>", 2)) { } else if (parser->version >= NOOM_VERSION_53 && noom_memeq(parser->code + token->offset, token->length, ">>", 2)) {
*a = 68; *a = 68;
*b = 69; *b = 69;
return 1; return 1;
} else if (parser->version >= NOOM_VERSION_53 && noom_streql(parser->code + token->offset, token->length, "<<", 2)) { } else if (parser->version >= NOOM_VERSION_53 && noom_memeq(parser->code + token->offset, token->length, "<<", 2)) {
*a = 68; *a = 68;
*b = 69; *b = 69;
return 1; return 1;
} else if (parser->version >= NOOM_VERSION_53 && noom_streql(parser->code + token->offset, token->length, "&", 1)) { } else if (parser->version >= NOOM_VERSION_53 && noom_memeq(parser->code + token->offset, token->length, "&", 1)) {
*a = 66; *a = 66;
*b = 67; *b = 67;
return 1; return 1;
} else if (parser->version >= NOOM_VERSION_53 && noom_streql(parser->code + token->offset, token->length, "~", 1)) { } else if (parser->version >= NOOM_VERSION_53 && noom_memeq(parser->code + token->offset, token->length, "~", 1)) {
*a = 64; *a = 64;
*b = 65; *b = 65;
return 1; return 1;
} else if (parser->version >= NOOM_VERSION_53 && noom_streql(parser->code + token->offset, token->length, "|", 1)) { } else if (parser->version >= NOOM_VERSION_53 && noom_memeq(parser->code + token->offset, token->length, "|", 1)) {
*a = 62; *a = 62;
*b = 63; *b = 63;
return 1; return 1;
// oh boy. // oh boy.
} else if (noom_streql(parser->code + token->offset, token->length, "<", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, "<", 1)) {
*a = 50; *a = 50;
*b = 60; *b = 60;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, ">", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, ">", 1)) {
*a = 50; *a = 50;
*b = 60; *b = 60;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "<=", 2)) { } else if (noom_memeq(parser->code + token->offset, token->length, "<=", 2)) {
*a = 50; *a = 50;
*b = 60; *b = 60;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, ">=", 2)) { } else if (noom_memeq(parser->code + token->offset, token->length, ">=", 2)) {
*a = 50; *a = 50;
*b = 60; *b = 60;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "~=", 2)) { } else if (noom_memeq(parser->code + token->offset, token->length, "~=", 2)) {
*a = 50; *a = 50;
*b = 60; *b = 60;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "==", 2)) { } else if (noom_memeq(parser->code + token->offset, token->length, "==", 2)) {
*a = 50; *a = 50;
*b = 60; *b = 60;
return 1; return 1;
} }
} else if (token->type == NOOML_TOKEN_KEYWORD) { } else if (token->type == NOOML_TOKEN_KEYWORD) {
if (noom_streql(parser->code + token->offset, token->length, "and", 3)) { if (noom_memeq(parser->code + token->offset, token->length, "and", 3)) {
*a = 30; *a = 30;
*b = 40; *b = 40;
return 1; return 1;
} else if (noom_streql(parser->code + token->offset, token->length, "or", 2)) { } else if (noom_memeq(parser->code + token->offset, token->length, "or", 2)) {
*a = 10; *a = 10;
*b = 20; *b = 20;
return 1; return 1;
@@ -747,15 +749,15 @@ int noomP_infixOperatorBP(noomP_Parser* parser, noomL_Token* token, noom_uint_t*
noom_uint_t noomP_prefixOperatorBP(noomP_Parser* parser, noomL_Token* token) { // todo: maybe make this not pointer? we'll see noom_uint_t noomP_prefixOperatorBP(noomP_Parser* parser, noomL_Token* token) { // todo: maybe make this not pointer? we'll see
if (token->type == NOOML_TOKEN_SYMBOL) { if (token->type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token->offset, token->length, "-", 1)) { if (noom_memeq(parser->code + token->offset, token->length, "-", 1)) {
return 125; return 125;
} else if (noom_streql(parser->code + token->offset, token->length, "#", 1)) { } else if (noom_memeq(parser->code + token->offset, token->length, "#", 1)) {
return 125; return 125;
} else if (parser->version >= NOOM_VERSION_53 && noom_streql(parser->code + token->offset, token->length, "~", 1)) { } else if (parser->version >= NOOM_VERSION_53 && noom_memeq(parser->code + token->offset, token->length, "~", 1)) {
return 125; return 125;
} }
} else if (token->type == NOOML_TOKEN_KEYWORD) { } else if (token->type == NOOML_TOKEN_KEYWORD) {
if (noom_streql(parser->code + token->offset, token->length, "not", 3)) { if (noom_memeq(parser->code + token->offset, token->length, "not", 3)) {
return 125; return 125;
} }
} }
@@ -841,7 +843,7 @@ noomP_Node* noomP_parseFunctionParameters(noomP_Parser* parser) {
noomL_Token token; noomL_Token token;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "(", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, "(", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_LPAREN_FOR_PARAMETERS; parser->error_state = NOOMP_ERROR_EXPECTED_LPAREN_FOR_PARAMETERS;
return 0; return 0;
} }
@@ -868,7 +870,7 @@ noomP_Node* noomP_parseFunctionParameters(noomP_Parser* parser) {
var->source_offset = token.offset; var->source_offset = token.offset;
if (noomP_addSubnode(parser, params, var)) return 0; if (noomP_addSubnode(parser, params, var)) return 0;
} else if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "...", 3)) { } else if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "...", 3)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* vararg = noomP_allocNode(parser); noomP_Node* vararg = noomP_allocNode(parser);
@@ -884,7 +886,7 @@ noomP_Node* noomP_parseFunctionParameters(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -893,7 +895,7 @@ noomP_Node* noomP_parseFunctionParameters(noomP_Parser* parser) {
// closing paren // closing paren
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, ")", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, ")", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_FOR_PARAMETERS; parser->error_state = NOOMP_ERROR_EXPECTED_RPAREN_FOR_PARAMETERS;
return 0; return 0;
} }
@@ -917,13 +919,13 @@ noomP_Node* noomP_parseBlock(noomP_Parser* parser) { // stops on end, else or el
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_KEYWORD) { if (token.type == NOOML_TOKEN_KEYWORD) {
if (noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
break; break;
} else if (noom_streql(parser->code + token.offset, token.length, "elseif", 6)) { } else if (noom_memeq(parser->code + token.offset, token.length, "elseif", 6)) {
break; break;
} else if (noom_streql(parser->code + token.offset, token.length, "else", 4)) { } else if (noom_memeq(parser->code + token.offset, token.length, "else", 4)) {
break; break;
} else if (noom_streql(parser->code + token.offset, token.length, "until", 5)) { } else if (noom_memeq(parser->code + token.offset, token.length, "until", 5)) {
break; break;
} }
} }
@@ -942,12 +944,12 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_KEYWORD) { if (token.type == NOOML_TOKEN_KEYWORD) {
if (noom_streql(parser->code + token.offset, token.length, "local", 5)) { if (noom_memeq(parser->code + token.offset, token.length, "local", 5)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_KEYWORD && noom_streql(parser->code + token.offset, token.length, "function", 8)) { if (token.type == NOOML_TOKEN_KEYWORD && noom_memeq(parser->code + token.offset, token.length, "function", 8)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* funcNode = noomP_allocNode(parser); noomP_Node* funcNode = noomP_allocNode(parser);
@@ -980,7 +982,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, funcNode, block)) return 0; if (noomP_addSubnode(parser, funcNode, block)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_LOCAL_FUNCTION; parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_LOCAL_FUNCTION;
return 0; return 0;
} }
@@ -1013,7 +1015,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (parser->version >= NOOM_VERSION_54) { if (parser->version >= NOOM_VERSION_54) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "<", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "<", 1)) {
// attribute yay // attribute yay
noomP_skip(parser, &token); noomP_skip(parser, &token);
@@ -1026,13 +1028,13 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
noom_uint_t attr = token.offset; noom_uint_t attr = token.offset;
noomP_skip(parser, &token); noomP_skip(parser, &token);
if (!noom_streql(parser->code + token.offset, token.length, "const", 5) && !noom_streql(parser->code + token.offset, token.length, "close", 5)) { if (!noom_memeq(parser->code + token.offset, token.length, "const", 5) && !noom_memeq(parser->code + token.offset, token.length, "close", 5)) {
parser->error_state = NOOMP_ERROR_FAKEATTRIBUTE; parser->error_state = NOOMP_ERROR_FAKEATTRIBUTE;
return 0; // not a real attribute smh my head return 0; // not a real attribute smh my head
} }
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, ">", 1)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, ">", 1)) {
parser->error_state = NOOMP_ERROR_EXPECTED_RANGLE_TO_CLOSE_ATTRIBUTE; parser->error_state = NOOMP_ERROR_EXPECTED_RANGLE_TO_CLOSE_ATTRIBUTE;
return 0; return 0;
} }
@@ -1053,7 +1055,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL) { if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -1067,7 +1069,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
// local with no equals initializes to nil // local with no equals initializes to nil
if (token.type != NOOML_TOKEN_SYMBOL) return localNode; if (token.type != NOOML_TOKEN_SYMBOL) return localNode;
if (!noom_streql(parser->code + token.offset, token.length, "=", 1)) return localNode; if (!noom_memeq(parser->code + token.offset, token.length, "=", 1)) return localNode;
noomP_skip(parser, &token); noomP_skip(parser, &token);
// equals has already been eaten by loop (thank you loop) // equals has already been eaten by loop (thank you loop)
@@ -1081,7 +1083,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL) { if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -1092,7 +1094,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
} }
return localNode; return localNode;
} else if (noom_streql(parser->code + token.offset, token.length, "if", 2)) { } else if (noom_memeq(parser->code + token.offset, token.length, "if", 2)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* ifStatement = noomP_allocNode(parser); noomP_Node* ifStatement = noomP_allocNode(parser);
@@ -1108,7 +1110,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "then", 4)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "then", 4)) {
parser->error_state = NOOMP_ERROR_EXPECTED_THEN_AFTER_EXPRESSION; parser->error_state = NOOMP_ERROR_EXPECTED_THEN_AFTER_EXPRESSION;
return 0; // unexpected return 0; // unexpected
} }
@@ -1128,7 +1130,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
return 0; // unexpected return 0; // unexpected
} }
if (noom_streql(parser->code + token.offset, token.length, "elseif", 6)) { if (noom_memeq(parser->code + token.offset, token.length, "elseif", 6)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* elseIfCondition = noomP_parseExpression(parser); noomP_Node* elseIfCondition = noomP_parseExpression(parser);
if (elseIfCondition == 0) return 0; if (elseIfCondition == 0) return 0;
@@ -1138,7 +1140,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
// now we need to check for "then" // now we need to check for "then"
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "then", 4)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "then", 4)) {
parser->error_state = NOOMP_ERROR_EXPECTED_THEN_AFTER_EXPRESSION; parser->error_state = NOOMP_ERROR_EXPECTED_THEN_AFTER_EXPRESSION;
return 0; // unexpected return 0; // unexpected
} }
@@ -1151,7 +1153,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, ifStatement, elseIfBlock)) return 0; if (noomP_addSubnode(parser, ifStatement, elseIfBlock)) return 0;
// could be even more // could be even more
} else if (noom_streql(parser->code + token.offset, token.length, "else", 4)) { } else if (noom_memeq(parser->code + token.offset, token.length, "else", 4)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* elseBlock = noomP_parseBlock(parser); noomP_Node* elseBlock = noomP_parseBlock(parser);
@@ -1161,7 +1163,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, ifStatement, elseBlock)) return 0; if (noomP_addSubnode(parser, ifStatement, elseBlock)) return 0;
break; // this must be the last one; end is handled after the loop break; // this must be the last one; end is handled after the loop
} else if (noom_streql(parser->code + token.offset, token.length, "end", 3)) { } else if (noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
break; // will check for end outside the loop because else and things break; // will check for end outside the loop because else and things
} else { } else {
// unexpected // unexpected
@@ -1171,14 +1173,14 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
} }
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_IF; parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_IF;
return 0; // unexpected return 0; // unexpected
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
return ifStatement; return ifStatement;
} else if (noom_streql(parser->code + token.offset, token.length, "while", 5)) { } else if (noom_memeq(parser->code + token.offset, token.length, "while", 5)) {
noomP_skip(parser, &token); // skip `while` noomP_skip(parser, &token); // skip `while`
noomP_Node* node = noomP_allocNode(parser); noomP_Node* node = noomP_allocNode(parser);
@@ -1194,7 +1196,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "do", 2)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "do", 2)) {
parser->error_state = NOOMP_ERROR_EXPECTED_DO_AFTER_EXPRESSION; parser->error_state = NOOMP_ERROR_EXPECTED_DO_AFTER_EXPRESSION;
return 0; // unexpected return 0; // unexpected
} }
@@ -1206,14 +1208,14 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, node, block)) return 0; if (noomP_addSubnode(parser, node, block)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_WHILE; parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_WHILE;
return 0; // unexpected return 0; // unexpected
} }
noomP_skip(parser, &token); // skip `end` noomP_skip(parser, &token); // skip `end`
return node; return node;
} else if (noom_streql(parser->code + token.offset, token.length, "break", 5)) { } else if (noom_memeq(parser->code + token.offset, token.length, "break", 5)) {
noomP_skip(parser, &token); // skip `break` noomP_skip(parser, &token); // skip `break`
// uhh yeah that's it i guess? idk. maybe parsers should figure out what loop? // uhh yeah that's it i guess? idk. maybe parsers should figure out what loop?
@@ -1225,7 +1227,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
node->source_offset = token.offset; node->source_offset = token.offset;
return node; return node;
} else if (noom_streql(parser->code + token.offset, token.length, "function", 8)) { } else if (noom_memeq(parser->code + token.offset, token.length, "function", 8)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* func = noomP_allocNode(parser); noomP_Node* func = noomP_allocNode(parser);
@@ -1263,7 +1265,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (token.type != NOOML_TOKEN_SYMBOL) return 0; // unexp. if (token.type != NOOML_TOKEN_SYMBOL) return 0; // unexp.
if (noom_streql(parser->code + token.offset, token.length, ".", 1)) { if (noom_memeq(parser->code + token.offset, token.length, ".", 1)) {
// just keep on going at it // just keep on going at it
noomP_skip(parser, &token); noomP_skip(parser, &token);
@@ -1282,7 +1284,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
new->source_offset = token.offset; new->source_offset = token.offset;
if (noomP_addSubnode(parser, fname, new)) return 0; if (noomP_addSubnode(parser, fname, new)) return 0;
} else if (noom_streql(parser->code + token.offset, token.length, ":", 1)) { } else if (noom_memeq(parser->code + token.offset, token.length, ":", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
@@ -1301,7 +1303,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, fname, new)) return 0; if (noomP_addSubnode(parser, fname, new)) return 0;
break; // ( checked later. break; // ( checked later.
} else if (noom_streql(parser->code + token.offset, token.length, "(", 1)) { } else if (noom_memeq(parser->code + token.offset, token.length, "(", 1)) {
break; break;
} else { } else {
return 0; return 0;
@@ -1320,14 +1322,14 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
// remove `end` // remove `end`
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_FUNCTION; parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_FUNCTION;
return 0; return 0;
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
return func; return func;
} else if (noom_streql(parser->code + token.offset, token.length, "return", 6)) { } else if (noom_memeq(parser->code + token.offset, token.length, "return", 6)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* ret = noomP_allocNode(parser); noomP_Node* ret = noomP_allocNode(parser);
@@ -1344,17 +1346,17 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
int is_empty = 0; int is_empty = 0;
if (token.type == NOOML_TOKEN_SYMBOL) { if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, ";", 1)) { if (noom_memeq(parser->code + token.offset, token.length, ";", 1)) {
is_empty = 1; is_empty = 1;
} }
} else if (token.type == NOOML_TOKEN_KEYWORD) { } else if (token.type == NOOML_TOKEN_KEYWORD) {
if (noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
is_empty = 1; is_empty = 1;
} else if (noom_streql(parser->code + token.offset, token.length, "elseif", 6)) { } else if (noom_memeq(parser->code + token.offset, token.length, "elseif", 6)) {
is_empty = 1; is_empty = 1;
} else if (noom_streql(parser->code + token.offset, token.length, "else", 4)) { } else if (noom_memeq(parser->code + token.offset, token.length, "else", 4)) {
is_empty = 1; is_empty = 1;
} else if (noom_streql(parser->code + token.offset, token.length, "until", 1)) { } else if (noom_memeq(parser->code + token.offset, token.length, "until", 1)) {
is_empty = 1; is_empty = 1;
} }
} else if (token.type == NOOML_TOKEN_EOF) { } else if (token.type == NOOML_TOKEN_EOF) {
@@ -1370,7 +1372,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -1381,7 +1383,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
while (1) { // remove semis so we can check for ender after. while (1) { // remove semis so we can check for ender after.
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ";", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ";", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -1392,13 +1394,13 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
// now we have to make sure we he have an ender; // now we have to make sure we he have an ender;
int is_done = 0; int is_done = 0;
if (token.type == NOOML_TOKEN_KEYWORD) { if (token.type == NOOML_TOKEN_KEYWORD) {
if (noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
is_done = 1; is_done = 1;
} else if (noom_streql(parser->code + token.offset, token.length, "elseif", 6)) { } else if (noom_memeq(parser->code + token.offset, token.length, "elseif", 6)) {
is_done = 1; is_done = 1;
} else if (noom_streql(parser->code + token.offset, token.length, "else", 4)) { } else if (noom_memeq(parser->code + token.offset, token.length, "else", 4)) {
is_done = 1; is_done = 1;
} else if (noom_streql(parser->code + token.offset, token.length, "until", 1)) { } else if (noom_memeq(parser->code + token.offset, token.length, "until", 1)) {
is_done = 1; is_done = 1;
} }
} else if (token.type == NOOML_TOKEN_EOF) { } else if (token.type == NOOML_TOKEN_EOF) {
@@ -1411,7 +1413,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
} }
return ret; return ret;
} else if (noom_streql(parser->code + token.offset, token.length, "for", 3)) { } else if (noom_memeq(parser->code + token.offset, token.length, "for", 3)) {
// yay... // yay...
noomP_skip(parser, &token); noomP_skip(parser, &token);
@@ -1439,7 +1441,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "=", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "=", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
// ehhh i'll do a stupid // ehhh i'll do a stupid
@@ -1453,7 +1455,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -1467,7 +1469,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
} else { } else {
forl->type = NOOMP_NODE_FORLOOPIN; forl->type = NOOMP_NODE_FORLOOPIN;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
while (1) { while (1) {
@@ -1488,7 +1490,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -1498,7 +1500,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
// okay. that took a while. now for the in and the expressions // okay. that took a while. now for the in and the expressions
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "in", 2)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "in", 2)) {
parser->error_state = NOOMP_ERROR_EXPECTED_IN; parser->error_state = NOOMP_ERROR_EXPECTED_IN;
return 0; return 0;
} }
@@ -1513,7 +1515,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
} else { } else {
break; break;
@@ -1523,7 +1525,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
// making this the same for all of them: do [block] end // making this the same for all of them: do [block] end
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "do", 2)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "do", 2)) {
parser->error_state = NOOMP_ERROR_EXPECTED_DO_AFTER_EXPRESSION; parser->error_state = NOOMP_ERROR_EXPECTED_DO_AFTER_EXPRESSION;
return 0; return 0;
} }
@@ -1535,14 +1537,14 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, forl, block)) return 0; if (noomP_addSubnode(parser, forl, block)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_FOR; parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_FOR;
return 0; return 0;
} }
noomP_skip(parser, &token); noomP_skip(parser, &token);
return forl; return forl;
} else if (noom_streql(parser->code + token.offset, token.length, "goto", 4)) { // this keyword can't exist if not on the right version } else if (noom_memeq(parser->code + token.offset, token.length, "goto", 4)) { // this keyword can't exist if not on the right version
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* thing = noomP_allocNode(parser); noomP_Node* thing = noomP_allocNode(parser);
@@ -1567,7 +1569,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, thing, name)) return 0; if (noomP_addSubnode(parser, thing, name)) return 0;
return thing; return thing;
} else if (noom_streql(parser->code + token.offset, token.length, "repeat", 6)) { } else if (noom_memeq(parser->code + token.offset, token.length, "repeat", 6)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* repeat = noomP_allocNode(parser); noomP_Node* repeat = noomP_allocNode(parser);
@@ -1582,7 +1584,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, repeat, block)) return 0; if (noomP_addSubnode(parser, repeat, block)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "until", 5)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "until", 5)) {
parser->error_state = NOOMP_ERROR_EXPECTED_UNTIL; parser->error_state = NOOMP_ERROR_EXPECTED_UNTIL;
return 0; return 0;
} }
@@ -1595,7 +1597,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, repeat, condition)) return 0; if (noomP_addSubnode(parser, repeat, condition)) return 0;
return repeat; return repeat;
} else if (noom_streql(parser->code + token.offset, token.length, "do", 2)) { } else if (noom_memeq(parser->code + token.offset, token.length, "do", 2)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* doblock = noomP_allocNode(parser); noomP_Node* doblock = noomP_allocNode(parser);
@@ -1610,7 +1612,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, doblock, block)) return 0; if (noomP_addSubnode(parser, doblock, block)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_KEYWORD || !noom_streql(parser->code + token.offset, token.length, "end", 3)) { if (token.type != NOOML_TOKEN_KEYWORD || !noom_memeq(parser->code + token.offset, token.length, "end", 3)) {
parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_DO; parser->error_state = NOOMP_ERROR_EXPECTED_END_AFTER_DO;
return 0; return 0;
} }
@@ -1619,7 +1621,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
return doblock; return doblock;
} }
} else if (token.type == NOOML_TOKEN_SYMBOL) { } else if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, "::", 2)) { // symbol doesn't exist on wrong versions if (noom_memeq(parser->code + token.offset, token.length, "::", 2)) { // symbol doesn't exist on wrong versions
noomP_skip(parser, &token); noomP_skip(parser, &token);
noomP_Node* thing = noomP_allocNode(parser); noomP_Node* thing = noomP_allocNode(parser);
@@ -1641,7 +1643,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, thing, name)) return 0; if (noomP_addSubnode(parser, thing, name)) return 0;
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "::", 2)) { if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, "::", 2)) {
parser->error_state = NOOMP_ERROR_EXPECTED_COLONCOLON; parser->error_state = NOOMP_ERROR_EXPECTED_COLONCOLON;
return 0; return 0;
} }
@@ -1651,7 +1653,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
} }
} }
if (token.type == NOOML_TOKEN_IDENTIFIER || (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, "(", 1))) { if (token.type == NOOML_TOKEN_IDENTIFIER || (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, "(", 1))) {
noomP_Node* base = noomP_parseRawExpression(parser); noomP_Node* base = noomP_parseRawExpression(parser);
if (base == 0) return 0; if (base == 0) return 0;
@@ -1678,12 +1680,12 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL) { if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type != NOOML_TOKEN_IDENTIFIER && (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "(", 1))) { if (token.type != NOOML_TOKEN_IDENTIFIER && (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(parser->code + token.offset, token.length, "(", 1))) {
// unexpected // unexpected
parser->error_state = NOOMP_ERROR_EXPECTED_ASSIGNABLE; parser->error_state = NOOMP_ERROR_EXPECTED_ASSIGNABLE;
return 0; return 0;
@@ -1707,7 +1709,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_addSubnode(parser, container, item)) return 0; if (noomP_addSubnode(parser, container, item)) return 0;
if (noomP_addSubnode(parser, assignment, container)) return 0; if (noomP_addSubnode(parser, assignment, container)) return 0;
} else if (noom_streql(parser->code + token.offset, token.length, "=", 1)) { } else if (noom_memeq(parser->code + token.offset, token.length, "=", 1)) {
assignment->source_offset = token.offset; assignment->source_offset = token.offset;
noomP_skip(parser, &token); noomP_skip(parser, &token);
break; break;
@@ -1729,7 +1731,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL && noom_streql(parser->code + token.offset, token.length, ",", 1)) { if (token.type == NOOML_TOKEN_SYMBOL && noom_memeq(parser->code + token.offset, token.length, ",", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
continue; // explicit cause felt like it continue; // explicit cause felt like it
} else { } else {
@@ -1760,7 +1762,7 @@ noomP_Node* noomP_parseStatement(noomP_Parser* parser) {
while (1) { while (1) {
if (noomP_peek(parser, &token)) return 0; if (noomP_peek(parser, &token)) return 0;
if (token.type == NOOML_TOKEN_SYMBOL) { if (token.type == NOOML_TOKEN_SYMBOL) {
if (noom_streql(parser->code + token.offset, token.length, ";", 1)) { if (noom_memeq(parser->code + token.offset, token.length, ";", 1)) {
noomP_skip(parser, &token); noomP_skip(parser, &token);
continue; continue;
} }

View File

@@ -13,6 +13,47 @@ noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size)
return o; return o;
} }
noomV_String *noomV_allocStr(noom_LuaVM *vm, const char *str, noom_uint_t len) {
noomV_String *s = (noomV_String*)noomV_allocObj(vm, NOOMV_OSTR, sizeof(noomV_String) + len + 1);
if (s == 0) return 0;
noom_memcpy(s->data, str, len);
s->data[len] = '\0';
return s;
}
noomV_Function *noomV_allocFunc(noom_LuaVM *vm, const char *str, noom_uint_t len) {
noomV_Function *f = (noomV_Function*)noomV_allocObj(vm, NOOMV_OFUNC, sizeof(noomV_Function));
if (f == 0) return 0;
f->chunkname = noomV_allocStr(vm, str, len);
if (f->chunkname == 0) return 0;
f->code = 0;
f->consts = 0;
f->protos = 0;
f->upvals = 0;
f->locals = 0;
f->codesize = 0;
f->linedefined = 0;
f->lastlinedefined = 0;
f->argc = 0;
f->flags = 0;
f->constsize = 0;
f->protosize = 0;
f->upvalsize = 0;
f->localsize = 0;
return f;
}
noomV_Table *noomV_allocTable(noom_LuaVM *vm) {
noomV_Table *t = (noomV_Table *)noomV_allocObj(vm, NOOMV_OTABLE, sizeof(noomV_Table));
if (t == 0) return 0;
t->meta = 0;
t->entries = 0;
t->used = 0;
t->len = 0;
t->entrydata = 0;
return t;
}
void noomV_freeObj(noomV_Object *obj) { void noomV_freeObj(noomV_Object *obj) {
noom_free(obj); noom_free(obj);
} }

View File

@@ -51,6 +51,7 @@ typedef struct noomV_Value {
noom_int_t integer; noom_int_t integer;
noom_float_t number; noom_float_t number;
noomV_Object *obj; noomV_Object *obj;
struct noomV_Pointer *ptr;
}; };
} noomV_Value; } noomV_Value;
@@ -64,8 +65,15 @@ typedef struct noomV_String {
typedef struct noomV_Table { typedef struct noomV_Table {
noomV_Object obj; noomV_Object obj;
struct noomV_Table *meta; struct noomV_Table *meta;
// amount of entries allocated
noom_uint_t entries; noom_uint_t entries;
// how many entries are defined.
// Note, this includes keys with null values, which are still scanned.
noom_uint_t used;
// cache of #t, for faster subsequent fetches
noom_uint_t len; noom_uint_t len;
// actual values. Given index i, V[i] is the key and V[i+entries] is the value.
// This is because we mostly read the key, so we should put more keys in cache.
noomV_Value *entrydata; noomV_Value *entrydata;
} noomV_Table; } noomV_Table;
@@ -166,6 +174,7 @@ typedef struct noomV_Inst {
typedef struct noomV_UpvalDesc { typedef struct noomV_UpvalDesc {
char *name; char *name;
unsigned char idx; unsigned char idx;
// whether the index is a stack index
noom_bool_t isStack; noom_bool_t isStack;
} noomV_UpvalDesc; } noomV_UpvalDesc;
@@ -188,7 +197,9 @@ typedef struct noomV_Function {
noomV_UpvalDesc *upvals; noomV_UpvalDesc *upvals;
noomV_LocalDesc *locals; noomV_LocalDesc *locals;
unsigned int codesize; unsigned int codesize;
// line of function
unsigned int linedefined; unsigned int linedefined;
// line of end
unsigned int lastlinedefined; unsigned int lastlinedefined;
// very size limited // very size limited
// ░░░░░░░░░ ░ ▒░░▒ ▒░ ░░ ░ // ░░░░░░░░░ ░ ▒░░▒ ▒░ ░░ ░
@@ -296,7 +307,11 @@ struct noom_LuaVM {
noom_LuaVersion version; noom_LuaVersion version;
}; };
// Allocating objects
noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size); noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size);
noomV_String *noomV_allocStr(noom_LuaVM *vm, const char *str, noom_uint_t len);
noomV_Function *noomV_allocFunc(noom_LuaVM *vm, const char *str, noom_uint_t len);
noomV_Function *noomV_allocFunc(noom_LuaVM *vm, const char *str, noom_uint_t len);
void noomV_freeObj(noomV_Object *obj); void noomV_freeObj(noomV_Object *obj);
#endif #endif