Skip to content

Commit

Permalink
fix: array indexing and global arrays (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamesbarford authored Dec 25, 2024
1 parent accf3bb commit cb98951
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/cctrl.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,11 @@ lexeme *cctrlMaybeExpandToken(Cctrl *cc, lexeme *token) {
return token;
}

lexeme *cctrlTokenPeekBy(Cctrl *cc, int idx) {
return tokenRingBufferPeekBy(cc->token_buffer, idx);
lexeme *cctrlTokenPeekBy(Cctrl *cc, int cnt) {
assert(cnt > 0);
/* The -1 is bizzare, however as an argument peeking by `1` you'd
* expect to see the next token, which is infact `0` */
return tokenRingBufferPeekBy(cc->token_buffer, cnt-1);
}

lexeme *cctrlTokenPeek(Cctrl *cc) {
Expand Down
1 change: 1 addition & 0 deletions src/cctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Cctrl *cctrlNew(void);
Cctrl *ccMacroProcessor(StrMap *macro_defs);
lexeme *cctrlTokenGet(Cctrl *cc);
lexeme *cctrlTokenPeek(Cctrl *cc);
lexeme *cctrlTokenPeekBy(Cctrl *cc, int cnt);
void cctrlInitMacroProcessor(Cctrl *cc);
void cctrlTokenRewind(Cctrl *cc);
void cctrlTokenExpect(Cctrl *cc, long expected);
Expand Down
33 changes: 21 additions & 12 deletions src/prslib.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,10 +750,14 @@ static int parseGetPriority(lexeme *tok) {
switch (tok->i64) {
case TK_ARROW:
case '.':
case '[':
case '(':
return 1;
case '!': case '~':

case '[':
return 2;

case '!':
case '~':
case TK_PLUS_PLUS:
case TK_MINUS_MINUS:
case TK_PRE_PLUS_PLUS:
Expand Down Expand Up @@ -1210,25 +1214,30 @@ Ast *parseUnaryExpr(Cctrl *cc) {
return parsePostFixExpr(cc);
}
}
Ast *operand = parseUnaryExpr(cc);

lexeme *peek = cctrlTokenPeekBy(cc,1);
Ast *operand = NULL;
AstType *type = NULL;
lexeme *peek = cctrlTokenPeek(cc);

if (!operand) {
cctrlRewindUntilPunctMatch(cc,tok->i64,NULL);
cctrlRaiseException(cc,"Cannot use unary operator `%c` in this context", tok->i64);
/* XXX: This feels wrong but allows things like:
* !arr[0][1][2] to work properly */
if (tokenPunctIs(peek, '[') && !(unary_op == AST_ADDR || unary_op == AST_DEREF)) {
operand = parseExpr(cc,16);
} else {
operand = parseUnaryExpr(cc);
peek = cctrlTokenPeek(cc);
if (tokenPunctIs(peek, '[') && (operand->kind == AST_CLASS_REF || operand->type->kind == AST_TYPE_ARRAY)) {
astPrint(operand);
cctrlTokenGet(cc);
operand = parseSubscriptExpr(cc, operand);
}
}

if (tokenPunctIs(peek, '[') && (operand->kind == AST_CLASS_REF || operand->type->kind == AST_TYPE_ARRAY)) {
cctrlTokenGet(cc);
operand = parseSubscriptExpr(cc, operand);
}

if (unary_op == AST_ADDR && parseIsFunction(operand)) {
return operand;
}


switch (unary_op) {
case AST_ADDR: type = astMakePointerType(operand->type); break;
case AST_DEREF: type = operand->type->ptr; break;
Expand Down
11 changes: 10 additions & 1 deletion src/transpiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,21 @@ void transpileBinaryOp(TranspileCtx *ctx, aoStr *buf, char *op, Ast *ast, ssize_

*indent = 0;
if (ast->left && ast->left->kind == AST_DEREF) {
int is_bang = !strncmp(op,"!",1);
if (is_bang) {
aoStrCatFmt(buf, "%s", op);
}

if (ast->left) {
transpileAstInternal(ast->left,ctx, indent);
} else {
transpileAstInternal(ast,ctx, indent);
}
aoStrCatFmt(buf, " %s ", op);

if (!is_bang) {
aoStrCatFmt(buf, " %s ", op);
}

if (ast->right) {
transpileAstInternal(ast->right,ctx, indent);
}
Expand Down
16 changes: 16 additions & 0 deletions src/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,22 @@ void asmDataInternal(aoStr *buf, Ast *data) {
return;
}

/* If we have something like:
* ```
* I64 arr[][2] = {
* {1,2},
* {3,4},
* };
* ```
* We need to call this function again.
* */
if (data->kind == AST_ARRAY_INIT) {
listForEach(data->arrayinit) {
asmDataInternal(buf,it->value);
}
return;
}

if (data->type->kind == AST_TYPE_FLOAT) {
aoStrCatPrintf(buf,".quad 0x%lX #%.9f\n\t",
ieee754(data->f64),
Expand Down

0 comments on commit cb98951

Please sign in to comment.