formatting and shi
This commit is contained in:
@@ -37,3 +37,5 @@ IndentGotoLabels: true
|
||||
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
|
||||
InsertTrailingCommas: Wrapped
|
||||
|
||||
148
src/compiler.c
148
src/compiler.c
@@ -30,29 +30,31 @@
|
||||
// ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡘⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
|
||||
|
||||
typedef struct noomC_LocalInfo {
|
||||
enum { NOOMC_GLOBAL, NOOMC_LOCAL, NOOMC_UPVAL } type;
|
||||
enum { NOOMC_GLOBAL,
|
||||
NOOMC_LOCAL,
|
||||
NOOMC_UPVAL } type;
|
||||
unsigned int idx;
|
||||
} noomC_LocalInfo;
|
||||
|
||||
noom_Exit noomC_addLocal(noomC_Compiler *c, noomC_Local local) {
|
||||
if(c->localc == NOOMC_MAXLOCAL) return NOOM_EINTERNAL;
|
||||
noom_Exit noomC_addLocal(noomC_Compiler* c, noomC_Local local) {
|
||||
if (c->localc == NOOMC_MAXLOCAL) return NOOM_EINTERNAL;
|
||||
c->locals[c->localc++] = local;
|
||||
return NOOM_OK;
|
||||
}
|
||||
|
||||
noom_Exit noomC_addUpval(noomC_Compiler *c, noomC_Upval upval) {
|
||||
if(c->upvalc == NOOMC_MAXUPVAL) return NOOM_EINTERNAL;
|
||||
noom_Exit noomC_addUpval(noomC_Compiler* c, noomC_Upval upval) {
|
||||
if (c->upvalc == NOOMC_MAXUPVAL) return NOOM_EINTERNAL;
|
||||
c->upvals[c->upvalc++] = upval;
|
||||
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
|
||||
|
||||
for(int i = compiler->localc-1; i >= 0; i--) {
|
||||
for (int i = compiler->localc - 1; i >= 0; i--) {
|
||||
noomC_Local l = compiler->locals[i];
|
||||
if(l.dropped) continue;
|
||||
if(noom_memeq(l.name, l.namelen, name, namelen)) {
|
||||
if (l.dropped) continue;
|
||||
if (noom_memeq(l.name, l.namelen, name, namelen)) {
|
||||
info->type = NOOMC_LOCAL;
|
||||
info->idx = l.stackslot;
|
||||
return NOOM_OK;
|
||||
@@ -64,7 +66,7 @@ noom_Exit noomC_identifyLocal(noomC_Compiler *compiler, noomC_LocalInfo *info, c
|
||||
return NOOM_OK;
|
||||
}
|
||||
|
||||
void noomC_compiler_init(noomC_Compiler *compiler) {
|
||||
void noomC_compiler_init(noomC_Compiler* compiler) {
|
||||
compiler->parent = 0;
|
||||
compiler->target = 0;
|
||||
|
||||
@@ -73,62 +75,62 @@ void noomC_compiler_init(noomC_Compiler *compiler) {
|
||||
compiler->curstack = 0;
|
||||
}
|
||||
|
||||
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));
|
||||
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));
|
||||
if (newCode == 0) return NOOM_ENOMEM;
|
||||
func->code = newCode;
|
||||
func->code[func->codesize++] = inst;
|
||||
return NOOM_OK;
|
||||
}
|
||||
|
||||
static noom_Exit noomC_emit_ABC(noomV_Function *func, const noomV_Opcode op, const unsigned char a, const unsigned char b, const unsigned char c) {
|
||||
static noom_Exit noomC_emit_ABC(noomV_Function* func, const noomV_Opcode op, const unsigned char a, const unsigned char b, const unsigned char c) {
|
||||
return noomC_emit(func, (noomV_Inst){.op = op, .a = a, .b = b, .c = c});
|
||||
}
|
||||
|
||||
static noom_Exit noomC_emit_Aus(noomV_Function *func, const noomV_Opcode op, const unsigned char a, const unsigned short us) {
|
||||
static noom_Exit noomC_emit_Aus(noomV_Function* func, const noomV_Opcode op, const unsigned char a, const unsigned short us) {
|
||||
return noomC_emit(func, (noomV_Inst){.op = op, .a = a, .us = us});
|
||||
}
|
||||
|
||||
static noom_Exit noomC_addconst(noomV_Function *func, const noomV_Value val) {
|
||||
static noom_Exit noomC_addconst(noomV_Function* func, const noomV_Value val) {
|
||||
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;
|
||||
func->consts = newConsts;
|
||||
func->consts[func->constsize++] = val;
|
||||
return NOOM_OK;
|
||||
}
|
||||
|
||||
static noomV_String *noomC_internString(noomC_Compiler *c, noom_LuaVM *vm, const char *str, noom_uint_t len) {
|
||||
while(c) {
|
||||
noomV_Function *f = c->target;
|
||||
for(int i = 0; i < f->constsize; i++) {
|
||||
static noomV_String* noomC_internString(noomC_Compiler* c, noom_LuaVM* vm, const char* str, noom_uint_t len) {
|
||||
while (c) {
|
||||
noomV_Function* f = c->target;
|
||||
for (int i = 0; i < f->constsize; i++) {
|
||||
noomV_Value v = f->consts[i];
|
||||
if(v.tag != NOOMV_VOBJ) continue;
|
||||
noomV_Object *o = v.obj;
|
||||
if(o->tag != NOOMV_OSTR) continue;
|
||||
noomV_String *s = (noomV_String *)o;
|
||||
if(noom_memeq(s->data, s->len, str, len)) return s;
|
||||
if (v.tag != NOOMV_VOBJ) continue;
|
||||
noomV_Object* o = v.obj;
|
||||
if (o->tag != NOOMV_OSTR) continue;
|
||||
noomV_String* s = (noomV_String*)o;
|
||||
if (noom_memeq(s->data, s->len, str, len)) return s;
|
||||
}
|
||||
c = c->parent;
|
||||
}
|
||||
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) {
|
||||
noomV_String *s = noomC_internString(c, vm, str, 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);
|
||||
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_lex(parser->code, offset, &token, parser->version);
|
||||
return token;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
const char *op = parser->code + offset;
|
||||
static noom_BinOp noomC_what_bop_is_this(const noomP_Parser* parser, noom_uint_t offset) {
|
||||
const char* op = parser->code + offset;
|
||||
// I AM THE LEXER NOW!!!!!!!!!
|
||||
if (noom_startswith(op, "==")) return NOOM_BIN_EQL;
|
||||
if (noom_startswith(op, "~=")) return NOOM_BIN_NEQL;
|
||||
@@ -161,12 +163,11 @@ static noom_BinOp noomC_what_bop_is_this(const noomP_Parser *parser, noom_uint_t
|
||||
// mmap ahh function 🥀🥀🥀🥀🥀🥀🥀
|
||||
// What 6 argument syscalls there even are other than mmap and clone and is there anything longer
|
||||
static noom_Exit noomC_compile_expr(
|
||||
noom_LuaVM *vm,
|
||||
noomC_Compiler *compiler,
|
||||
const noomP_Parser *parser,
|
||||
noomV_Function *func,
|
||||
const noomP_Node *node
|
||||
) {
|
||||
noom_LuaVM* vm,
|
||||
noomC_Compiler* compiler,
|
||||
const noomP_Parser* parser,
|
||||
noomV_Function* func,
|
||||
const noomP_Node* node) {
|
||||
noom_Exit result;
|
||||
// Baba is You OST is a very cool soundtrack to code to Can recommend
|
||||
if (node->type == NOOMP_NODE_NILLITERAL) {
|
||||
@@ -193,34 +194,34 @@ static noom_Exit noomC_compile_expr(
|
||||
}
|
||||
if (node->type == NOOMP_NODE_BINARYOPERATOR) {
|
||||
if (node->subnodec != 2) return NOOM_EINTERNAL;
|
||||
const char *op = parser->code + node->source_offset;
|
||||
if(noom_startswith(op, "..")) {
|
||||
const char* op = parser->code + node->source_offset;
|
||||
if (noom_startswith(op, "..")) {
|
||||
// concat is special
|
||||
noom_uint_t amount = 1;
|
||||
const noomP_Node *n = node->subnodes[1];
|
||||
if((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]))) return result;
|
||||
const noomP_Node* n = node->subnodes[1];
|
||||
if ((result = noomC_compile_expr(vm, compiler, parser, func, node->subnodes[0]))) return result;
|
||||
// we have no flattening so we unwind it ourselves
|
||||
// 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).
|
||||
// VM will handle the concat order within the array for us
|
||||
while(1) {
|
||||
if(amount > NOOM_USHORT_MAX) return NOOM_EINTERNAL;
|
||||
while (1) {
|
||||
if (amount > NOOM_USHORT_MAX) return NOOM_EINTERNAL;
|
||||
amount++;
|
||||
if(noom_startswith(parser->code + n->source_offset, "..")) {
|
||||
if((result = noomC_compile_expr(vm, compiler, parser, func, n->subnodes[0]))) return result;
|
||||
if (noom_startswith(parser->code + n->source_offset, "..")) {
|
||||
if ((result = noomC_compile_expr(vm, compiler, parser, func, n->subnodes[0]))) return result;
|
||||
n = n->subnodes[1];
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
compiler->curstack -= amount;
|
||||
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[1]))) 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;
|
||||
// this consumes 2 operands and pushes 1 value, thus having a net stack effect of removing 1 item
|
||||
compiler->curstack--;
|
||||
return noomC_emit_ABC(func, NOOMV_INSTR_OP, 1, noomC_what_bop_is_this(parser, node->source_offset), 0);
|
||||
@@ -244,14 +245,14 @@ static noom_Exit noomC_compile_expr(
|
||||
}
|
||||
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;
|
||||
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);
|
||||
if((result = noomC_identifyLocal(compiler, &info, varname, namelen))) return result;
|
||||
if ((result = noomC_identifyLocal(compiler, &info, varname, namelen))) return result;
|
||||
compiler->curstack++;
|
||||
|
||||
switch(info.type) {
|
||||
switch (info.type) {
|
||||
case NOOMC_LOCAL:
|
||||
return noomC_emit_Aus(func, NOOMV_INSTR_PUSHVAL, 0, info.idx);
|
||||
case NOOMC_UPVAL:
|
||||
@@ -266,9 +267,9 @@ static noom_Exit noomC_compile_expr(
|
||||
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;
|
||||
for (noom_uint_t i = 0; i < node->subnodec; 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
|
||||
|
||||
// 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;
|
||||
// local name <attr> = expression i think
|
||||
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
|
||||
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;
|
||||
|
||||
const char *namebuf = parser->code + varname_node->source_offset;
|
||||
const char* namebuf = parser->code + varname_node->source_offset;
|
||||
|
||||
noomC_Local local;
|
||||
local.startpc = func->codesize;
|
||||
@@ -308,17 +309,15 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
||||
if (varname_node->subnodes[i]->type != NOOMP_NODE_ATTRIBUTE) return NOOM_EINTERNAL;
|
||||
|
||||
noomL_Token attr_token = noomC_token_at(parser, varname_node->subnodes[i]->source_offset);
|
||||
const char *ap = parser->code + attr_token.offset;
|
||||
const char* ap = parser->code + attr_token.offset;
|
||||
noom_uint_t al = attr_token.length;
|
||||
if (noom_memeq(ap, al, "close", 5)) {
|
||||
if (local.close) return NOOM_EINTERNAL;
|
||||
local.close = 1;
|
||||
}
|
||||
else if (noom_memeq(ap, al, "const", 5)) {
|
||||
} else if (noom_memeq(ap, al, "const", 5)) {
|
||||
if (local.constant) return NOOM_EINTERNAL;
|
||||
local.constant = 1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
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);
|
||||
if (result != NOOM_OK) return result;
|
||||
compiler->curstack++;
|
||||
}
|
||||
else {
|
||||
const noomP_Node *value_node = node->subnodes[1];
|
||||
} else {
|
||||
const noomP_Node* value_node = node->subnodes[1];
|
||||
const noom_Exit r = noomC_compile_expr(vm, compiler, parser, func, value_node);
|
||||
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->subnodec != 3) return NOOM_EINTERNAL;
|
||||
// Funny staircase huh
|
||||
const noomP_Node *varname_node = node->subnodes[0];
|
||||
const noomP_Node *params_node = node->subnodes[1];
|
||||
const noomP_Node *block_node = node->subnodes[2];
|
||||
const noomP_Node* varname_node = node->subnodes[0];
|
||||
const noomP_Node* params_node = node->subnodes[1];
|
||||
const noomP_Node* block_node = node->subnodes[2];
|
||||
if (varname_node->type != NOOMP_NODE_VARNAME) return NOOM_EINTERNAL;
|
||||
if (params_node->type != NOOMP_NODE_FUNCTIONPARAMETERS) return NOOM_EINTERNAL;
|
||||
if (block_node->type != NOOMP_NODE_BLOCK) return NOOM_EINTERNAL;
|
||||
|
||||
const noomL_Token name_token = noomC_token_at(parser, varname_node->source_offset);
|
||||
noomV_Function *proto = noomV_allocFunc(vm, func->chunkname);
|
||||
noomV_Function* proto = noomV_allocFunc(vm, func->chunkname);
|
||||
if (proto == 0) return NOOM_ENOMEM;
|
||||
|
||||
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);
|
||||
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;
|
||||
func->protos = newProtos;
|
||||
func->protos[func->protosize++] = proto;
|
||||
@@ -398,17 +396,17 @@ static noom_Exit noomC_add_stuff_to_function(noom_LuaVM *vm, noomC_Compiler *com
|
||||
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;
|
||||
|
||||
noomV_Function* program = noomV_allocFunc(vm, chunkname);
|
||||
if(program == 0) return NOOM_ENOMEM;
|
||||
if (program == 0) return NOOM_ENOMEM;
|
||||
|
||||
noomC_Compiler compiler;
|
||||
noomC_compiler_init(&compiler);
|
||||
compiler.target = program;
|
||||
|
||||
if(parser->version == NOOM_VERSION_51) {
|
||||
if (parser->version == NOOM_VERSION_51) {
|
||||
program->env = env;
|
||||
} else {
|
||||
noomC_Upval upval;
|
||||
@@ -423,7 +421,7 @@ noom_Exit noomC_compile(noom_LuaVM *vm, const noomP_Parser *parser, const noomP_
|
||||
noom_Exit status = noomC_compile_block(vm, &compiler, parser, program, node);
|
||||
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).
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
||||
[NOOMP_ERROR_EXPRESSION_NOT_STATEMENT] = {"loose expression is not a valid statement", 1},
|
||||
[NOOMP_ERROR_FAKEATTRIBUTE] = {"invalid attribute", 2},
|
||||
[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[] = {
|
||||
@@ -84,8 +84,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
||||
if (parser->code[i] == '\n') {
|
||||
row++;
|
||||
column = 1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
column++;
|
||||
}
|
||||
}
|
||||
@@ -106,7 +105,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, const char* program_na
|
||||
linedig +
|
||||
sizeof(": ") - 1 +
|
||||
noom_strlen(err.s) +
|
||||
+ 1; // \0;
|
||||
+1; // \0;
|
||||
|
||||
if (err.near) space +=
|
||||
(err.near == 1 ? sizeof(" near") - 1 : 0) +
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
noom_uint_t noom_strlen(const char *s) {
|
||||
noom_uint_t noom_strlen(const char* s) {
|
||||
#ifdef __has_builtin
|
||||
#if __has_builtin(__builtin_strlen)
|
||||
return __builtin_strlen(s);
|
||||
#endif
|
||||
#endif
|
||||
const char *a = s;
|
||||
const char* a = s;
|
||||
while (*s) s++;
|
||||
return s - a;
|
||||
}
|
||||
|
||||
int noom_strcmp(const char *a, const char *b) {
|
||||
int noom_strcmp(const char* a, const char* b) {
|
||||
#ifdef __has_builtin
|
||||
#if __has_builtin(__builtin_strcmp)
|
||||
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 *whar = noom_alloc(len + 1);
|
||||
char* noom_strndup(const char* s, const noom_uint_t len) {
|
||||
char* whar = noom_alloc(len + 1);
|
||||
if (whar == 0) return 0;
|
||||
noom_memcpy(whar, s, len);
|
||||
whar[len] = '\0';
|
||||
@@ -69,8 +69,8 @@ void noom_memcpy(void* dst, const void* src, noom_uint_t n) {
|
||||
return;
|
||||
#endif
|
||||
#endif
|
||||
unsigned char *d = dst;
|
||||
const unsigned char *s = src;
|
||||
unsigned char* d = dst;
|
||||
const unsigned char* s = src;
|
||||
|
||||
while (n--) {
|
||||
*d++ = *s++;
|
||||
|
||||
42
src/lexer.c
42
src/lexer.c
@@ -123,7 +123,6 @@ noom_uint_t noomL_getnumber(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (len == 2) { // nothing after the x; malformed number.
|
||||
@@ -202,7 +201,10 @@ noom_uint_t noomL_getcomment(const char* str, noomL_ErrorType* error) {
|
||||
if (str[len] == '[') {
|
||||
len++;
|
||||
|
||||
while (str[len] == '=') { longb_len++; len++; }
|
||||
while (str[len] == '=') {
|
||||
longb_len++;
|
||||
len++;
|
||||
}
|
||||
|
||||
if (str[len] == '[') {
|
||||
// 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 testlong = 0;
|
||||
|
||||
while (str[len] == '=') { testlong++; len++; }
|
||||
while (str[len] == '=') {
|
||||
testlong++;
|
||||
len++;
|
||||
}
|
||||
|
||||
if (str[len] == ']') { // actual long bracket! holy shit!
|
||||
len++;
|
||||
@@ -297,17 +302,18 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
||||
len++;
|
||||
} else if (s[len] == '\n') {
|
||||
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;
|
||||
|
||||
} else if (noomL_isnumber(s[len])) {
|
||||
// fuckkkk
|
||||
noom_uint_t count = 0;
|
||||
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 ((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
|
||||
*error = NOOML_ERROR_DECIMAL_ESCAPE_TOO_BIG;
|
||||
return 0;
|
||||
@@ -319,7 +325,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
||||
} else if (s[len] == 'x' && version >= NOOM_VERSION_52) {
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
@@ -347,7 +353,7 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
||||
// fuck my life
|
||||
if (version == NOOM_VERSION_53) {
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
@@ -397,9 +403,15 @@ noom_uint_t noomL_getstring(const char* s, noomL_ErrorType* error, noom_LuaVersi
|
||||
noom_uint_t order = 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.
|
||||
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 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
|
||||
len++;
|
||||
@@ -466,7 +481,7 @@ int noomL_iskeyword(const char* s, noom_uint_t len, noom_LuaVersion version) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *noomL_formatTokenType(noomL_TokenType token_type) {
|
||||
const char* noomL_formatTokenType(noomL_TokenType token_type) {
|
||||
switch (token_type) {
|
||||
case NOOML_TOKEN_EOF:
|
||||
return "EOF";
|
||||
@@ -487,7 +502,6 @@ const char *noomL_formatTokenType(noomL_TokenType token_type) {
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token, noom_LuaVersion version) {
|
||||
@@ -584,7 +598,7 @@ noomL_ErrorType noomL_lex(const char* s, noom_uint_t start, noomL_Token* token,
|
||||
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_lex(s, start, &t, version);
|
||||
return t.length;
|
||||
|
||||
29
src/main.c
29
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);
|
||||
printf("{\n");
|
||||
|
||||
@@ -34,9 +34,9 @@ void print_node(const noomP_Node *node, noom_uint_t depth) {
|
||||
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_Node *program;
|
||||
noomP_Node* program;
|
||||
|
||||
// goodbye "shitass" you will be missed
|
||||
int success = noomP_parse(code, filename, NOOM_VERSION_54, &program, &parser);
|
||||
@@ -82,8 +82,7 @@ int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(
|
||||
puts("\x1b[0m");
|
||||
puts("PARSE OUTPUT:");
|
||||
print_node(program, 0);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
noom_uint_t bleh = noom_format_error(&parser, program_name, NULL, 0);
|
||||
char* buf = noom_alloc(bleh);
|
||||
noom_format_error(&parser, program_name, buf, bleh);
|
||||
@@ -92,9 +91,9 @@ int the_theoretical_function_to_execute_your_code_that_should_be_replaced_later(
|
||||
}
|
||||
|
||||
// freeing time
|
||||
noomP_Node *last_node = parser.last_node;
|
||||
noomP_Node* last_node = parser.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
|
||||
if (last_node->subnodes) noom_free(last_node->subnodes);
|
||||
noom_free(last_node);
|
||||
@@ -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;
|
||||
const size_t len = noom_strlen(buf);
|
||||
if (len > 0 && buf[len - 1] != '\n') {
|
||||
while (getchar() != '\n' && !feof(stdin)) ;
|
||||
}
|
||||
else if (len > 0) {
|
||||
while (getchar() != '\n' && !feof(stdin));
|
||||
} else if (len > 0) {
|
||||
buf[len - 1] = '\0';
|
||||
}
|
||||
} while (buf[0] == '\0' && required);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
const char *err = 0;
|
||||
int main(int argc, char** argv) {
|
||||
const char* err = 0;
|
||||
struct {
|
||||
noom_bool_t enter_repl;
|
||||
noom_bool_t use_stdin;
|
||||
@@ -242,7 +240,8 @@ int main(int argc, char **argv) {
|
||||
char* code = read_file(params.script_path);
|
||||
if (code == 0) return 1;
|
||||
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);
|
||||
noom_free(code);
|
||||
return e;
|
||||
@@ -269,6 +268,8 @@ die:
|
||||
" - execute stdin\n"
|
||||
" -e stat execute string 'stat'\n"
|
||||
" -v show version\n",
|
||||
argv[0], err, argv[0]);
|
||||
argv[0],
|
||||
err,
|
||||
argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
10
src/parser.c
10
src/parser.c
@@ -1,7 +1,7 @@
|
||||
#include "parser.h"
|
||||
#include "helper.h"
|
||||
|
||||
const char *noomP_formatNodeType(noomP_NodeType node_type) {
|
||||
const char* noomP_formatNodeType(noomP_NodeType node_type) {
|
||||
switch (node_type) {
|
||||
case NOOMP_NODE_PROGRAM:
|
||||
return "program";
|
||||
@@ -480,7 +480,6 @@ noomP_Node* noomP_parseComplexExpression(noomP_Parser* parser, noomP_Node* snode
|
||||
return 0; // unexpected :(
|
||||
}
|
||||
|
||||
|
||||
node = new;
|
||||
} else {
|
||||
return node; // done
|
||||
@@ -705,7 +704,6 @@ int noomP_infixOperatorBP(noomP_Parser* parser, noomL_Token* token, noom_uint_t*
|
||||
*b = 63;
|
||||
return 1;
|
||||
|
||||
|
||||
// oh boy.
|
||||
} else if (noom_memeq(parser->code + token->offset, token->length, "<", 1)) {
|
||||
*a = 50;
|
||||
@@ -1075,7 +1073,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
||||
// equals has already been eaten by loop (thank you loop)
|
||||
|
||||
while (1) {
|
||||
noomP_Node *expr = noomP_parseExpression(parser);
|
||||
noomP_Node* expr = noomP_parseExpression(parser);
|
||||
if (expr == 0) return 0;
|
||||
|
||||
if (noomP_addSubnode(parser, localNode, expr)) return 0;
|
||||
@@ -1409,7 +1407,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) {
|
||||
|
||||
if (!is_done) {
|
||||
parser->error_state = NOOMP_ERROR_RETURN_NOT_END;
|
||||
return 0; //darn it
|
||||
return 0; // darn it
|
||||
}
|
||||
|
||||
return ret;
|
||||
@@ -1803,7 +1801,7 @@ int noomP_initParser(noomP_Parser* parser, const char* code, const char* filenam
|
||||
parser->filename = filename;
|
||||
parser->lex_offset = 0;
|
||||
parser->last_token_length = 0;
|
||||
parser->last_node = (void *)0;
|
||||
parser->last_node = (void*)0;
|
||||
parser->version = version;
|
||||
|
||||
return 0;
|
||||
|
||||
34
src/vm.c
34
src/vm.c
@@ -1,9 +1,9 @@
|
||||
#include "vm.h"
|
||||
#include "helper.h"
|
||||
|
||||
noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size) {
|
||||
noomV_Object *o = noom_alloc(size);
|
||||
if(o == 0) return o;
|
||||
noomV_Object* noomV_allocObj(noom_LuaVM* vm, noomV_ObjTag tag, noom_uint_t size) {
|
||||
noomV_Object* o = noom_alloc(size);
|
||||
if (o == 0) return o;
|
||||
|
||||
o->tag = tag;
|
||||
o->marked = 0;
|
||||
@@ -13,16 +13,16 @@ noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size)
|
||||
return o;
|
||||
}
|
||||
|
||||
noomV_String *noomV_allocStr(noom_LuaVM *vm, const char *str, noom_uint_t len) {
|
||||
noomV_String *s = (noomV_String*)noomV_allocObj(vm, NOOMV_OSTR, sizeof(noomV_String) + len + 1);
|
||||
noomV_String* noomV_allocStr(noom_LuaVM* vm, const char* str, noom_uint_t len) {
|
||||
noomV_String* s = (noomV_String*)noomV_allocObj(vm, NOOMV_OSTR, sizeof(noomV_String) + len + 1);
|
||||
if (s == 0) return 0;
|
||||
noom_memcpy(s->data, str, len);
|
||||
s->data[len] = '\0';
|
||||
return s;
|
||||
}
|
||||
|
||||
noomV_Function *noomV_allocFunc(noom_LuaVM *vm, noomV_String *chunkname) {
|
||||
noomV_Function *f = (noomV_Function*)noomV_allocObj(vm, NOOMV_OFUNC, sizeof(noomV_Function));
|
||||
noomV_Function* noomV_allocFunc(noom_LuaVM* vm, noomV_String* chunkname) {
|
||||
noomV_Function* f = (noomV_Function*)noomV_allocObj(vm, NOOMV_OFUNC, sizeof(noomV_Function));
|
||||
if (f == 0) return 0;
|
||||
f->chunkname = chunkname;
|
||||
f->env = 0;
|
||||
@@ -43,8 +43,8 @@ noomV_Function *noomV_allocFunc(noom_LuaVM *vm, noomV_String *chunkname) {
|
||||
return f;
|
||||
}
|
||||
|
||||
noomV_Table *noomV_allocTable(noom_LuaVM *vm) {
|
||||
noomV_Table *t = (noomV_Table *)noomV_allocObj(vm, NOOMV_OTABLE, sizeof(noomV_Table));
|
||||
noomV_Table* noomV_allocTable(noom_LuaVM* vm) {
|
||||
noomV_Table* t = (noomV_Table*)noomV_allocObj(vm, NOOMV_OTABLE, sizeof(noomV_Table));
|
||||
if (t == 0) return 0;
|
||||
t->meta = 0;
|
||||
t->entries = 0;
|
||||
@@ -54,13 +54,13 @@ noomV_Table *noomV_allocTable(noom_LuaVM *vm) {
|
||||
return t;
|
||||
}
|
||||
|
||||
void noomV_freeObj(noomV_Object *obj) {
|
||||
void noomV_freeObj(noomV_Object* obj) {
|
||||
noom_free(obj);
|
||||
}
|
||||
|
||||
noom_LuaVM *noom_createVM(noom_LuaVersion version) {
|
||||
noom_LuaVM *vm = noom_alloc(sizeof(*vm));
|
||||
if(vm == 0) return 0;
|
||||
noom_LuaVM* noom_createVM(noom_LuaVersion version) {
|
||||
noom_LuaVM* vm = noom_alloc(sizeof(*vm));
|
||||
if (vm == 0) return 0;
|
||||
// initialize the universe to NULL, handles partial OOMs nicely
|
||||
vm->heap = 0;
|
||||
vm->graySet = 0;
|
||||
@@ -72,10 +72,10 @@ noom_LuaVM *noom_createVM(noom_LuaVersion version) {
|
||||
return vm;
|
||||
}
|
||||
|
||||
void noom_destroyVM(noom_LuaVM *vm) {
|
||||
noomV_Object *iter = vm->heap;
|
||||
while(iter) {
|
||||
noomV_Object *cur = iter;
|
||||
void noom_destroyVM(noom_LuaVM* vm) {
|
||||
noomV_Object* iter = vm->heap;
|
||||
while (iter) {
|
||||
noomV_Object* cur = iter;
|
||||
iter = iter->next;
|
||||
noomV_freeObj(cur);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user