Skip to content

Commit

Permalink
Refactor parser
Browse files Browse the repository at this point in the history
  • Loading branch information
bamless committed May 12, 2024
1 parent 393d645 commit c18426c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 61 deletions.
2 changes: 1 addition & 1 deletion include/jstar/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// Options
#define JSTAR_COMPUTED_GOTOS
#define JSTAR_NAN_TAGGING
/* #undef JSTAR_DBG_PRINT_EXEC */
#define JSTAR_DBG_PRINT_EXEC
/* #undef JSTAR_DBG_PRINT_GC */
/* #undef JSTAR_DBG_STRESS_GC */

Expand Down
12 changes: 6 additions & 6 deletions include/jstar/parse/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,17 @@ JSTAR_API void jsrExprFree(JStarExpr* e);
// STATEMENT NODES
// -----------------------------------------------------------------------------

JSTAR_API JStarStmt* jsrFuncDecl(int line, const JStarTok* name, const FormalArgs* args, bool isGenerator, JStarStmt* body);
JSTAR_API JStarStmt* jsrNativeDecl(int line, JStarTok* name, const FormalArgs* args);
JSTAR_API JStarStmt* jsrFuncDecl(int line, const JStarIdentifier* name, const FormalArgs* args, bool isGenerator, JStarStmt* body);
JSTAR_API JStarStmt* jsrNativeDecl(int line, const JStarIdentifier* name, const FormalArgs* args);
JSTAR_API JStarStmt* jsrForStmt(int line, JStarStmt* init, JStarExpr* cond, JStarExpr* act, JStarStmt* body);
JSTAR_API JStarStmt* jsrClassDecl(int line, JStarTok* clsName, JStarExpr* sup, ext_vector(JStarStmt*) methods);
JSTAR_API JStarStmt* jsrImportStmt(int line, ext_vector(JStarIdentifier) modules, ext_vector(JStarIdentifier) names, JStarTok* as);
JSTAR_API JStarStmt* jsrClassDecl(int line, const JStarIdentifier* clsName, JStarExpr* sup, ext_vector(JStarStmt*) methods);
JSTAR_API JStarStmt* jsrImportStmt(int line, ext_vector(JStarIdentifier) modules, ext_vector(JStarIdentifier) names, const JStarIdentifier* as);
JSTAR_API JStarStmt* jsrVarDecl(int line, bool isUnpack, ext_vector(JStarIdentifier) ids, JStarExpr* init);
JSTAR_API JStarStmt* jsrTryStmt(int line, JStarStmt* blck, ext_vector(JStarStmt*) excs, JStarStmt* ensure);
JSTAR_API JStarStmt* jsrIfStmt(int line, JStarExpr* cond, JStarStmt* thenStmt, JStarStmt* elseStmt);
JSTAR_API JStarStmt* jsrForEachStmt(int line, JStarStmt* varDecl, JStarExpr* iter, JStarStmt* body);
JSTAR_API JStarStmt* jsrExceptStmt(int line, JStarExpr* cls, JStarTok* varName, JStarStmt* block);
JSTAR_API JStarStmt* jsrWithStmt(int line, JStarExpr* e, JStarTok* varName, JStarStmt* block);
JSTAR_API JStarStmt* jsrExceptStmt(int line, JStarExpr* cls, const JStarIdentifier* varName, JStarStmt* block);
JSTAR_API JStarStmt* jsrWithStmt(int line, JStarExpr* e, const JStarIdentifier* varName, JStarStmt* block);
JSTAR_API JStarStmt* jsrWhileStmt(int line, JStarExpr* cond, JStarStmt* body);
JSTAR_API JStarStmt* jsrBlockStmt(int line, ext_vector(JStarStmt*) list);
JSTAR_API JStarStmt* jsrReturnStmt(int line, JStarExpr* e);
Expand Down
43 changes: 25 additions & 18 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1730,25 +1730,35 @@ static void compileFormalArg(Compiler* c, const FormalArg* arg, int argIdx, int
}
}

static void compileFormalArgs(Compiler* c, const ext_vector(FormalArg) args, int line) {
int argIdx = 0;
ext_vec_foreach(const FormalArg* arg, args) {
compileFormalArg(c, arg, argIdx++, line);
}
}

static void unpackFormalArgs(Compiler* c, ext_vector(FormalArg) args, int line) {
int argIdx = 0;
ext_vec_foreach(const FormalArg* arg, args) {
if(arg->type == UNPACK) {
char name[sizeof(UNPACK_ARG_FMT) + STRLEN_FOR_INT(int)];
sprintf(name, UNPACK_ARG_FMT, argIdx);
JStarIdentifier id = createIdentifier(name);

compileVarLit(c, &id, false, line);
emitOpcode(c, OP_UNPACK, line);
emitByte(c, ext_vec_size(arg->as.unpack), line);

ext_vec_foreach(const JStarIdentifier* id, arg->as.unpack) {
Variable unpackedArg = declareVar(c, id, false, line);
defineVar(c, &unpackedArg, line);
}
if(arg->type == SIMPLE) {
continue;
}

char name[sizeof(UNPACK_ARG_FMT) + STRLEN_FOR_INT(int)];
sprintf(name, UNPACK_ARG_FMT, argIdx);
JStarIdentifier id = createIdentifier(name);

compileVarLit(c, &id, false, line);
emitOpcode(c, OP_UNPACK, line);
emitByte(c, ext_vec_size(arg->as.unpack), line);

ext_vec_foreach(const JStarIdentifier* id, arg->as.unpack) {
Variable unpackedArg = declareVar(c, id, false, line);
defineVar(c, &unpackedArg, line);
}

argIdx++;
}
argIdx++;
}

static ObjFunction* function(Compiler* c, ObjModule* m, ObjString* name, JStarStmt* s) {
Expand All @@ -1772,10 +1782,7 @@ static ObjFunction* function(Compiler* c, ObjModule* m, ObjString* name, JStarSt
int receiverLocal = addLocal(c, &receiverName, s->line);
initializeLocal(c, receiverLocal);

int argIdx = 0;
ext_vec_foreach(const FormalArg* arg, s->as.decl.as.fun.formalArgs.args) {
compileFormalArg(c, arg, argIdx, s->line);
}
compileFormalArgs(c, s->as.decl.as.fun.formalArgs.args, s->line);

if(isVararg) {
Variable vararg = declareVar(c, varargName, false, s->line);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ static JSR_NATIVE(jsr_Class_string) {
jsrBufferPush(&str);
return true;
}

// end

// class Number
JSR_NATIVE(jsr_Number_construct) {
if(jsrIsNumber(vm, 1)) {
Expand Down Expand Up @@ -492,9 +492,10 @@ JSR_NATIVE(jsr_Generator_isDone) {

JSR_NATIVE(jsr_Generator_string) {
ObjGenerator* gen = AS_GENERATOR(vm->apiStack[0]);
const Prototype* proto = &gen->closure->fn->proto;
JStarBuffer str;
jsrBufferInit(vm, &str);
jsrBufferAppendf(&str, "<generator@%p>", (void*)gen);
jsrBufferAppendf(&str, "<Generator %s.%s@%p>", proto->module->name->data, proto->name->data, (void*)gen);
jsrBufferPush(&str);
return true;
}
Expand Down
26 changes: 13 additions & 13 deletions src/parse/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ JStarExpr* jsrCompundAssExpr(int line, JStarTokType op, JStarExpr* lval, JStarEx

JStarExpr* jsrFuncLiteral(int line, const FormalArgs* args, bool isGenerator, JStarStmt* body) {
JStarExpr* e = newExpr(line, JSR_FUNC_LIT);
e->as.funLit.func = jsrFuncDecl(line, &(JStarTok){0}, args, isGenerator, body);
e->as.funLit.func = jsrFuncDecl(line, &(JStarIdentifier){0}, args, isGenerator, body);
return e;
}

Expand Down Expand Up @@ -265,27 +265,27 @@ static JStarStmt* newDecl(int line, JStarStmtType type) {

// Declarations

JStarStmt* jsrFuncDecl(int line, const JStarTok* name, const FormalArgs* args, bool isGenerator, JStarStmt* body) {
JStarStmt* jsrFuncDecl(int line, const JStarIdentifier* name, const FormalArgs* args, bool isGenerator, JStarStmt* body) {
JStarStmt* f = newDecl(line, JSR_FUNCDECL);
f->as.decl.as.fun.id = (JStarIdentifier){name->length, name->lexeme};
f->as.decl.as.fun.id = *name;
f->as.decl.as.fun.formalArgs = *args;
f->as.decl.as.fun.body = body;
f->as.decl.as.fun.isGenerator = isGenerator;
return f;
}

JStarStmt* jsrNativeDecl(int line, JStarTok* name, const FormalArgs* args) {
JStarStmt* jsrNativeDecl(int line, const JStarIdentifier* name, const FormalArgs* args) {
JStarStmt* n = newDecl(line, JSR_NATIVEDECL);
n->as.decl.as.native.id = (JStarIdentifier){name->length, name->lexeme};
n->as.decl.as.native.id = *name;
n->as.decl.as.native.formalArgs = *args;
return n;
}

JStarStmt* jsrClassDecl(int line, JStarTok* clsName, JStarExpr* sup,
JStarStmt* jsrClassDecl(int line, const JStarIdentifier* clsName, JStarExpr* sup,
ext_vector(JStarStmt*) methods) {
JStarStmt* c = newDecl(line, JSR_CLASSDECL);
c->as.decl.as.cls.sup = sup;
c->as.decl.as.cls.id = (JStarIdentifier){clsName->length, clsName->lexeme};
c->as.decl.as.cls.id = *clsName;
c->as.decl.as.cls.methods = methods;
return c;
}
Expand All @@ -300,10 +300,10 @@ JStarStmt* jsrVarDecl(int line, bool isUnpack, ext_vector(JStarIdentifier) ids,

// Control flow statements

JStarStmt* jsrWithStmt(int line, JStarExpr* e, JStarTok* varName, JStarStmt* block) {
JStarStmt* jsrWithStmt(int line, JStarExpr* e, const JStarIdentifier* varName, JStarStmt* block) {
JStarStmt* w = newStmt(line, JSR_WITH);
w->as.withStmt.e = e;
w->as.withStmt.var = (JStarIdentifier){varName->length, varName->lexeme};
w->as.withStmt.var = *varName;
w->as.withStmt.block = block;
return w;
}
Expand Down Expand Up @@ -353,11 +353,11 @@ JStarStmt* jsrBlockStmt(int line, ext_vector(JStarStmt*) list) {
}

JStarStmt* jsrImportStmt(int line, ext_vector(JStarIdentifier) modules,
ext_vector(JStarIdentifier) names, JStarTok* as) {
ext_vector(JStarIdentifier) names, const JStarIdentifier* as) {
JStarStmt* s = newStmt(line, JSR_IMPORT);
s->as.importStmt.modules = modules;
s->as.importStmt.names = names;
s->as.importStmt.as = (JStarIdentifier){as->length, as->lexeme};
s->as.importStmt.as = *as;
return s;
}

Expand All @@ -375,11 +375,11 @@ JStarStmt* jsrTryStmt(int line, JStarStmt* blck, ext_vector(JStarStmt*) excs, JS
return s;
}

JStarStmt* jsrExceptStmt(int line, JStarExpr* cls, JStarTok* varName, JStarStmt* block) {
JStarStmt* jsrExceptStmt(int line, JStarExpr* cls, const JStarIdentifier* varName, JStarStmt* block) {
JStarStmt* s = newStmt(line, JSR_EXCEPT);
s->as.excStmt.block = block;
s->as.excStmt.cls = cls;
s->as.excStmt.var = (JStarIdentifier){varName->length, varName->lexeme};
s->as.excStmt.var = *varName;
return s;
}

Expand Down
43 changes: 22 additions & 21 deletions src/parse/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ static FormalArgs formalArgs(Parser* p, JStarTokType open, JStarTokType close) {
if(peek.type == TOK_LPAREN) {
arg = parseUnpackArgument(p);
} else {
JStarTok argument = advance(p);
arg = (FormalArg){.type = SIMPLE, .as = {.simple = createIdentifier(&argument)}};
JStarTok argName = advance(p);
arg = (FormalArg){.type = SIMPLE, .as = {.simple = createIdentifier(&argName)}};
}

skipNewLines(p);
Expand Down Expand Up @@ -606,7 +606,7 @@ static JStarStmt* importStmt(Parser* p) {
skipNewLines(p);
}

JStarTok asName = {0};
JStarIdentifier asName = {0};
ext_vector(JStarIdentifier) importNames = NULL;

if(match(p, TOK_FOR)) {
Expand All @@ -623,7 +623,8 @@ static JStarStmt* importStmt(Parser* p) {
} else if(match(p, TOK_AS)) {
advance(p);
skipNewLines(p);
asName = require(p, TOK_IDENTIFIER);
JStarTok as = require(p, TOK_IDENTIFIER);
asName = createIdentifier(&as);
}

requireStmtEnd(p);
Expand All @@ -647,9 +648,10 @@ static JStarStmt* tryStmt(Parser* p) {
skipNewLines(p);

JStarTok var = require(p, TOK_IDENTIFIER);
JStarIdentifier varName = createIdentifier(&var);

JStarStmt* block = blockStmt(p);
ext_vec_push_back(excs, jsrExceptStmt(excLine, cls, &var, block));
ext_vec_push_back(excs, jsrExceptStmt(excLine, cls, &varName, block));
}

if(match(p, TOK_ENSURE)) {
Expand Down Expand Up @@ -687,10 +689,11 @@ static JStarStmt* withStmt(Parser* p) {
skipNewLines(p);

JStarTok var = require(p, TOK_IDENTIFIER);
JStarIdentifier varName = createIdentifier(&var);
JStarStmt* block = blockStmt(p);
require(p, TOK_END);

return jsrWithStmt(line, e, &var, block);
return jsrWithStmt(line, e, &varName, block);
}

static ext_vector(JStarExpr*) parseDecorators(Parser* p) {
Expand All @@ -711,13 +714,8 @@ static void freeDecorators(ext_vector(JStarExpr*) decorators) {
ext_vec_free(decorators);
}

static JStarTok ctorName(int line) {
return (JStarTok){
.line = line,
.type = TOK_IDENTIFIER,
.lexeme = CONSTRUCT_ID,
.length = strlen(CONSTRUCT_ID),
};
static JStarIdentifier ctorName(int line) {
return (JStarIdentifier) {strlen(CONSTRUCT_ID), CONSTRUCT_ID};
}

static JStarStmt* funcDecl(Parser* p, bool parseCtor) {
Expand All @@ -729,12 +727,13 @@ static JStarStmt* funcDecl(Parser* p, bool parseCtor) {
JStarTok funTok = advance(p);
skipNewLines(p);

JStarTok funcName;
JStarIdentifier funcName;
if(parseCtor && funTok.type == TOK_CTOR) {
fn.isCtor = true;
funcName = ctorName(line);
} else {
funcName = require(p, TOK_IDENTIFIER);
JStarTok fun = require(p, TOK_IDENTIFIER);
funcName = createIdentifier(&fun);
}

skipNewLines(p);
Expand All @@ -756,20 +755,21 @@ static JStarStmt* nativeDecl(Parser* p, bool parseCtor) {
advance(p);
skipNewLines(p);

JStarTok funcName;
JStarIdentifier nativeName;
if(parseCtor && p->peek.type == TOK_CTOR) {
advance(p);
funcName = ctorName(line);
nativeName = ctorName(line);
} else {
funcName = require(p, TOK_IDENTIFIER);
JStarTok nat = require(p, TOK_IDENTIFIER);
nativeName = createIdentifier(&nat);
}

skipNewLines(p);

FormalArgs args = formalArgs(p, TOK_LPAREN, TOK_RPAREN);
requireStmtEnd(p);

return jsrNativeDecl(line, &funcName, &args);
return jsrNativeDecl(line, &nativeName, &args);
}

static JStarStmt* classDecl(Parser* p) {
Expand All @@ -778,7 +778,8 @@ static JStarStmt* classDecl(Parser* p) {
advance(p);
skipNewLines(p);

JStarTok clsName = require(p, TOK_IDENTIFIER);
JStarTok cls = require(p, TOK_IDENTIFIER);
JStarIdentifier clsName = createIdentifier(&cls);
skipNewLines(p);

JStarExpr* sup = NULL;
Expand Down Expand Up @@ -940,7 +941,7 @@ static JStarStmt* parseProgram(Parser* p) {

// Top level function doesn't have name or arguments, so pass them empty
FormalArgs args = {0};
return jsrFuncDecl(0, &(JStarTok){0}, &args, false, jsrBlockStmt(0, stmts));
return jsrFuncDecl(0, &(JStarIdentifier){0}, &args, false, jsrBlockStmt(0, stmts));
}

// -----------------------------------------------------------------------------
Expand Down

0 comments on commit c18426c

Please sign in to comment.