forked from NeoFlock/noom
formatting and shi
This commit is contained in:
@@ -37,3 +37,5 @@ IndentGotoLabels: true
|
|||||||
|
|
||||||
BinPackArguments: false
|
BinPackArguments: false
|
||||||
BinPackParameters: false
|
BinPackParameters: false
|
||||||
|
|
||||||
|
InsertTrailingCommas: Wrapped
|
||||||
|
|||||||
194
src/compiler.c
194
src/compiler.c
@@ -27,32 +27,34 @@
|
|||||||
// ⠀⠀⠀⠀⠀⠀⠀⠀⠙⠛⠛⠉⠁⠀⠀⠀⠀⠀⠀⡘⠂⠀⠀⠀⠀⠈⠉⠛⠛⠻⠿⠷⠦
|
// ⠀⠀⠀⠀⠀⠀⠀⠀⠙⠛⠛⠉⠁⠀⠀⠀⠀⠀⠀⡘⠂⠀⠀⠀⠀⠈⠉⠛⠛⠻⠿⠷⠦
|
||||||
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡘⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡘⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||||
|
|
||||||
typedef struct noomC_LocalInfo {
|
typedef struct noomC_LocalInfo {
|
||||||
enum { NOOMC_GLOBAL, NOOMC_LOCAL, NOOMC_UPVAL } type;
|
enum { NOOMC_GLOBAL,
|
||||||
|
NOOMC_LOCAL,
|
||||||
|
NOOMC_UPVAL } type;
|
||||||
unsigned int idx;
|
unsigned int idx;
|
||||||
} noomC_LocalInfo;
|
} noomC_LocalInfo;
|
||||||
|
|
||||||
noom_Exit noomC_addLocal(noomC_Compiler *c, noomC_Local local) {
|
noom_Exit noomC_addLocal(noomC_Compiler* c, noomC_Local local) {
|
||||||
if(c->localc == NOOMC_MAXLOCAL) return NOOM_EINTERNAL;
|
if (c->localc == NOOMC_MAXLOCAL) return NOOM_EINTERNAL;
|
||||||
c->locals[c->localc++] = local;
|
c->locals[c->localc++] = local;
|
||||||
return NOOM_OK;
|
return NOOM_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
noom_Exit noomC_addUpval(noomC_Compiler *c, noomC_Upval upval) {
|
noom_Exit noomC_addUpval(noomC_Compiler* c, noomC_Upval upval) {
|
||||||
if(c->upvalc == NOOMC_MAXUPVAL) return NOOM_EINTERNAL;
|
if (c->upvalc == NOOMC_MAXUPVAL) return NOOM_EINTERNAL;
|
||||||
c->upvals[c->upvalc++] = upval;
|
c->upvals[c->upvalc++] = upval;
|
||||||
return NOOM_OK;
|
return NOOM_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
noom_Exit noomC_identifyLocal(noomC_Compiler *compiler, noomC_LocalInfo *info, const char *name, noom_uint_t namelen) {
|
noom_Exit noomC_identifyLocal(noomC_Compiler* compiler, noomC_LocalInfo* info, const char* name, noom_uint_t namelen) {
|
||||||
// TODO: attempt to steal locals for upvalues
|
// TODO: attempt to steal locals for upvalues
|
||||||
|
|
||||||
for(int i = compiler->localc-1; i >= 0; i--) {
|
for (int i = compiler->localc - 1; i >= 0; i--) {
|
||||||
noomC_Local l = compiler->locals[i];
|
noomC_Local l = compiler->locals[i];
|
||||||
if(l.dropped) continue;
|
if (l.dropped) continue;
|
||||||
if(noom_memeq(l.name, l.namelen, name, namelen)) {
|
if (noom_memeq(l.name, l.namelen, name, namelen)) {
|
||||||
info->type = NOOMC_LOCAL;
|
info->type = NOOMC_LOCAL;
|
||||||
info->idx = l.stackslot;
|
info->idx = l.stackslot;
|
||||||
return NOOM_OK;
|
return NOOM_OK;
|
||||||
@@ -64,71 +66,71 @@ noom_Exit noomC_identifyLocal(noomC_Compiler *compiler, noomC_LocalInfo *info, c
|
|||||||
return NOOM_OK;
|
return NOOM_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void noomC_compiler_init(noomC_Compiler *compiler) {
|
void noomC_compiler_init(noomC_Compiler* compiler) {
|
||||||
compiler->parent = 0;
|
compiler->parent = 0;
|
||||||
compiler->target = 0;
|
compiler->target = 0;
|
||||||
|
|
||||||
compiler->localc = 0;
|
compiler->localc = 0;
|
||||||
compiler->upvalc = 0;
|
compiler->upvalc = 0;
|
||||||
compiler->curstack = 0;
|
compiler->curstack = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static noom_Exit noomC_emit(noomV_Function *func, const noomV_Inst inst) {
|
static noom_Exit noomC_emit(noomV_Function* func, const noomV_Inst inst) {
|
||||||
noomV_Inst *newCode =noom_realloc(func->code, sizeof(noomV_Inst) * (func->codesize + 1));
|
noomV_Inst* newCode = noom_realloc(func->code, sizeof(noomV_Inst) * (func->codesize + 1));
|
||||||
if (newCode == 0) return NOOM_ENOMEM;
|
if (newCode == 0) return NOOM_ENOMEM;
|
||||||
func->code = newCode;
|
func->code = newCode;
|
||||||
func->code[func->codesize++] = inst;
|
func->code[func->codesize++] = inst;
|
||||||
return NOOM_OK;
|
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) {
|
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});
|
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) {
|
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});
|
return noomC_emit(func, (noomV_Inst){.op = op, .a = a, .us = us});
|
||||||
}
|
}
|
||||||
|
|
||||||
static noom_Exit noomC_addconst(noomV_Function *func, const noomV_Value val) {
|
static noom_Exit noomC_addconst(noomV_Function* func, const noomV_Value val) {
|
||||||
if (func->constsize == NOOM_USHORT_MAX) return NOOM_PLEASEHELPMEIAMSCARED;
|
if (func->constsize == NOOM_USHORT_MAX) return NOOM_PLEASEHELPMEIAMSCARED;
|
||||||
noomV_Value *newConsts = noom_realloc(func->consts, sizeof(noomV_Value) * (func->constsize + 1));
|
noomV_Value* newConsts = noom_realloc(func->consts, sizeof(noomV_Value) * (func->constsize + 1));
|
||||||
if (newConsts == 0) return NOOM_ENOMEM;
|
if (newConsts == 0) return NOOM_ENOMEM;
|
||||||
func->consts = newConsts;
|
func->consts = newConsts;
|
||||||
func->consts[func->constsize++] = val;
|
func->consts[func->constsize++] = val;
|
||||||
return NOOM_OK;
|
return NOOM_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static noomV_String *noomC_internString(noomC_Compiler *c, noom_LuaVM *vm, const char *str, noom_uint_t len) {
|
static noomV_String* noomC_internString(noomC_Compiler* c, noom_LuaVM* vm, const char* str, noom_uint_t len) {
|
||||||
while(c) {
|
while (c) {
|
||||||
noomV_Function *f = c->target;
|
noomV_Function* f = c->target;
|
||||||
for(int i = 0; i < f->constsize; i++) {
|
for (int i = 0; i < f->constsize; i++) {
|
||||||
noomV_Value v = f->consts[i];
|
noomV_Value v = f->consts[i];
|
||||||
if(v.tag != NOOMV_VOBJ) continue;
|
if (v.tag != NOOMV_VOBJ) continue;
|
||||||
noomV_Object *o = v.obj;
|
noomV_Object* o = v.obj;
|
||||||
if(o->tag != NOOMV_OSTR) continue;
|
if (o->tag != NOOMV_OSTR) continue;
|
||||||
noomV_String *s = (noomV_String *)o;
|
noomV_String* s = (noomV_String*)o;
|
||||||
if(noom_memeq(s->data, s->len, str, len)) return s;
|
if (noom_memeq(s->data, s->len, str, len)) return s;
|
||||||
}
|
}
|
||||||
c = c->parent;
|
c = c->parent;
|
||||||
}
|
}
|
||||||
return noomV_allocStr(vm, str, len);
|
return noomV_allocStr(vm, str, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static noom_Exit noomC_addconst_str(noomC_Compiler *c, noom_LuaVM *vm, const char *str, const noom_uint_t len) {
|
static noom_Exit noomC_addconst_str(noomC_Compiler* c, noom_LuaVM* vm, const char* str, const noom_uint_t len) {
|
||||||
noomV_String *s = noomC_internString(c, vm, str, len);
|
noomV_String* s = noomC_internString(c, vm, str, len);
|
||||||
if (s == 0) return NOOM_ENOMEM;
|
if (s == 0) return NOOM_ENOMEM;
|
||||||
return noomC_addconst(c->target, (noomV_Value){.tag = NOOMV_VOBJ, .autoclose = 0, .isptr = 0, .obj = (noomV_Object *)s});
|
return noomC_addconst(c->target, (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) {
|
static noomL_Token noomC_token_at(const noomP_Parser* parser, noom_uint_t offset) {
|
||||||
noomL_Token token;
|
noomL_Token token;
|
||||||
noomL_lex(parser->code, offset, &token, parser->version);
|
noomL_lex(parser->code, offset, &token, parser->version);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is a high chance I forgot something here
|
// There is a high chance I forgot something here
|
||||||
static noom_BinOp noomC_what_bop_is_this(const noomP_Parser *parser, noom_uint_t offset) {
|
static noom_BinOp noomC_what_bop_is_this(const noomP_Parser* parser, noom_uint_t offset) {
|
||||||
const char *op = parser->code + offset;
|
const char* op = parser->code + offset;
|
||||||
// I AM THE LEXER NOW!!!!!!!!!
|
// I AM THE LEXER NOW!!!!!!!!!
|
||||||
if (noom_startswith(op, "==")) return NOOM_BIN_EQL;
|
if (noom_startswith(op, "==")) return NOOM_BIN_EQL;
|
||||||
if (noom_startswith(op, "~=")) return NOOM_BIN_NEQL;
|
if (noom_startswith(op, "~=")) return NOOM_BIN_NEQL;
|
||||||
@@ -137,7 +139,7 @@ static noom_BinOp noomC_what_bop_is_this(const noomP_Parser *parser, noom_uint_t
|
|||||||
if (noom_startswith(op, ">")) return NOOM_BIN_GREATER;
|
if (noom_startswith(op, ">")) return NOOM_BIN_GREATER;
|
||||||
if (noom_startswith(op, ">=")) return NOOM_BIN_GREATEREQL;
|
if (noom_startswith(op, ">=")) return NOOM_BIN_GREATEREQL;
|
||||||
// no .., that is special cased due to funky behavior
|
// no .., that is special cased due to funky behavior
|
||||||
|
|
||||||
if (parser->version >= NOOM_VERSION_53) {
|
if (parser->version >= NOOM_VERSION_53) {
|
||||||
// Fucking atom forgot to put make an instruction
|
// Fucking atom forgot to put make an instruction
|
||||||
// update no i am unable to read
|
// update no i am unable to read
|
||||||
@@ -161,12 +163,11 @@ static noom_BinOp noomC_what_bop_is_this(const noomP_Parser *parser, noom_uint_t
|
|||||||
// mmap ahh function 🥀🥀🥀🥀🥀🥀🥀
|
// mmap ahh function 🥀🥀🥀🥀🥀🥀🥀
|
||||||
// What 6 argument syscalls there even are other than mmap and clone and is there anything longer
|
// What 6 argument syscalls there even are other than mmap and clone and is there anything longer
|
||||||
static noom_Exit noomC_compile_expr(
|
static noom_Exit noomC_compile_expr(
|
||||||
noom_LuaVM *vm,
|
noom_LuaVM* vm,
|
||||||
noomC_Compiler *compiler,
|
noomC_Compiler* compiler,
|
||||||
const noomP_Parser *parser,
|
const noomP_Parser* parser,
|
||||||
noomV_Function *func,
|
noomV_Function* func,
|
||||||
const noomP_Node *node
|
const noomP_Node* node) {
|
||||||
) {
|
|
||||||
noom_Exit result;
|
noom_Exit result;
|
||||||
// Baba is You OST is a very cool soundtrack to code to Can recommend
|
// Baba is You OST is a very cool soundtrack to code to Can recommend
|
||||||
if (node->type == NOOMP_NODE_NILLITERAL) {
|
if (node->type == NOOMP_NODE_NILLITERAL) {
|
||||||
@@ -193,34 +194,34 @@ static noom_Exit noomC_compile_expr(
|
|||||||
}
|
}
|
||||||
if (node->type == NOOMP_NODE_BINARYOPERATOR) {
|
if (node->type == NOOMP_NODE_BINARYOPERATOR) {
|
||||||
if (node->subnodec != 2) return NOOM_EINTERNAL;
|
if (node->subnodec != 2) return NOOM_EINTERNAL;
|
||||||
const char *op = parser->code + node->source_offset;
|
const char* op = parser->code + node->source_offset;
|
||||||
if(noom_startswith(op, "..")) {
|
if (noom_startswith(op, "..")) {
|
||||||
// concat is special
|
// concat is special
|
||||||
noom_uint_t amount = 1;
|
noom_uint_t amount = 1;
|
||||||
const noomP_Node *n = node->subnodes[1];
|
const noomP_Node* n = node->subnodes[1];
|
||||||
if((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]))) return result;
|
if ((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]))) return result;
|
||||||
// we have no flattening so we unwind it ourselves
|
// we have no flattening so we unwind it ourselves
|
||||||
// unlike all left-associative ones, like +, where 1 + 2 + 3 is (1 + 2) + 3,
|
// unlike all left-associative ones, like +, where 1 + 2 + 3 is (1 + 2) + 3,
|
||||||
// concat is right-associate, like ^, meaning 1 .. 2 .. 3 is 1 .. (2 .. 3).
|
// concat is right-associate, like ^, meaning 1 .. 2 .. 3 is 1 .. (2 .. 3).
|
||||||
// VM will handle the concat order within the array for us
|
// VM will handle the concat order within the array for us
|
||||||
while(1) {
|
while (1) {
|
||||||
if(amount > NOOM_USHORT_MAX) return NOOM_EINTERNAL;
|
if (amount > NOOM_USHORT_MAX) return NOOM_EINTERNAL;
|
||||||
amount++;
|
amount++;
|
||||||
if(noom_startswith(parser->code + n->source_offset, "..")) {
|
if (noom_startswith(parser->code + n->source_offset, "..")) {
|
||||||
if((result = noomC_compile_expr(vm, compiler, parser, func, n->subnodes[0]))) return result;
|
if ((result = noomC_compile_expr(vm, compiler, parser, func, n->subnodes[0]))) return result;
|
||||||
n = n->subnodes[1];
|
n = n->subnodes[1];
|
||||||
} else {
|
} else {
|
||||||
if((result = noomC_compile_expr(vm, compiler, parser, func, n))) return result;
|
if ((result = noomC_compile_expr(vm, compiler, parser, func, n))) return result;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
compiler->curstack -= amount;
|
compiler->curstack -= amount;
|
||||||
compiler->curstack++;
|
compiler->curstack++;
|
||||||
return noomC_emit_Aus(func, NOOMV_INSTR_CONCAT, 0, amount-1);
|
return noomC_emit_Aus(func, NOOMV_INSTR_CONCAT, 0, amount - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]))) return result;
|
if ((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]))) return result;
|
||||||
if((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[1]))) return result;
|
if ((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[1]))) return result;
|
||||||
// this consumes 2 operands and pushes 1 value, thus having a net stack effect of removing 1 item
|
// this consumes 2 operands and pushes 1 value, thus having a net stack effect of removing 1 item
|
||||||
compiler->curstack--;
|
compiler->curstack--;
|
||||||
return noomC_emit_ABC(func, NOOMV_INSTR_OP, 1, noomC_what_bop_is_this(parser, node->source_offset), 0);
|
return noomC_emit_ABC(func, NOOMV_INSTR_OP, 1, noomC_what_bop_is_this(parser, node->source_offset), 0);
|
||||||
@@ -244,21 +245,21 @@ static noom_Exit noomC_compile_expr(
|
|||||||
}
|
}
|
||||||
return noomC_emit_Aus(func, NOOMV_INSTR_JMP, 0, /* where do i start......... */ 0);
|
return noomC_emit_Aus(func, NOOMV_INSTR_JMP, 0, /* where do i start......... */ 0);
|
||||||
}
|
}
|
||||||
if(node->type == NOOMP_NODE_VARIABLE) {
|
if (node->type == NOOMP_NODE_VARIABLE) {
|
||||||
noomC_LocalInfo info;
|
noomC_LocalInfo info;
|
||||||
const char *varname = parser->code + node->source_offset;
|
const char* varname = parser->code + node->source_offset;
|
||||||
noom_uint_t namelen = noomL_tokenlen(parser->code, node->source_offset, parser->version);
|
noom_uint_t namelen = noomL_tokenlen(parser->code, node->source_offset, parser->version);
|
||||||
if((result = noomC_identifyLocal(compiler, &info, varname, namelen))) return result;
|
if ((result = noomC_identifyLocal(compiler, &info, varname, namelen))) return result;
|
||||||
compiler->curstack++;
|
compiler->curstack++;
|
||||||
|
|
||||||
switch(info.type) {
|
switch (info.type) {
|
||||||
case NOOMC_LOCAL:
|
case NOOMC_LOCAL:
|
||||||
return noomC_emit_Aus(func, NOOMV_INSTR_PUSHVAL, 0, info.idx);
|
return noomC_emit_Aus(func, NOOMV_INSTR_PUSHVAL, 0, info.idx);
|
||||||
case NOOMC_UPVAL:
|
case NOOMC_UPVAL:
|
||||||
return noomC_emit_Aus(func, NOOMV_INSTR_PUSHUPVAL, 0, info.idx);
|
return noomC_emit_Aus(func, NOOMV_INSTR_PUSHUPVAL, 0, info.idx);
|
||||||
case NOOMC_GLOBAL:
|
case NOOMC_GLOBAL:
|
||||||
// FIXME: handle globals
|
// FIXME: handle globals
|
||||||
return NOOM_EINTERNAL;
|
return NOOM_EINTERNAL;
|
||||||
}
|
}
|
||||||
// forgot a case
|
// forgot a case
|
||||||
return NOOM_EINTERNAL;
|
return NOOM_EINTERNAL;
|
||||||
@@ -266,9 +267,9 @@ static noom_Exit noomC_compile_expr(
|
|||||||
return NOOM_EINTERNAL;
|
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_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) {
|
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;
|
noom_Exit r = NOOM_OK;
|
||||||
for (noom_uint_t i = 0; i < node->subnodec; i++) {
|
for (noom_uint_t i = 0; i < node->subnodec; i++) {
|
||||||
r = noomC_add_stuff_to_function(vm, compiler, parser, func, node->subnodes[i]);
|
r = noomC_add_stuff_to_function(vm, compiler, parser, func, node->subnodes[i]);
|
||||||
@@ -282,7 +283,7 @@ static noom_Exit noomC_compile_block(noom_LuaVM *vm, noomC_Compiler *compiler, c
|
|||||||
// If you think too much you will get headaches
|
// 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
|
// 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) {
|
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) {
|
||||||
noom_Exit result;
|
noom_Exit result;
|
||||||
// local name <attr> = expression i think
|
// local name <attr> = expression i think
|
||||||
if (node->type == NOOMP_NODE_LOCALDECLARATION) {
|
if (node->type == NOOMP_NODE_LOCALDECLARATION) {
|
||||||
@@ -290,10 +291,10 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
|||||||
// This code is awful for several reasons and we shall burn it
|
// This code is awful for several reasons and we shall burn it
|
||||||
if (node->subnodec != 1 && node->subnodec != 2) return NOOM_EINTERNAL;
|
if (node->subnodec != 1 && node->subnodec != 2) return NOOM_EINTERNAL;
|
||||||
|
|
||||||
const noomP_Node *varname_node = node->subnodes[0];
|
const noomP_Node* varname_node = node->subnodes[0];
|
||||||
if (varname_node->type != NOOMP_NODE_ASSIGNPLACE) return NOOM_EINTERNAL;
|
if (varname_node->type != NOOMP_NODE_ASSIGNPLACE) return NOOM_EINTERNAL;
|
||||||
|
|
||||||
const char *namebuf = parser->code + varname_node->source_offset;
|
const char* namebuf = parser->code + varname_node->source_offset;
|
||||||
|
|
||||||
noomC_Local local;
|
noomC_Local local;
|
||||||
local.startpc = func->codesize;
|
local.startpc = func->codesize;
|
||||||
@@ -306,19 +307,17 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
|||||||
|
|
||||||
for (noom_uint_t i = 0; i < varname_node->subnodec; i++) {
|
for (noom_uint_t i = 0; i < varname_node->subnodec; i++) {
|
||||||
if (varname_node->subnodes[i]->type != NOOMP_NODE_ATTRIBUTE) return NOOM_EINTERNAL;
|
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);
|
noomL_Token attr_token = noomC_token_at(parser, varname_node->subnodes[i]->source_offset);
|
||||||
const char *ap = parser->code + attr_token.offset;
|
const char* ap = parser->code + attr_token.offset;
|
||||||
noom_uint_t al = attr_token.length;
|
noom_uint_t al = attr_token.length;
|
||||||
if (noom_memeq(ap, al, "close", 5)) {
|
if (noom_memeq(ap, al, "close", 5)) {
|
||||||
if (local.close) return NOOM_EINTERNAL;
|
if (local.close) return NOOM_EINTERNAL;
|
||||||
local.close = 1;
|
local.close = 1;
|
||||||
}
|
} else if (noom_memeq(ap, al, "const", 5)) {
|
||||||
else if (noom_memeq(ap, al, "const", 5)) {
|
|
||||||
if (local.constant) return NOOM_EINTERNAL;
|
if (local.constant) return NOOM_EINTERNAL;
|
||||||
local.constant = 1;
|
local.constant = 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return NOOM_EINTERNAL;
|
return NOOM_EINTERNAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -331,9 +330,8 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
|||||||
result = noomC_emit_Aus(func, NOOMV_INSTR_PUSHNIL, 0, 0);
|
result = noomC_emit_Aus(func, NOOMV_INSTR_PUSHNIL, 0, 0);
|
||||||
if (result != NOOM_OK) return result;
|
if (result != NOOM_OK) return result;
|
||||||
compiler->curstack++;
|
compiler->curstack++;
|
||||||
}
|
} else {
|
||||||
else {
|
const noomP_Node* value_node = node->subnodes[1];
|
||||||
const noomP_Node *value_node = node->subnodes[1];
|
|
||||||
const noom_Exit r = noomC_compile_expr(vm, compiler, parser, func, value_node);
|
const noom_Exit r = noomC_compile_expr(vm, compiler, parser, func, value_node);
|
||||||
if (r != NOOM_OK) return r;
|
if (r != NOOM_OK) return r;
|
||||||
}
|
}
|
||||||
@@ -347,15 +345,15 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
|||||||
if (node->type == NOOMP_NODE_LOCALFUNCTIONDECLARATION) {
|
if (node->type == NOOMP_NODE_LOCALFUNCTIONDECLARATION) {
|
||||||
if (node->subnodec != 3) return NOOM_EINTERNAL;
|
if (node->subnodec != 3) return NOOM_EINTERNAL;
|
||||||
// Funny staircase huh
|
// Funny staircase huh
|
||||||
const noomP_Node *varname_node = node->subnodes[0];
|
const noomP_Node* varname_node = node->subnodes[0];
|
||||||
const noomP_Node *params_node = node->subnodes[1];
|
const noomP_Node* params_node = node->subnodes[1];
|
||||||
const noomP_Node *block_node = node->subnodes[2];
|
const noomP_Node* block_node = node->subnodes[2];
|
||||||
if (varname_node->type != NOOMP_NODE_VARNAME) return NOOM_EINTERNAL;
|
if (varname_node->type != NOOMP_NODE_VARNAME) return NOOM_EINTERNAL;
|
||||||
if (params_node->type != NOOMP_NODE_FUNCTIONPARAMETERS) return NOOM_EINTERNAL;
|
if (params_node->type != NOOMP_NODE_FUNCTIONPARAMETERS) return NOOM_EINTERNAL;
|
||||||
if (block_node->type != NOOMP_NODE_BLOCK) 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);
|
const noomL_Token name_token = noomC_token_at(parser, varname_node->source_offset);
|
||||||
noomV_Function *proto = noomV_allocFunc(vm, func->chunkname);
|
noomV_Function* proto = noomV_allocFunc(vm, func->chunkname);
|
||||||
if (proto == 0) return NOOM_ENOMEM;
|
if (proto == 0) return NOOM_ENOMEM;
|
||||||
|
|
||||||
proto->argc = (unsigned char)params_node->subnodec;
|
proto->argc = (unsigned char)params_node->subnodec;
|
||||||
@@ -368,7 +366,7 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
|||||||
const noom_Exit result = noomC_compile_block(vm, &child, parser, proto, block_node);
|
const noom_Exit result = noomC_compile_block(vm, &child, parser, proto, block_node);
|
||||||
if (result != NOOM_OK) return result;
|
if (result != NOOM_OK) return result;
|
||||||
|
|
||||||
noomV_Function **newProtos = noom_realloc(func->protos, sizeof(noomV_Function *) * (func->protosize + 1));
|
noomV_Function** newProtos = noom_realloc(func->protos, sizeof(noomV_Function*) * (func->protosize + 1));
|
||||||
if (newProtos == 0) return NOOM_ENOMEM;
|
if (newProtos == 0) return NOOM_ENOMEM;
|
||||||
func->protos = newProtos;
|
func->protos = newProtos;
|
||||||
func->protos[func->protosize++] = proto;
|
func->protos[func->protosize++] = proto;
|
||||||
@@ -381,7 +379,7 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
|||||||
local.constant = 0;
|
local.constant = 0;
|
||||||
local.name = parser->code + varname_node->source_offset;
|
local.name = parser->code + varname_node->source_offset;
|
||||||
local.namelen = noomL_tokenlen(parser->code, varname_node->source_offset, parser->version);
|
local.namelen = noomL_tokenlen(parser->code, varname_node->source_offset, parser->version);
|
||||||
|
|
||||||
return noomC_addLocal(compiler, local);
|
return noomC_addLocal(compiler, local);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,17 +396,17 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
|||||||
return NOOM_EINTERNAL;
|
return NOOM_EINTERNAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
noom_Exit noomC_compile(noom_LuaVM *vm, const noomP_Parser *parser, const noomP_Node *node, noomV_String *chunkname, noomV_Table *env) {
|
noom_Exit noomC_compile(noom_LuaVM* vm, const noomP_Parser* parser, const noomP_Node* node, noomV_String* chunkname, noomV_Table* env) {
|
||||||
if (node->type != NOOMP_NODE_PROGRAM) return NOOM_EINTERNAL;
|
if (node->type != NOOMP_NODE_PROGRAM) return NOOM_EINTERNAL;
|
||||||
|
|
||||||
noomV_Function* program = noomV_allocFunc(vm, chunkname);
|
noomV_Function* program = noomV_allocFunc(vm, chunkname);
|
||||||
if(program == 0) return NOOM_ENOMEM;
|
if (program == 0) return NOOM_ENOMEM;
|
||||||
|
|
||||||
noomC_Compiler compiler;
|
noomC_Compiler compiler;
|
||||||
noomC_compiler_init(&compiler);
|
noomC_compiler_init(&compiler);
|
||||||
compiler.target = program;
|
compiler.target = program;
|
||||||
|
|
||||||
if(parser->version == NOOM_VERSION_51) {
|
if (parser->version == NOOM_VERSION_51) {
|
||||||
program->env = env;
|
program->env = env;
|
||||||
} else {
|
} else {
|
||||||
noomC_Upval upval;
|
noomC_Upval upval;
|
||||||
@@ -419,19 +417,19 @@ noom_Exit noomC_compile(noom_LuaVM *vm, const noomP_Parser *parser, const noomP_
|
|||||||
upval.constant = 0;
|
upval.constant = 0;
|
||||||
noomC_addUpval(&compiler, upval);
|
noomC_addUpval(&compiler, upval);
|
||||||
}
|
}
|
||||||
|
|
||||||
noom_Exit status = noomC_compile_block(vm, &compiler, parser, program, node);
|
noom_Exit status = noomC_compile_block(vm, &compiler, parser, program, node);
|
||||||
if (status != NOOM_OK) return status;
|
if (status != NOOM_OK) return status;
|
||||||
|
|
||||||
if(parser->version > NOOM_VERSION_51) {
|
if (parser->version > NOOM_VERSION_51) {
|
||||||
// TODO: we need to wrap the value as a *closure*, with one upvalue, the environment (_ENV).
|
// TODO: we need to wrap the value as a *closure*, with one upvalue, the environment (_ENV).
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vm->mainThread->stacklen == vm->mainThread->stackcap) {
|
if (vm->mainThread->stacklen == vm->mainThread->stackcap) {
|
||||||
vm->mainThread->stackcap = vm->mainThread->stackcap ? vm->mainThread->stackcap * 2 : 16;
|
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 = noom_realloc(vm->mainThread->stack, sizeof(noomV_Value) * vm->mainThread->stackcap);
|
||||||
}
|
}
|
||||||
vm->mainThread->stack[vm->mainThread->stacklen++] =
|
vm->mainThread->stack[vm->mainThread->stacklen++] =
|
||||||
(noomV_Value){.tag = NOOMV_VOBJ, .autoclose = 0, .isptr = 0, .obj = (noomV_Object*)program};
|
(noomV_Value){.tag = NOOMV_VOBJ, .autoclose = 0, .isptr = 0, .obj = (noomV_Object*)program};
|
||||||
return NOOM_OK;
|
return NOOM_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
131
src/error.c
131
src/error.c
@@ -8,64 +8,64 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const struct noom_error parser_errors[] = {
|
static const struct noom_error parser_errors[] = {
|
||||||
[NOOMP_ERROR_NONE] = {0, 0},
|
[NOOMP_ERROR_NONE] = {0, 0},
|
||||||
|
|
||||||
[NOOMP_ERROR_OOM] = {"Whoops! Out of memory :(\n", 0},
|
[NOOMP_ERROR_OOM] = {"Whoops! Out of memory :(\n", 0},
|
||||||
[NOOMP_ERROR_EXPECTED_LCURLY] = {"expected '{'", 1},
|
[NOOMP_ERROR_EXPECTED_LCURLY] = {"expected '{'", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_RCURLY] = {"expected '}' to close table literal", 1},
|
[NOOMP_ERROR_EXPECTED_RCURLY] = {"expected '}' to close table literal", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_KEY] = {"expected ']' to close computed key", 1},
|
[NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_KEY] = {"expected ']' to close computed key", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_EQUALS_AFTER_KEY] = {"expected '=' after table key", 1},
|
[NOOMP_ERROR_EXPECTED_EQUALS_AFTER_KEY] = {"expected '=' after table key", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_FIELD_IDENTIFIER] = {"expected identifier after '.' for field access", 1},
|
[NOOMP_ERROR_EXPECTED_FIELD_IDENTIFIER] = {"expected identifier after '.' for field access", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_METHOD_CALL] = {"expected identifier after ':' for method call", 1},
|
[NOOMP_ERROR_EXPECTED_METHOD_CALL] = {"expected identifier after ':' for method call", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_INDEX] = {"expected ']' after index expression", 1},
|
[NOOMP_ERROR_EXPECTED_RBRACKET_AFTER_INDEX] = {"expected ']' after index expression", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION] = {"expected ')' after expression", 1},
|
[NOOMP_ERROR_EXPECTED_RPAREN_AFTER_EXPRESSION] = {"expected ')' after expression", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_LPAREN_FOR_PARAMETERS] = {"expected '(' for function parameters", 1},
|
[NOOMP_ERROR_EXPECTED_LPAREN_FOR_PARAMETERS] = {"expected '(' for function parameters", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_RPAREN_FOR_PARAMETERS] = {"expected ')' for function parameters", 1},
|
[NOOMP_ERROR_EXPECTED_RPAREN_FOR_PARAMETERS] = {"expected ')' for function parameters", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LOCAL_FUNCTION] = {"expected identifier after 'local function'\n", 0},
|
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LOCAL_FUNCTION] = {"expected identifier after 'local function'\n", 0},
|
||||||
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_FUNCTION] = {"expected identifier after 'function'\n", 0},
|
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_FUNCTION] = {"expected identifier after 'function'\n", 0},
|
||||||
[NOOMP_ERROR_EXPECTED_END_AFTER_LOCAL_FUNCTION] = {"expected 'end' to close local function declaration", 1},
|
[NOOMP_ERROR_EXPECTED_END_AFTER_LOCAL_FUNCTION] = {"expected 'end' to close local function declaration", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_END_AFTER_FUNCTION] = {"expected 'end' to close function declaration", 1},
|
[NOOMP_ERROR_EXPECTED_END_AFTER_FUNCTION] = {"expected 'end' to close function declaration", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LOCAL] = {"expected identifier after 'local'\n", 0},
|
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LOCAL] = {"expected identifier after 'local'\n", 0},
|
||||||
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LANGLE] = {"expected identifier after '<' for attribute\n", 0},
|
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LANGLE] = {"expected identifier after '<' for attribute\n", 0},
|
||||||
[NOOMP_ERROR_EXPECTED_RANGLE_TO_CLOSE_ATTRIBUTE] = {"expected '>' to close attribute after identifier", 1},
|
[NOOMP_ERROR_EXPECTED_RANGLE_TO_CLOSE_ATTRIBUTE] = {"expected '>' to close attribute after identifier", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_THEN_AFTER_EXPRESSION] = {"expected 'then' after expression", 1},
|
[NOOMP_ERROR_EXPECTED_THEN_AFTER_EXPRESSION] = {"expected 'then' after expression", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_ELSE_ELSEIF_OR_END] = {"expected 'else', 'elseif', or 'end'", 1},
|
[NOOMP_ERROR_EXPECTED_ELSE_ELSEIF_OR_END] = {"expected 'else', 'elseif', or 'end'", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_END_AFTER_IF] = {"expected 'end' to close if statement", 1},
|
[NOOMP_ERROR_EXPECTED_END_AFTER_IF] = {"expected 'end' to close if statement", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_DO_AFTER_EXPRESSION] = {"expected 'do' after expression", 1},
|
[NOOMP_ERROR_EXPECTED_DO_AFTER_EXPRESSION] = {"expected 'do' after expression", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_END_AFTER_WHILE] = {"expected 'end' to close while statement", 1},
|
[NOOMP_ERROR_EXPECTED_END_AFTER_WHILE] = {"expected 'end' to close while statement", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_END_AFTER_FOR] = {"expected 'end' to close for statement", 1},
|
[NOOMP_ERROR_EXPECTED_END_AFTER_FOR] = {"expected 'end' to close for statement", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_END_AFTER_DO] = {"expected 'end' to close do statement", 1},
|
[NOOMP_ERROR_EXPECTED_END_AFTER_DO] = {"expected 'end' to close do statement", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_FOR] = {"expected identifier after 'for'\n", 1},
|
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_FOR] = {"expected identifier after 'for'\n", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_VARIABLE_AFTER_COMMA_IN_FOR] = {"expected variable name after ',' in for loop\n", 0},
|
[NOOMP_ERROR_EXPECTED_VARIABLE_AFTER_COMMA_IN_FOR] = {"expected variable name after ',' in for loop\n", 0},
|
||||||
[NOOMP_ERROR_EXPECTED_IN] = {"expected 'in'", 1},
|
[NOOMP_ERROR_EXPECTED_IN] = {"expected 'in'", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_UNTIL] = {"expected 'until' to close repeat expression", 1},
|
[NOOMP_ERROR_EXPECTED_UNTIL] = {"expected 'until' to close repeat expression", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_GOTO] = {"expected identifier after goto\n", 0},
|
[NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_GOTO] = {"expected identifier after goto\n", 0},
|
||||||
[NOOMP_ERROR_EXPECTED_COLONCOLON] = {"expected :: to end label identifier", 1},
|
[NOOMP_ERROR_EXPECTED_COLONCOLON] = {"expected :: to end label identifier", 1},
|
||||||
[NOOMP_ERROR_INVALID_STATEMENT] = {"expected statement, got", 2},
|
[NOOMP_ERROR_INVALID_STATEMENT] = {"expected statement, got", 2},
|
||||||
// I want someone smarter than me [tema5002] to give these a proper description
|
// I want someone smarter than me [tema5002] to give these a proper description
|
||||||
// ^ alrighty then
|
// ^ alrighty then
|
||||||
[NOOMP_ERROR_EXPECTED_ASSIGNABLE] = {"expected assignable expression after comma in assignment", 1},
|
[NOOMP_ERROR_EXPECTED_ASSIGNABLE] = {"expected assignable expression after comma in assignment", 1},
|
||||||
[NOOMP_ERROR_NOT_ASSIGNABLE] = {"expression in assignment is not assignable", 1},
|
[NOOMP_ERROR_NOT_ASSIGNABLE] = {"expression in assignment is not assignable", 1},
|
||||||
[NOOMP_ERROR_EXPECTED_COMMA_OR_EQUAL_IN_ASSIGNMENT] = {"expected a comma or equals after assignable in assignment", 1},
|
[NOOMP_ERROR_EXPECTED_COMMA_OR_EQUAL_IN_ASSIGNMENT] = {"expected a comma or equals after assignable in assignment", 1},
|
||||||
[NOOMP_ERROR_EXPRESSION_NOT_STATEMENT] = {"loose expression is not a valid statement", 1},
|
[NOOMP_ERROR_EXPRESSION_NOT_STATEMENT] = {"loose expression is not a valid statement", 1},
|
||||||
[NOOMP_ERROR_FAKEATTRIBUTE] = {"invalid attribute", 2},
|
[NOOMP_ERROR_FAKEATTRIBUTE] = {"invalid attribute", 2},
|
||||||
[NOOMP_ERROR_RETURN_NOT_END] = {"'return' must be the last statement in a block\n", 0},
|
[NOOMP_ERROR_RETURN_NOT_END] = {"'return' must be the last statement in a block\n", 0},
|
||||||
[NOOMP_ERROR_FOR_WRONG_AMOUNT] = {"'for' initializer must have 2 or 3 expressions\n", 0}
|
[NOOMP_ERROR_FOR_WRONG_AMOUNT] = {"'for' initializer must have 2 or 3 expressions\n", 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct noom_error lexer_errors[] = {
|
static const struct noom_error lexer_errors[] = {
|
||||||
[NOOML_ERROR_NONE] = {0, 0},
|
[NOOML_ERROR_NONE] = {0, 0},
|
||||||
|
|
||||||
[NOOML_ERROR_UNKNOWN] = {"unknown token", 1},
|
[NOOML_ERROR_UNKNOWN] = {"unknown token", 1},
|
||||||
[NOOML_ERROR_MALFORMED_NUM] = {"malformed number", 1},
|
[NOOML_ERROR_MALFORMED_NUM] = {"malformed number", 1},
|
||||||
[NOOML_ERROR_UNFINISHED_COMMENT] = {"unfinished comment", 1},
|
[NOOML_ERROR_UNFINISHED_COMMENT] = {"unfinished comment", 1},
|
||||||
[NOOML_ERROR_UNFINISHED_STRING] = {"unfinished string", 1},
|
[NOOML_ERROR_UNFINISHED_STRING] = {"unfinished string", 1},
|
||||||
[NOOML_ERROR_UNFINISHED_LONG_STRING] = {"unfinished long string", 1},
|
[NOOML_ERROR_UNFINISHED_LONG_STRING] = {"unfinished long string", 1},
|
||||||
[NOOML_ERROR_DECIMAL_ESCAPE_TOO_BIG] = {"decimal escape sequence too big (max 255)", 1},
|
[NOOML_ERROR_DECIMAL_ESCAPE_TOO_BIG] = {"decimal escape sequence too big (max 255)", 1},
|
||||||
[NOOML_ERROR_HEX_ESCAPE_INVALID] = {"invalid hexadecimal escape sequence", 1},
|
[NOOML_ERROR_HEX_ESCAPE_INVALID] = {"invalid hexadecimal escape sequence", 1},
|
||||||
[NOOML_ERROR_UNICODE_ESCAPE_UNOPENED] = {"expected '{' after '\\u' for Unicode escape", 1},
|
[NOOML_ERROR_UNICODE_ESCAPE_UNOPENED] = {"expected '{' after '\\u' for Unicode escape", 1},
|
||||||
[NOOML_ERROR_UNICODE_ESCAPE_UNCLOSED] = {"expected '}' to close Unicode escape sequence", 1},
|
[NOOML_ERROR_UNICODE_ESCAPE_UNCLOSED] = {"expected '}' to close Unicode escape sequence", 1},
|
||||||
[NOOML_ERROR_UNICODE_ESCAPE_TOO_BIG] = {"Unicode escape sequence exceeds maximum (0x10FFFF)", 1},
|
[NOOML_ERROR_UNICODE_ESCAPE_TOO_BIG] = {"Unicode escape sequence exceeds maximum (0x10FFFF)", 1},
|
||||||
};
|
};
|
||||||
|
|
||||||
noomP_Error base_err = parser->error_state;
|
noomP_Error base_err = parser->error_state;
|
||||||
@@ -84,8 +84,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
|||||||
if (parser->code[i] == '\n') {
|
if (parser->code[i] == '\n') {
|
||||||
row++;
|
row++;
|
||||||
column = 1;
|
column = 1;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
column++;
|
column++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,16 +98,16 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
|||||||
if (program_name) space +=
|
if (program_name) space +=
|
||||||
noom_strlen(program_name) +
|
noom_strlen(program_name) +
|
||||||
sizeof(": ") - 1;
|
sizeof(": ") - 1;
|
||||||
|
|
||||||
space +=
|
|
||||||
noom_strlen(parser->filename) +
|
|
||||||
sizeof(":") - 1 +
|
|
||||||
linedig +
|
|
||||||
sizeof(": ") - 1 +
|
|
||||||
noom_strlen(err.s) +
|
|
||||||
+ 1; // \0;
|
|
||||||
|
|
||||||
if (err.near) space +=
|
space +=
|
||||||
|
noom_strlen(parser->filename) +
|
||||||
|
sizeof(":") - 1 +
|
||||||
|
linedig +
|
||||||
|
sizeof(": ") - 1 +
|
||||||
|
noom_strlen(err.s) +
|
||||||
|
+1; // \0;
|
||||||
|
|
||||||
|
if (err.near) 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 +
|
||||||
@@ -126,7 +125,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
|||||||
|
|
||||||
char num_buf[20];
|
char num_buf[20];
|
||||||
noom_uint_t num_len = 0;
|
noom_uint_t num_len = 0;
|
||||||
|
|
||||||
if (row == 0) {
|
if (row == 0) {
|
||||||
num_buf[num_len++] = '0';
|
num_buf[num_len++] = '0';
|
||||||
} else {
|
} else {
|
||||||
@@ -156,7 +155,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
|||||||
}
|
}
|
||||||
noom_safe_strcpy(buffer, &pos, buffer_size, "'\n");
|
noom_safe_strcpy(buffer, &pos, buffer_size, "'\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos < buffer_size) buffer[pos] = '\0';
|
if (pos < buffer_size) buffer[pos] = '\0';
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/helper.c
14
src/helper.c
@@ -27,18 +27,18 @@ int noom_memeq(const char* stra, noom_uint_t lena, const char* strb, noom_uint_t
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
noom_uint_t noom_strlen(const char *s) {
|
noom_uint_t noom_strlen(const char* s) {
|
||||||
#ifdef __has_builtin
|
#ifdef __has_builtin
|
||||||
#if __has_builtin(__builtin_strlen)
|
#if __has_builtin(__builtin_strlen)
|
||||||
return __builtin_strlen(s);
|
return __builtin_strlen(s);
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
const char *a = s;
|
const char* a = s;
|
||||||
while (*s) s++;
|
while (*s) s++;
|
||||||
return s - a;
|
return s - a;
|
||||||
}
|
}
|
||||||
|
|
||||||
int noom_strcmp(const char *a, const char *b) {
|
int noom_strcmp(const char* a, const char* b) {
|
||||||
#ifdef __has_builtin
|
#ifdef __has_builtin
|
||||||
#if __has_builtin(__builtin_strcmp)
|
#if __has_builtin(__builtin_strcmp)
|
||||||
return __builtin_strcmp(a, b);
|
return __builtin_strcmp(a, b);
|
||||||
@@ -54,8 +54,8 @@ 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* noom_strndup(const char* s, const noom_uint_t len) {
|
||||||
char *whar = noom_alloc(len + 1);
|
char* whar = noom_alloc(len + 1);
|
||||||
if (whar == 0) return 0;
|
if (whar == 0) return 0;
|
||||||
noom_memcpy(whar, s, len);
|
noom_memcpy(whar, s, len);
|
||||||
whar[len] = '\0';
|
whar[len] = '\0';
|
||||||
@@ -69,8 +69,8 @@ void noom_memcpy(void* dst, const void* src, noom_uint_t n) {
|
|||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
unsigned char *d = dst;
|
unsigned char* d = dst;
|
||||||
const unsigned char *s = src;
|
const unsigned char* s = src;
|
||||||
|
|
||||||
while (n--) {
|
while (n--) {
|
||||||
*d++ = *s++;
|
*d++ = *s++;
|
||||||
|
|||||||
94
src/lexer.c
94
src/lexer.c
@@ -31,7 +31,7 @@ int noomL_ishex(char c) {
|
|||||||
|
|
||||||
noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version) { // TODO: maybe find some less shit crap holy crap
|
noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version) { // TODO: maybe find some less shit crap holy crap
|
||||||
if (noom_startswith(s, "...")) return 3;
|
if (noom_startswith(s, "...")) return 3;
|
||||||
|
|
||||||
if (noom_startswith(s, "==")) return 2;
|
if (noom_startswith(s, "==")) return 2;
|
||||||
if (noom_startswith(s, "~=")) return 2;
|
if (noom_startswith(s, "~=")) return 2;
|
||||||
if (noom_startswith(s, "<=")) return 2;
|
if (noom_startswith(s, "<=")) return 2;
|
||||||
@@ -47,12 +47,12 @@ noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version) { // TODO: m
|
|||||||
|
|
||||||
if (noom_startswith(s, ">>")) return 2;
|
if (noom_startswith(s, ">>")) return 2;
|
||||||
if (noom_startswith(s, "<<")) return 2;
|
if (noom_startswith(s, "<<")) return 2;
|
||||||
|
|
||||||
if (noom_startswith(s, "&")) return 1;
|
if (noom_startswith(s, "&")) return 1;
|
||||||
if (noom_startswith(s, "|")) return 1;
|
if (noom_startswith(s, "|")) return 1;
|
||||||
if (noom_startswith(s, "~")) return 1;
|
if (noom_startswith(s, "~")) return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noom_startswith(s, "+")) return 1;
|
if (noom_startswith(s, "+")) return 1;
|
||||||
if (noom_startswith(s, "-")) return 1;
|
if (noom_startswith(s, "-")) return 1;
|
||||||
if (noom_startswith(s, "*")) return 1;
|
if (noom_startswith(s, "*")) return 1;
|
||||||
@@ -62,10 +62,10 @@ noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version) { // TODO: m
|
|||||||
if (noom_startswith(s, "#")) return 1;
|
if (noom_startswith(s, "#")) return 1;
|
||||||
if (noom_startswith(s, "<")) return 1;
|
if (noom_startswith(s, "<")) return 1;
|
||||||
if (noom_startswith(s, ">")) return 1;
|
if (noom_startswith(s, ">")) return 1;
|
||||||
|
|
||||||
if (noom_startswith(s, "=")) return 1;
|
if (noom_startswith(s, "=")) return 1;
|
||||||
if (noom_startswith(s, ",")) return 1;
|
if (noom_startswith(s, ",")) return 1;
|
||||||
|
|
||||||
if (noom_startswith(s, "(")) return 1;
|
if (noom_startswith(s, "(")) return 1;
|
||||||
if (noom_startswith(s, ")")) return 1;
|
if (noom_startswith(s, ")")) return 1;
|
||||||
if (noom_startswith(s, "{")) return 1;
|
if (noom_startswith(s, "{")) return 1;
|
||||||
@@ -75,7 +75,7 @@ noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version) { // TODO: m
|
|||||||
|
|
||||||
if (noom_startswith(s, ":")) return 1;
|
if (noom_startswith(s, ":")) return 1;
|
||||||
if (noom_startswith(s, ".")) return 1;
|
if (noom_startswith(s, ".")) return 1;
|
||||||
|
|
||||||
if (noom_startswith(s, ";")) return 1;
|
if (noom_startswith(s, ";")) return 1;
|
||||||
|
|
||||||
return 0; // no symbol
|
return 0; // no symbol
|
||||||
@@ -83,7 +83,7 @@ noom_uint_t noomL_getsymbol(const char* s, noom_LuaVersion version) { // TODO: m
|
|||||||
|
|
||||||
noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersion version) { // TODO: more number kinds idk
|
noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersion version) { // TODO: more number kinds idk
|
||||||
noom_uint_t len = 0;
|
noom_uint_t len = 0;
|
||||||
|
|
||||||
if (s[0] == '0' && noomL_lower(s[1]) == 'x') {
|
if (s[0] == '0' && noomL_lower(s[1]) == 'x') {
|
||||||
len = 2;
|
len = 2;
|
||||||
|
|
||||||
@@ -123,7 +123,6 @@ noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len == 2) { // nothing after the x; malformed number.
|
if (len == 2) { // nothing after the x; malformed number.
|
||||||
@@ -138,7 +137,7 @@ noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
} else {
|
} else {
|
||||||
while (noomL_isnumber(s[len])) { // int part
|
while (noomL_isnumber(s[len])) { // int part
|
||||||
@@ -165,7 +164,7 @@ noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
if (s[len] == '-' || s[len] == '+') len++;
|
if (s[len] == '-' || s[len] == '+') len++;
|
||||||
|
|
||||||
noom_uint_t slen = len;
|
noom_uint_t slen = len;
|
||||||
|
|
||||||
while (noomL_isnumber(s[len])) { // the exponent
|
while (noomL_isnumber(s[len])) { // the exponent
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
@@ -195,14 +194,17 @@ noom_uint_t noomL_getcomment(const char* str, noomL_ErrorType* error) {
|
|||||||
if (str[0] == '-' && str[1] == '-') {
|
if (str[0] == '-' && str[1] == '-') {
|
||||||
noom_uint_t len = 2;
|
noom_uint_t len = 2;
|
||||||
noom_uint_t longb_len = 0;
|
noom_uint_t longb_len = 0;
|
||||||
|
|
||||||
// check for long bracket
|
// check for long bracket
|
||||||
int is_long = 0; // int for bools :fire:
|
int is_long = 0; // int for bools :fire:
|
||||||
|
|
||||||
if (str[len] == '[') {
|
if (str[len] == '[') {
|
||||||
len++;
|
len++;
|
||||||
|
|
||||||
while (str[len] == '=') { longb_len++; len++; }
|
while (str[len] == '=') {
|
||||||
|
longb_len++;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
if (str[len] == '[') {
|
if (str[len] == '[') {
|
||||||
// yay long bracket!
|
// yay long bracket!
|
||||||
@@ -223,7 +225,10 @@ noom_uint_t noomL_getcomment(const char* str, noomL_ErrorType* error) {
|
|||||||
noom_uint_t spos = len; // after the ] intentionally
|
noom_uint_t spos = len; // after the ] intentionally
|
||||||
noom_uint_t testlong = 0;
|
noom_uint_t testlong = 0;
|
||||||
|
|
||||||
while (str[len] == '=') { testlong++; len++; }
|
while (str[len] == '=') {
|
||||||
|
testlong++;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
if (str[len] == ']') { // actual long bracket! holy shit!
|
if (str[len] == ']') { // actual long bracket! holy shit!
|
||||||
len++;
|
len++;
|
||||||
@@ -245,7 +250,7 @@ noom_uint_t noomL_getcomment(const char* str, noomL_ErrorType* error) {
|
|||||||
len++; // just some character.
|
len++; // just some character.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// reset to remove stuff, in case we hit like --[===hello, technically not required but a good idea
|
// reset to remove stuff, in case we hit like --[===hello, technically not required but a good idea
|
||||||
len = 2;
|
len = 2;
|
||||||
@@ -263,7 +268,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
noom_uint_t len = 0;
|
noom_uint_t len = 0;
|
||||||
if (s[len] == '"' || s[len] == '\'') {
|
if (s[len] == '"' || s[len] == '\'') {
|
||||||
char starter = s[len]; // either `'` or `"`
|
char starter = s[len]; // either `'` or `"`
|
||||||
|
|
||||||
len++; // double quoted string
|
len++; // double quoted string
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
@@ -289,25 +294,26 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
len++;
|
len++;
|
||||||
} else if (s[len] == 'v') {
|
} else if (s[len] == 'v') {
|
||||||
len++;
|
len++;
|
||||||
|
|
||||||
// both string using single or double quote can have either escaped inside
|
// both string using single or double quote can have either escaped inside
|
||||||
} else if (s[len] == '"') {
|
} else if (s[len] == '"') {
|
||||||
len++;
|
len++;
|
||||||
} else if (s[len] == '\'') {
|
} else if (s[len] == '\'') {
|
||||||
len++;
|
len++;
|
||||||
} else if (s[len] == '\n') {
|
} else if (s[len] == '\n') {
|
||||||
len++;
|
len++;
|
||||||
} else if (s[len] == '\r' && s[len+1] == '\n') { // fuck windows :fire:
|
} else if (s[len] == '\r' && s[len + 1] == '\n') { // fuck windows :fire:
|
||||||
len += 2;
|
len += 2;
|
||||||
|
|
||||||
} else if (noomL_isnumber(s[len])) {
|
} else if (noomL_isnumber(s[len])) {
|
||||||
// fuckkkk
|
// fuckkkk
|
||||||
noom_uint_t count = 0;
|
noom_uint_t count = 0;
|
||||||
for (noom_uint_t i = 0; i < 3; i++) {
|
for (noom_uint_t i = 0; i < 3; i++) {
|
||||||
if (noomL_isnumber(s[len + i])) count++; else break;
|
if (noomL_isnumber(s[len + i])) count++;
|
||||||
|
else break;
|
||||||
}
|
}
|
||||||
if (count == 3) { // could be too big
|
if (count == 3) { // could be too big
|
||||||
if ((s[len] > '2') || (s[len] == '2' && (s[len+1] > '5' || (s[len+1] == '5' && s[len+2] > '5')))) {
|
if ((s[len] > '2') || (s[len] == '2' && (s[len + 1] > '5' || (s[len + 1] == '5' && s[len + 2] > '5')))) {
|
||||||
// >255, i could also make it a number first but meh
|
// >255, i could also make it a number first but meh
|
||||||
*error = NOOML_ERROR_DECIMAL_ESCAPE_TOO_BIG;
|
*error = NOOML_ERROR_DECIMAL_ESCAPE_TOO_BIG;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -315,11 +321,11 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
}
|
}
|
||||||
// count can't be 0 because this if wouldn't run.
|
// count can't be 0 because this if wouldn't run.
|
||||||
len += count;
|
len += count;
|
||||||
|
|
||||||
} else if (s[len] == 'x' && version >= NOOM_VERSION_52) {
|
} else if (s[len] == 'x' && version >= NOOM_VERSION_52) {
|
||||||
len++;
|
len++;
|
||||||
|
|
||||||
if ((!noomL_ishex(s[len])) || (!noomL_ishex(s[len+1]))) {
|
if ((!noomL_ishex(s[len])) || (!noomL_ishex(s[len + 1]))) {
|
||||||
*error = NOOML_ERROR_HEX_ESCAPE_INVALID;
|
*error = NOOML_ERROR_HEX_ESCAPE_INVALID;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -347,7 +353,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
// fuck my life
|
// fuck my life
|
||||||
if (version == NOOM_VERSION_53) {
|
if (version == NOOM_VERSION_53) {
|
||||||
if (hexlen == 6) {
|
if (hexlen == 6) {
|
||||||
if (s[len] > '1' || (s[len] == '1' && s[len+1] > '0')) {
|
if (s[len] > '1' || (s[len] == '1' && s[len + 1] > '0')) {
|
||||||
*error = NOOML_ERROR_UNICODE_ESCAPE_TOO_BIG;
|
*error = NOOML_ERROR_UNICODE_ESCAPE_TOO_BIG;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -386,7 +392,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
// unfinished because you can't have a newline in it
|
// unfinished because you can't have a newline in it
|
||||||
*error = NOOML_ERROR_UNFINISHED_STRING;
|
*error = NOOML_ERROR_UNFINISHED_STRING;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
len++; // anything else is just a thing in the string.
|
len++; // anything else is just a thing in the string.
|
||||||
}
|
}
|
||||||
@@ -397,9 +403,15 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
noom_uint_t order = 0;
|
noom_uint_t order = 0;
|
||||||
int succ = 0;
|
int succ = 0;
|
||||||
|
|
||||||
while (s[len] == '=') { order++; len++; }
|
while (s[len] == '=') {
|
||||||
|
order++;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
if (s[len] == '[') { len++; succ = 1; }
|
if (s[len] == '[') {
|
||||||
|
len++;
|
||||||
|
succ = 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (succ) { // it is a multi-line string.
|
if (succ) { // it is a multi-line string.
|
||||||
while (1) {
|
while (1) {
|
||||||
@@ -408,7 +420,10 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
noom_uint_t order2 = 0;
|
noom_uint_t order2 = 0;
|
||||||
noom_uint_t startp = len; // intentionally after the `]`
|
noom_uint_t startp = len; // intentionally after the `]`
|
||||||
|
|
||||||
while (s[len] == '=') { order2++; len++; }
|
while (s[len] == '=') {
|
||||||
|
order2++;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
|
||||||
if (s[len] == ']' && order == order2) { // holy shit it's real
|
if (s[len] == ']' && order == order2) { // holy shit it's real
|
||||||
len++;
|
len++;
|
||||||
@@ -418,7 +433,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
// nope.
|
// nope.
|
||||||
len = startp; // go back just in case like ]=]==]
|
len = startp; // go back just in case like ]=]==]
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (s[len] == '\0') {
|
} else if (s[len] == '\0') {
|
||||||
*error = NOOML_ERROR_UNFINISHED_LONG_STRING;
|
*error = NOOML_ERROR_UNFINISHED_LONG_STRING;
|
||||||
return 0;
|
return 0;
|
||||||
@@ -428,7 +443,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -436,18 +451,18 @@ int noomL_iskeyword(const char* s, noom_uint_t len, noom_LuaVersion version) {
|
|||||||
if (noom_memeq(s, len, "true", 4)) return 1;
|
if (noom_memeq(s, len, "true", 4)) return 1;
|
||||||
if (noom_memeq(s, len, "false", 5)) return 1;
|
if (noom_memeq(s, len, "false", 5)) return 1;
|
||||||
if (noom_memeq(s, len, "nil", 3)) return 1;
|
if (noom_memeq(s, len, "nil", 3)) return 1;
|
||||||
|
|
||||||
if (noom_memeq(s, len, "if", 2)) return 1;
|
if (noom_memeq(s, len, "if", 2)) return 1;
|
||||||
if (noom_memeq(s, len, "then", 4)) return 1;
|
if (noom_memeq(s, len, "then", 4)) return 1;
|
||||||
if (noom_memeq(s, len, "else", 4)) return 1;
|
if (noom_memeq(s, len, "else", 4)) return 1;
|
||||||
if (noom_memeq(s, len, "elseif", 6)) return 1;
|
if (noom_memeq(s, len, "elseif", 6)) return 1;
|
||||||
|
|
||||||
if (noom_memeq(s, len, "and", 3)) return 1;
|
if (noom_memeq(s, len, "and", 3)) return 1;
|
||||||
if (noom_memeq(s, len, "or", 2)) return 1;
|
if (noom_memeq(s, len, "or", 2)) return 1;
|
||||||
if (noom_memeq(s, len, "not", 3)) return 1;
|
if (noom_memeq(s, len, "not", 3)) return 1;
|
||||||
|
|
||||||
if (noom_memeq(s, len, "local", 5)) return 1;
|
if (noom_memeq(s, len, "local", 5)) return 1;
|
||||||
|
|
||||||
if (noom_memeq(s, len, "for", 3)) return 1;
|
if (noom_memeq(s, len, "for", 3)) return 1;
|
||||||
if (noom_memeq(s, len, "function", 8)) return 1;
|
if (noom_memeq(s, len, "function", 8)) return 1;
|
||||||
if (noom_memeq(s, len, "do", 2)) return 1;
|
if (noom_memeq(s, len, "do", 2)) return 1;
|
||||||
@@ -462,11 +477,11 @@ int noomL_iskeyword(const char* s, noom_uint_t len, noom_LuaVersion version) {
|
|||||||
if (version >= NOOM_VERSION_52) {
|
if (version >= NOOM_VERSION_52) {
|
||||||
if (noom_memeq(s, len, "goto", 4)) return 1;
|
if (noom_memeq(s, len, "goto", 4)) return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *noomL_formatTokenType(noomL_TokenType token_type) {
|
const char* noomL_formatTokenType(noomL_TokenType token_type) {
|
||||||
switch (token_type) {
|
switch (token_type) {
|
||||||
case NOOML_TOKEN_EOF:
|
case NOOML_TOKEN_EOF:
|
||||||
return "EOF";
|
return "EOF";
|
||||||
@@ -487,7 +502,6 @@ const char *noomL_formatTokenType(noomL_TokenType token_type) {
|
|||||||
default:
|
default:
|
||||||
return "unknown";
|
return "unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token, noom_LuaVersion version) {
|
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token, noom_LuaVersion version) {
|
||||||
@@ -516,7 +530,7 @@ noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token,
|
|||||||
|
|
||||||
token->type = NOOML_TOKEN_IDENTIFIER;
|
token->type = NOOML_TOKEN_IDENTIFIER;
|
||||||
if (noomL_iskeyword(str, len, version)) token->type = NOOML_TOKEN_KEYWORD;
|
if (noomL_iskeyword(str, len, version)) token->type = NOOML_TOKEN_KEYWORD;
|
||||||
|
|
||||||
token->offset = start;
|
token->offset = start;
|
||||||
token->length = len;
|
token->length = len;
|
||||||
|
|
||||||
@@ -567,7 +581,7 @@ noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token,
|
|||||||
if (err != NOOML_ERROR_NONE) return err;
|
if (err != NOOML_ERROR_NONE) return err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
noom_uint_t symbolLen = noomL_getsymbol(str, version);
|
noom_uint_t symbolLen = noomL_getsymbol(str, version);
|
||||||
|
|
||||||
@@ -584,7 +598,7 @@ noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token,
|
|||||||
return NOOML_ERROR_UNKNOWN;
|
return NOOML_ERROR_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
noom_uint_t noomL_tokenlen(const char *s, noom_uint_t start, noom_LuaVersion version) {
|
noom_uint_t noomL_tokenlen(const char* s, noom_uint_t start, noom_LuaVersion version) {
|
||||||
noomL_Token t;
|
noomL_Token t;
|
||||||
noomL_lex(s, start, &t, version);
|
noomL_lex(s, start, &t, version);
|
||||||
return t.length;
|
return t.length;
|
||||||
|
|||||||
119
src/main.c
119
src/main.c
@@ -13,7 +13,7 @@ void tab(noom_uint_t amount) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_node(const noomP_Node *node, noom_uint_t depth) {
|
void print_node(const noomP_Node* node, noom_uint_t depth) {
|
||||||
tab(depth);
|
tab(depth);
|
||||||
printf("{\n");
|
printf("{\n");
|
||||||
|
|
||||||
@@ -34,10 +34,10 @@ 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 *program_name, 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;
|
||||||
|
|
||||||
// goodbye "shitass" you will be missed
|
// goodbye "shitass" you will be missed
|
||||||
int success = noomP_parse(code, filename, NOOM_VERSION_54, &program, &parser);
|
int success = noomP_parse(code, filename, NOOM_VERSION_54, &program, &parser);
|
||||||
if (success == 0) {
|
if (success == 0) {
|
||||||
@@ -82,19 +82,18 @@ int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(
|
|||||||
puts("\x1b[0m");
|
puts("\x1b[0m");
|
||||||
puts("PARSE OUTPUT:");
|
puts("PARSE OUTPUT:");
|
||||||
print_node(program, 0);
|
print_node(program, 0);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
noom_uint_t bleh = noom_format_error(&parser, program_name, 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, program_name, buf, bleh);
|
noom_format_error(&parser, program_name, buf, bleh);
|
||||||
fputs(buf, stdout);
|
fputs(buf, stdout);
|
||||||
noom_free(buf);
|
noom_free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// freeing time
|
// freeing time
|
||||||
noomP_Node *last_node = parser.last_node;
|
noomP_Node* last_node = parser.last_node;
|
||||||
while (last_node) {
|
while (last_node) {
|
||||||
noomP_Node *next = last_node->previous_node;
|
noomP_Node* next = last_node->previous_node;
|
||||||
// subnodes could be null if we OOM'd during a realloc of it
|
// subnodes could be null if we OOM'd during a realloc of it
|
||||||
if (last_node->subnodes) noom_free(last_node->subnodes);
|
if (last_node->subnodes) noom_free(last_node->subnodes);
|
||||||
noom_free(last_node);
|
noom_free(last_node);
|
||||||
@@ -121,7 +120,7 @@ static char* read_file(const char* filename) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
buffer[filesize] = '\0';
|
buffer[filesize] = '\0';
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
@@ -130,7 +129,7 @@ static char* read_stdin() {
|
|||||||
noom_uint_t capacity = 4096;
|
noom_uint_t capacity = 4096;
|
||||||
noom_uint_t size = 0;
|
noom_uint_t size = 0;
|
||||||
char* buffer = noom_alloc(capacity);
|
char* buffer = noom_alloc(capacity);
|
||||||
|
|
||||||
size_t n;
|
size_t n;
|
||||||
while ((n = fread(buffer + size, 1, capacity - size, stdin)) > 0) {
|
while ((n = fread(buffer + size, 1, capacity - size, stdin)) > 0) {
|
||||||
size += n;
|
size += n;
|
||||||
@@ -139,7 +138,7 @@ static char* read_stdin() {
|
|||||||
buffer = noom_realloc(buffer, capacity);
|
buffer = noom_realloc(buffer, capacity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer[size] = '\0';
|
buffer[size] = '\0';
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
@@ -152,17 +151,16 @@ static int read_prompt(char* buf, int buf_size, char* prompt, const int required
|
|||||||
if (!fgets(buf, buf_size, stdin)) return 1;
|
if (!fgets(buf, buf_size, stdin)) return 1;
|
||||||
const size_t len = noom_strlen(buf);
|
const size_t len = noom_strlen(buf);
|
||||||
if (len > 0 && buf[len - 1] != '\n') {
|
if (len > 0 && buf[len - 1] != '\n') {
|
||||||
while (getchar() != '\n' && !feof(stdin)) ;
|
while (getchar() != '\n' && !feof(stdin));
|
||||||
}
|
} else if (len > 0) {
|
||||||
else if (len > 0) {
|
|
||||||
buf[len - 1] = '\0';
|
buf[len - 1] = '\0';
|
||||||
}
|
}
|
||||||
} while (buf[0] == '\0' && required);
|
} while (buf[0] == '\0' && required);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char** argv) {
|
||||||
const char *err = 0;
|
const char* err = 0;
|
||||||
struct {
|
struct {
|
||||||
noom_bool_t enter_repl;
|
noom_bool_t enter_repl;
|
||||||
noom_bool_t use_stdin;
|
noom_bool_t use_stdin;
|
||||||
@@ -170,39 +168,39 @@ int main(int argc, char **argv) {
|
|||||||
const char* script_path;
|
const char* script_path;
|
||||||
noom_bool_t do_i_already_know_what_to_do;
|
noom_bool_t do_i_already_know_what_to_do;
|
||||||
} params = {0};
|
} params = {0};
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
params.enter_repl = 1;
|
params.enter_repl = 1;
|
||||||
params.do_i_already_know_what_to_do = 1;
|
params.do_i_already_know_what_to_do = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if (noom_strcmp(argv[i], "-") == 0) {
|
if (noom_strcmp(argv[i], "-") == 0) {
|
||||||
params.use_stdin = 1;
|
params.use_stdin = 1;
|
||||||
params.do_i_already_know_what_to_do = 1;
|
params.do_i_already_know_what_to_do = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noom_strcmp(argv[i], "--") == 0) {
|
if (noom_strcmp(argv[i], "--") == 0) {
|
||||||
if (++i >= argc) break;
|
if (++i >= argc) break;
|
||||||
if (params.do_i_already_know_what_to_do) {
|
if (params.do_i_already_know_what_to_do) {
|
||||||
err = "too many arguments";
|
err = "too many arguments";
|
||||||
goto die;
|
goto die;
|
||||||
}
|
}
|
||||||
params.script_exec = argv[i];
|
params.script_exec = argv[i];
|
||||||
params.do_i_already_know_what_to_do = 1;
|
params.do_i_already_know_what_to_do = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv[i][0] != '-') {
|
if (argv[i][0] != '-') {
|
||||||
if (params.do_i_already_know_what_to_do) {
|
if (params.do_i_already_know_what_to_do) {
|
||||||
err = "too many arguments";
|
err = "too many arguments";
|
||||||
goto die;
|
goto die;
|
||||||
}
|
}
|
||||||
params.script_path = argv[i];
|
params.script_path = argv[i];
|
||||||
params.do_i_already_know_what_to_do = 1;
|
params.do_i_already_know_what_to_do = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv[i][1] == 'e') {
|
if (argv[i][1] == 'e') {
|
||||||
if (params.do_i_already_know_what_to_do) {
|
if (params.do_i_already_know_what_to_do) {
|
||||||
@@ -214,7 +212,7 @@ int main(int argc, char **argv) {
|
|||||||
params.do_i_already_know_what_to_do = 1;
|
params.do_i_already_know_what_to_do = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (++i >= argc) {
|
if (++i >= argc) {
|
||||||
err = "-e needs an argument";
|
err = "-e needs an argument";
|
||||||
goto die;
|
goto die;
|
||||||
@@ -222,15 +220,15 @@ int main(int argc, char **argv) {
|
|||||||
params.script_exec = argv[i];
|
params.script_exec = argv[i];
|
||||||
params.do_i_already_know_what_to_do = 1;
|
params.do_i_already_know_what_to_do = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv[i][1] == 'v') {
|
if (argv[i][1] == 'v') {
|
||||||
puts(NOOM_VERSION_TEXT);
|
puts(NOOM_VERSION_TEXT);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
err = "unknown option";
|
err = "unknown option";
|
||||||
goto die;
|
goto die;
|
||||||
}
|
}
|
||||||
if (!params.do_i_already_know_what_to_do) {
|
if (!params.do_i_already_know_what_to_do) {
|
||||||
err = "script not set";
|
err = "script not set";
|
||||||
goto die;
|
goto die;
|
||||||
@@ -242,7 +240,8 @@ int main(int argc, char **argv) {
|
|||||||
char* code = read_file(params.script_path);
|
char* code = read_file(params.script_path);
|
||||||
if (code == 0) return 1;
|
if (code == 0) return 1;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
if (code[0] == '#' && code[1] == '!') for (offset = 2; code[offset] && code[offset] != '\n'; offset++);
|
if (code[0] == '#' && code[1] == '!')
|
||||||
|
for (offset = 2; code[offset] && code[offset] != '\n'; offset++);
|
||||||
int e = the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code + offset, argv[0], params.script_path);
|
int e = the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(code + offset, argv[0], params.script_path);
|
||||||
noom_free(code);
|
noom_free(code);
|
||||||
return e;
|
return e;
|
||||||
@@ -263,12 +262,14 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
die:
|
die:
|
||||||
fprintf(stderr, "%s: %s\n"
|
fprintf(stderr, "%s: %s\n"
|
||||||
"usage: %s [options] [script [args]]\n"
|
"usage: %s [options] [script [args]]\n"
|
||||||
"Available options are:\n"
|
"Available options are:\n"
|
||||||
" - execute stdin\n"
|
" - execute stdin\n"
|
||||||
" -e stat execute string 'stat'\n"
|
" -e stat execute string 'stat'\n"
|
||||||
" -v show version\n",
|
" -v show version\n",
|
||||||
argv[0], err, argv[0]);
|
argv[0],
|
||||||
|
err,
|
||||||
|
argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
86
src/parser.c
86
src/parser.c
@@ -1,7 +1,7 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
|
||||||
const char *noomP_formatNodeType(noomP_NodeType node_type) {
|
const char* noomP_formatNodeType(noomP_NodeType node_type) {
|
||||||
switch (node_type) {
|
switch (node_type) {
|
||||||
case NOOMP_NODE_PROGRAM:
|
case NOOMP_NODE_PROGRAM:
|
||||||
return "program";
|
return "program";
|
||||||
@@ -103,7 +103,7 @@ int noomP_peek(noomP_Parser* parser, noomL_Token* token) {
|
|||||||
parser->error_state = NOOMP_ERROR_LEXER | success;
|
parser->error_state = NOOMP_ERROR_LEXER | success;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token->type == NOOML_TOKEN_WHITESPACE || token->type == NOOML_TOKEN_COMMENT) {
|
if (token->type == NOOML_TOKEN_WHITESPACE || token->type == NOOML_TOKEN_COMMENT) {
|
||||||
// peek changes state, but only if it's one of these useless tokens anyway.
|
// peek changes state, but only if it's one of these useless tokens anyway.
|
||||||
parser->lex_offset += token->length;
|
parser->lex_offset += token->length;
|
||||||
@@ -114,7 +114,7 @@ int noomP_peek(noomP_Parser* parser, noomL_Token* token) {
|
|||||||
parser->last_token_offset = token->offset;
|
parser->last_token_offset = token->offset;
|
||||||
parser->last_token_length = token->length;
|
parser->last_token_length = token->length;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,12 +150,12 @@ 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) {
|
||||||
noomP_Node** new = noom_realloc(node->subnodes, sizeof(noomP_Node*) * node->subnode_cap * 2);
|
noomP_Node** new = noom_realloc(node->subnodes, sizeof(noomP_Node*) * node->subnode_cap * 2);
|
||||||
|
|
||||||
if (new == 0) {
|
if (new == 0) {
|
||||||
parser->error_state = NOOMP_ERROR_OOM; // well fuck
|
parser->error_state = NOOMP_ERROR_OOM; // well fuck
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
node->subnodes = new;
|
node->subnodes = new;
|
||||||
node->subnode_cap = node->subnode_cap * 2;
|
node->subnode_cap = node->subnode_cap * 2;
|
||||||
}
|
}
|
||||||
@@ -282,7 +282,7 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
if (token.type != NOOML_TOKEN_SYMBOL || !noom_memeq(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;
|
||||||
@@ -296,7 +296,7 @@ noomP_Node* noomP_parseTableLiteral(noomP_Parser* parser) {
|
|||||||
noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode) {
|
noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode) {
|
||||||
noomP_Node* node = snode;
|
noomP_Node* node = snode;
|
||||||
noomL_Token token;
|
noomL_Token token;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
|
|
||||||
@@ -326,7 +326,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
|
|||||||
|
|
||||||
if (noomP_addSubnode(parser, new, node)) return 0;
|
if (noomP_addSubnode(parser, new, node)) return 0;
|
||||||
if (noomP_addSubnode(parser, new, fname)) return 0;
|
if (noomP_addSubnode(parser, new, fname)) return 0;
|
||||||
|
|
||||||
node = new;
|
node = new;
|
||||||
} else if (noom_memeq(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 [
|
||||||
@@ -398,7 +398,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
|
|||||||
|
|
||||||
new->type = NOOMP_NODE_TABLECALL;
|
new->type = NOOMP_NODE_TABLECALL;
|
||||||
new->source_offset = token.offset;
|
new->source_offset = token.offset;
|
||||||
|
|
||||||
noomP_Node* table = noomP_parseTableLiteral(parser);
|
noomP_Node* table = noomP_parseTableLiteral(parser);
|
||||||
if (table == 0) return 0;
|
if (table == 0) return 0;
|
||||||
|
|
||||||
@@ -417,7 +417,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
|
|||||||
noomP_Node* new = noomP_allocNode(parser);
|
noomP_Node* new = noomP_allocNode(parser);
|
||||||
if (new == 0) return 0;
|
if (new == 0) return 0;
|
||||||
|
|
||||||
new->type = 0;
|
new->type = 0;
|
||||||
new->type = NOOMP_NODE_METHODCALL;
|
new->type = NOOMP_NODE_METHODCALL;
|
||||||
new->source_offset = sym_loc;
|
new->source_offset = sym_loc;
|
||||||
|
|
||||||
@@ -451,7 +451,7 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
|
|||||||
noomP_skip(parser, &token);
|
noomP_skip(parser, &token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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_memeq(parser->code + token.offset, token.length, ")", 1))) {
|
if (token.type != NOOML_TOKEN_SYMBOL || (!noom_memeq(parser->code + token.offset, token.length, ")", 1))) {
|
||||||
@@ -480,7 +480,6 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
|
|||||||
return 0; // unexpected :(
|
return 0; // unexpected :(
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
node = new;
|
node = new;
|
||||||
} else {
|
} else {
|
||||||
return node; // done
|
return node; // done
|
||||||
@@ -512,7 +511,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
|
|||||||
|
|
||||||
noomP_Node* numNode = noomP_allocNode(parser);
|
noomP_Node* numNode = noomP_allocNode(parser);
|
||||||
if (numNode == 0) return 0;
|
if (numNode == 0) return 0;
|
||||||
|
|
||||||
numNode->type = NOOMP_NODE_NUMBERLITERAL;
|
numNode->type = NOOMP_NODE_NUMBERLITERAL;
|
||||||
numNode->source_offset = token.offset;
|
numNode->source_offset = token.offset;
|
||||||
|
|
||||||
@@ -522,7 +521,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
|
|||||||
|
|
||||||
noomP_Node* numNode = noomP_allocNode(parser);
|
noomP_Node* numNode = noomP_allocNode(parser);
|
||||||
if (numNode == 0) return 0;
|
if (numNode == 0) return 0;
|
||||||
|
|
||||||
numNode->type = NOOMP_NODE_STRINGLITERAL;
|
numNode->type = NOOMP_NODE_STRINGLITERAL;
|
||||||
numNode->source_offset = token.offset;
|
numNode->source_offset = token.offset;
|
||||||
|
|
||||||
@@ -543,7 +542,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
|
|||||||
} else if (token.type == NOOML_TOKEN_KEYWORD) {
|
} else if (token.type == NOOML_TOKEN_KEYWORD) {
|
||||||
if (noom_memeq(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);
|
||||||
if (litNode == 0) return 0;
|
if (litNode == 0) return 0;
|
||||||
|
|
||||||
@@ -553,7 +552,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
|
|||||||
return litNode;
|
return litNode;
|
||||||
} else if (noom_memeq(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);
|
||||||
if (litNode == 0) return 0;
|
if (litNode == 0) return 0;
|
||||||
|
|
||||||
@@ -563,7 +562,7 @@ noomP_Node* noomP_parseRawExpression(noomP_Parser* parser) {
|
|||||||
return litNode;
|
return litNode;
|
||||||
} else if (noom_memeq(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);
|
||||||
if (litNode == 0) return 0;
|
if (litNode == 0) return 0;
|
||||||
|
|
||||||
@@ -705,8 +704,7 @@ int noomP_infixOperatorBP(noomP_Parser* parser, noomL_Token* token, noom_uint_t*
|
|||||||
*b = 63;
|
*b = 63;
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
// oh boy.
|
||||||
// oh boy.
|
|
||||||
} else if (noom_memeq(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;
|
||||||
@@ -785,7 +783,7 @@ noomP_Node* noomP_parseOperatorExpression(noomP_Parser* parser, noom_uint_t min_
|
|||||||
|
|
||||||
lhs = noomP_allocNode(parser);
|
lhs = noomP_allocNode(parser);
|
||||||
if (lhs == 0) return 0;
|
if (lhs == 0) return 0;
|
||||||
|
|
||||||
lhs->type = NOOMP_NODE_UNARYOPERATOR;
|
lhs->type = NOOMP_NODE_UNARYOPERATOR;
|
||||||
lhs->source_offset = token.offset; // the operator! we need this to check what it was when compiling.
|
lhs->source_offset = token.offset; // the operator! we need this to check what it was when compiling.
|
||||||
|
|
||||||
@@ -908,12 +906,12 @@ noomP_Node* noomP_parseBlock(noomP_Parser* parser) { // stops on end, else or el
|
|||||||
// block starter has been eaten already; we just go until ending keyword
|
// block starter has been eaten already; we just go until ending keyword
|
||||||
noomP_Node* node = noomP_allocNode(parser);
|
noomP_Node* node = noomP_allocNode(parser);
|
||||||
if (node == 0) return 0; // OOM :(
|
if (node == 0) return 0; // OOM :(
|
||||||
|
|
||||||
node->type = NOOMP_NODE_BLOCK;
|
node->type = NOOMP_NODE_BLOCK;
|
||||||
node->source_offset = parser->lex_offset;
|
node->source_offset = parser->lex_offset;
|
||||||
|
|
||||||
noomL_Token token;
|
noomL_Token token;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// check if end reached
|
// check if end reached
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
@@ -929,7 +927,7 @@ noomP_Node* noomP_parseBlock(noomP_Parser* parser) { // stops on end, else or el
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
noomP_Node* stmt = noomP_parseStatement(parser);
|
noomP_Node* stmt = noomP_parseStatement(parser);
|
||||||
if (stmt == 0) return 0;
|
if (stmt == 0) return 0;
|
||||||
|
|
||||||
@@ -951,20 +949,20 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
if (token.type == NOOML_TOKEN_KEYWORD && noom_memeq(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);
|
||||||
if (funcNode == 0) return 0;
|
if (funcNode == 0) return 0;
|
||||||
|
|
||||||
funcNode->type = NOOMP_NODE_LOCALFUNCTIONDECLARATION;
|
funcNode->type = NOOMP_NODE_LOCALFUNCTIONDECLARATION;
|
||||||
funcNode->source_offset = token.offset;
|
funcNode->source_offset = token.offset;
|
||||||
|
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
if (token.type != NOOML_TOKEN_IDENTIFIER) {
|
if (token.type != NOOML_TOKEN_IDENTIFIER) {
|
||||||
parser->error_state = NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LOCAL_FUNCTION;
|
parser->error_state = NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_LOCAL_FUNCTION;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
noomP_skip(parser, &token);
|
noomP_skip(parser, &token);
|
||||||
|
|
||||||
noomP_Node* nameNode = noomP_allocNode(parser);
|
noomP_Node* nameNode = noomP_allocNode(parser);
|
||||||
if (nameNode == 0) return 0;
|
if (nameNode == 0) return 0;
|
||||||
|
|
||||||
@@ -1008,7 +1006,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
noomP_Node* varname = noomP_allocNode(parser);
|
noomP_Node* varname = noomP_allocNode(parser);
|
||||||
if (varname == 0) return 0;
|
if (varname == 0) return 0;
|
||||||
|
|
||||||
varname->type = NOOMP_NODE_VARNAME;
|
varname->type = NOOMP_NODE_VARNAME;
|
||||||
varname->source_offset = token.offset;
|
varname->source_offset = token.offset;
|
||||||
|
|
||||||
@@ -1049,7 +1047,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
if (noomP_addSubnode(parser, varname, attrn)) return 0;
|
if (noomP_addSubnode(parser, varname, attrn)) return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (noomP_addSubnode(parser, localNode, varname)) return 0;
|
if (noomP_addSubnode(parser, localNode, varname)) return 0;
|
||||||
|
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
@@ -1075,7 +1073,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
// equals has already been eaten by loop (thank you loop)
|
// equals has already been eaten by loop (thank you loop)
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
if (noomP_addSubnode(parser, localNode, expr)) return 0;
|
if (noomP_addSubnode(parser, localNode, expr)) return 0;
|
||||||
@@ -1161,7 +1159,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
// we know it's an else if it's an odd number. no need to do anything special.
|
// we know it's an else if it's an odd number. no need to do anything special.
|
||||||
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_memeq(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
|
||||||
@@ -1222,7 +1220,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
// sounds like a thing for the compiler to bytecode though
|
// sounds like a thing for the compiler to bytecode though
|
||||||
noomP_Node* node = noomP_allocNode(parser);
|
noomP_Node* node = noomP_allocNode(parser);
|
||||||
if (node == 0) return 0;
|
if (node == 0) return 0;
|
||||||
|
|
||||||
node->type = NOOMP_NODE_BREAK;
|
node->type = NOOMP_NODE_BREAK;
|
||||||
node->source_offset = token.offset;
|
node->source_offset = token.offset;
|
||||||
|
|
||||||
@@ -1246,7 +1244,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
fname->type = NOOMP_NODE_FUNCTIONNAME;
|
fname->type = NOOMP_NODE_FUNCTIONNAME;
|
||||||
fname->source_offset = token.offset;
|
fname->source_offset = token.offset;
|
||||||
|
|
||||||
if (token.type != NOOML_TOKEN_IDENTIFIER) {
|
if (token.type != NOOML_TOKEN_IDENTIFIER) {
|
||||||
parser->error_state = NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_FUNCTION;
|
parser->error_state = NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_FUNCTION;
|
||||||
return 0; // unex.
|
return 0; // unex.
|
||||||
}
|
}
|
||||||
@@ -1259,7 +1257,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
base->source_offset = token.offset;
|
base->source_offset = token.offset;
|
||||||
|
|
||||||
if (noomP_addSubnode(parser, fname, base)) return 0;
|
if (noomP_addSubnode(parser, fname, base)) return 0;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
|
|
||||||
@@ -1409,7 +1407,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
if (!is_done) {
|
if (!is_done) {
|
||||||
parser->error_state = NOOMP_ERROR_RETURN_NOT_END;
|
parser->error_state = NOOMP_ERROR_RETURN_NOT_END;
|
||||||
return 0; //darn it
|
return 0; // darn it
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -1468,7 +1466,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_memeq(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);
|
||||||
|
|
||||||
@@ -1542,7 +1540,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
noomP_skip(parser, &token);
|
noomP_skip(parser, &token);
|
||||||
|
|
||||||
return forl;
|
return forl;
|
||||||
} else if (noom_memeq(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);
|
||||||
@@ -1552,7 +1550,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
thing->type = NOOMP_NODE_GOTO;
|
thing->type = NOOMP_NODE_GOTO;
|
||||||
thing->source_offset = token.offset;
|
thing->source_offset = token.offset;
|
||||||
|
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
if (token.type != NOOML_TOKEN_IDENTIFIER) {
|
if (token.type != NOOML_TOKEN_IDENTIFIER) {
|
||||||
parser->error_state = NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_GOTO;
|
parser->error_state = NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_GOTO;
|
||||||
@@ -1629,7 +1627,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
thing->type = NOOMP_NODE_LABEL;
|
thing->type = NOOMP_NODE_LABEL;
|
||||||
thing->source_offset = token.offset;
|
thing->source_offset = token.offset;
|
||||||
|
|
||||||
if (noomP_peek(parser, &token)) return 0;
|
if (noomP_peek(parser, &token)) return 0;
|
||||||
if (token.type != NOOML_TOKEN_IDENTIFIER) return 0;
|
if (token.type != NOOML_TOKEN_IDENTIFIER) return 0;
|
||||||
noomP_skip(parser, &token);
|
noomP_skip(parser, &token);
|
||||||
@@ -1671,7 +1669,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
initial_place->type = NOOMP_NODE_ASSIGNPLACE;
|
initial_place->type = NOOMP_NODE_ASSIGNPLACE;
|
||||||
initial_place->source_offset = base->source_offset;
|
initial_place->source_offset = base->source_offset;
|
||||||
|
|
||||||
if (noomP_addSubnode(parser, initial_place, base)) return 0;
|
if (noomP_addSubnode(parser, initial_place, base)) return 0;
|
||||||
|
|
||||||
if (noomP_addSubnode(parser, assignment, initial_place)) return 0;
|
if (noomP_addSubnode(parser, assignment, initial_place)) return 0;
|
||||||
@@ -1682,15 +1680,15 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
if (token.type == NOOML_TOKEN_SYMBOL) {
|
if (token.type == NOOML_TOKEN_SYMBOL) {
|
||||||
if (noom_memeq(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_memeq(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// more thingers
|
// more thingers
|
||||||
noomP_Node* item = noomP_parseRawExpression(parser);
|
noomP_Node* item = noomP_parseRawExpression(parser);
|
||||||
if (item == 0) return 0;
|
if (item == 0) return 0;
|
||||||
@@ -1755,7 +1753,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
|||||||
|
|
||||||
noomP_Node* noomP_parseStatement(noomP_Parser* parser) {
|
noomP_Node* noomP_parseStatement(noomP_Parser* parser) {
|
||||||
noomL_Token token;
|
noomL_Token token;
|
||||||
|
|
||||||
noomP_Node* stmt = noomP_parseRawStatement(parser);
|
noomP_Node* stmt = noomP_parseRawStatement(parser);
|
||||||
if (stmt == 0) return 0;
|
if (stmt == 0) return 0;
|
||||||
|
|
||||||
@@ -1803,7 +1801,7 @@ int noomP_initParser(noomP_Parser* parser, const char* code, const char* filenam
|
|||||||
parser->filename = filename;
|
parser->filename = filename;
|
||||||
parser->lex_offset = 0;
|
parser->lex_offset = 0;
|
||||||
parser->last_token_length = 0;
|
parser->last_token_length = 0;
|
||||||
parser->last_node = (void *)0;
|
parser->last_node = (void*)0;
|
||||||
parser->version = version;
|
parser->version = version;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
34
src/vm.c
34
src/vm.c
@@ -1,9 +1,9 @@
|
|||||||
#include "vm.h"
|
#include "vm.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
|
||||||
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_Object *o = noom_alloc(size);
|
noomV_Object* o = noom_alloc(size);
|
||||||
if(o == 0) return o;
|
if (o == 0) return o;
|
||||||
|
|
||||||
o->tag = tag;
|
o->tag = tag;
|
||||||
o->marked = 0;
|
o->marked = 0;
|
||||||
@@ -13,16 +13,16 @@ 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* 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);
|
noomV_String* s = (noomV_String*)noomV_allocObj(vm, NOOMV_OSTR, sizeof(noomV_String) + len + 1);
|
||||||
if (s == 0) return 0;
|
if (s == 0) return 0;
|
||||||
noom_memcpy(s->data, str, len);
|
noom_memcpy(s->data, str, len);
|
||||||
s->data[len] = '\0';
|
s->data[len] = '\0';
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
noomV_Function *noomV_allocFunc(noom_LuaVM *vm, noomV_String *chunkname) {
|
noomV_Function* noomV_allocFunc(noom_LuaVM* vm, noomV_String* chunkname) {
|
||||||
noomV_Function *f = (noomV_Function*)noomV_allocObj(vm, NOOMV_OFUNC, sizeof(noomV_Function));
|
noomV_Function* f = (noomV_Function*)noomV_allocObj(vm, NOOMV_OFUNC, sizeof(noomV_Function));
|
||||||
if (f == 0) return 0;
|
if (f == 0) return 0;
|
||||||
f->chunkname = chunkname;
|
f->chunkname = chunkname;
|
||||||
f->env = 0;
|
f->env = 0;
|
||||||
@@ -43,8 +43,8 @@ noomV_Function *noomV_allocFunc(noom_LuaVM *vm, noomV_String *chunkname) {
|
|||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
noomV_Table *noomV_allocTable(noom_LuaVM *vm) {
|
noomV_Table* noomV_allocTable(noom_LuaVM* vm) {
|
||||||
noomV_Table *t = (noomV_Table *)noomV_allocObj(vm, NOOMV_OTABLE, sizeof(noomV_Table));
|
noomV_Table* t = (noomV_Table*)noomV_allocObj(vm, NOOMV_OTABLE, sizeof(noomV_Table));
|
||||||
if (t == 0) return 0;
|
if (t == 0) return 0;
|
||||||
t->meta = 0;
|
t->meta = 0;
|
||||||
t->entries = 0;
|
t->entries = 0;
|
||||||
@@ -54,13 +54,13 @@ noomV_Table *noomV_allocTable(noom_LuaVM *vm) {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
void noomV_freeObj(noomV_Object *obj) {
|
void noomV_freeObj(noomV_Object* obj) {
|
||||||
noom_free(obj);
|
noom_free(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
noom_LuaVM *noom_createVM(noom_LuaVersion version) {
|
noom_LuaVM* noom_createVM(noom_LuaVersion version) {
|
||||||
noom_LuaVM *vm = noom_alloc(sizeof(*vm));
|
noom_LuaVM* vm = noom_alloc(sizeof(*vm));
|
||||||
if(vm == 0) return 0;
|
if (vm == 0) return 0;
|
||||||
// initialize the universe to NULL, handles partial OOMs nicely
|
// initialize the universe to NULL, handles partial OOMs nicely
|
||||||
vm->heap = 0;
|
vm->heap = 0;
|
||||||
vm->graySet = 0;
|
vm->graySet = 0;
|
||||||
@@ -72,10 +72,10 @@ noom_LuaVM *noom_createVM(noom_LuaVersion version) {
|
|||||||
return vm;
|
return vm;
|
||||||
}
|
}
|
||||||
|
|
||||||
void noom_destroyVM(noom_LuaVM *vm) {
|
void noom_destroyVM(noom_LuaVM* vm) {
|
||||||
noomV_Object *iter = vm->heap;
|
noomV_Object* iter = vm->heap;
|
||||||
while(iter) {
|
while (iter) {
|
||||||
noomV_Object *cur = iter;
|
noomV_Object* cur = iter;
|
||||||
iter = iter->next;
|
iter = iter->next;
|
||||||
noomV_freeObj(cur);
|
noomV_freeObj(cur);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user