Browse Source

Switched "short" to "int", "byte" to "char"

remotes/origin/HEAD
Seth Stubbs 3 months ago
parent
commit
af756b451f
  1. 4
      Makefile
  2. 4
      src/main.c
  3. 42
      src/uasm.c
  4. 4
      src/ulex.c
  5. 4
      src/ulex.h
  6. 24
      src/uparse.c
  7. 8
      src/uparse.h
  8. 2
      test.uxnc

4
Makefile

@ -1,6 +1,6 @@
CC=clang
CFLAGS=-fPIE -Wall -g -fsanitize=address -O2 -Isrc -std=c89
LDFLAGS=-lm -fsanitize=address
CFLAGS=-fPIE -Wall -O2 -Isrc -std=c89
LDFLAGS=-lm #-fsanitize=address
OUT=bin/uxncle
CHDR=\

4
src/main.c

@ -3,8 +3,8 @@
int main() {
UASTNode *tree = UP_parseSource(
"short a;\n"
"short b;"
"int a;\n"
"int b;"
"a = 8;\n"
"b = 64 / a / 2;"
"prntint a;"

42
src/uasm.c

@ -93,7 +93,7 @@ void cError(UCompState *state, const char *fmt, ...) {
exit(EXIT_FAILURE);
}
void writeShortLit(UCompState *state, uint16_t lit) {
void writeIntLit(UCompState *state, uint16_t lit) {
fprintf(state->out, "#%.4x ", lit);
state->pushed += 2;
}
@ -105,8 +105,8 @@ void writeByteLit(UCompState *state, uint8_t lit) {
uint16_t getSize(UCompState *state, UVar *var) {
switch(var->type) {
case TYPE_BYTE: return 1;
case TYPE_SHORT: return 2;
case TYPE_CHAR: return 1;
case TYPE_INT: return 2;
default:
cError(state, "unknown type! [%d]", var->type);
return 0;
@ -126,14 +126,14 @@ uint16_t getScopeSize(UCompState *state, UScope *scope) {
void pushScope(UCompState *state, UScope *scope) {
state->scopes[state->sCount++] = scope;
writeShortLit(state, getScopeSize(state, scope));
writeIntLit(state, getScopeSize(state, scope));
fwrite(";alloc-uxncle JSR2\n", 19, 1, state->out);
state->pushed -= 2;
}
void popScope(UCompState *state) {
UScope *scope = state->scopes[--state->sCount];
writeShortLit(state, getScopeSize(state, scope));
writeIntLit(state, getScopeSize(state, scope));
fwrite(";dealloc-uxncle JSR2\n", 21, 1, state->out);
state->pushed -= 2;
}
@ -154,10 +154,10 @@ uint16_t getOffset(UCompState *state, int scope, int var) {
return offsetAddr;
}
void getShortVar(UCompState *state, int scope, int var) {
void getIntVar(UCompState *state, int scope, int var) {
uint16_t offsetAddr = getOffset(state, scope, var);
writeShortLit(state, offsetAddr); /* write the offset */
writeIntLit(state, offsetAddr); /* write the offset */
fprintf(state->out, ";peek-uxncle-short JSR2\n"); /* call the mem lib */
}
@ -165,17 +165,17 @@ UVar* getVarByID(UCompState *state, int scope, int var) {
return &state->scopes[scope]->vars[var];
}
void setShortVar(UCompState *state, int scope, int var) {
void setIntVar(UCompState *state, int scope, int var) {
uint16_t offsetAddr = getOffset(state, scope, var);
writeShortLit(state, offsetAddr); /* write the offset */
writeIntLit(state, offsetAddr); /* write the offset */
fprintf(state->out, ";poke-uxncle-short JSR2\n"); /* call the mem lib */
state->pushed -= 4; /* pops the offset (short) & the value (short) */
}
void setVar(UCompState *state, int scope, int var, UVarType type) {
switch(type) {
case TYPE_SHORT: setShortVar(state, scope, var); break;
case TYPE_INT: setIntVar(state, scope, var); break;
default:
cError(state, "Unimplemented setter for type '%s'", getTypeName(type));
}
@ -185,7 +185,7 @@ UVarType getVar(UCompState *state, int scope, int var) {
UVar *rawVar = getVarByID(state, scope, var);
switch(rawVar->type) {
case TYPE_SHORT: getShortVar(state, scope, var); break;
case TYPE_INT: getIntVar(state, scope, var); break;
default:
cError(state, "Unimplemented getter for type '%s'", getTypeName(rawVar->type));
}
@ -221,21 +221,21 @@ void pop(UCompState *state, int size) {
void dupValue(UCompState *state, UVarType type) {
switch(type) {
case TYPE_SHORT: fprintf(state->out, "DUP2\n"); state->pushed+=2; break;
case TYPE_BYTE: fprintf(state->out, "DUP\n"); state->pushed++; break;
case TYPE_INT: fprintf(state->out, "DUP2\n"); state->pushed+=2; break;
case TYPE_CHAR: fprintf(state->out, "DUP\n"); state->pushed++; break;
default:
cError(state, "Unknown variable type! [%d]", type);
}
}
void cShortArith(UCompState *state, const char *instr) {
void cIntArith(UCompState *state, const char *instr) {
fprintf(state->out, "%s2\n", instr);
state->pushed -= 2;
}
void doArith(UCompState *state, const char *instr, UVarType type) {
switch(type) {
case TYPE_SHORT: cShortArith(state, instr); break;
case TYPE_INT: cIntArith(state, instr); break;
default:
cError(state, "Unknown variable type! [%d]", type);
}
@ -283,7 +283,7 @@ UVarType compileExpression(UCompState *state, UASTNode *node) {
case NODE_SUB: doArith(state, "SUB", lType); break;
case NODE_MUL: doArith(state, "MUL", lType); break;
case NODE_DIV: doArith(state, "DIV", lType); break;
case NODE_SHORTLIT: writeShortLit(state, ((UASTIntNode*)node)->num); return TYPE_SHORT; break;
case NODE_INTLIT: writeIntLit(state, ((UASTIntNode*)node)->num); return TYPE_INT; break;
case NODE_VAR: return compileVar(state, node); break;
default:
cError(state, "unknown AST node!! [%d]\n", node->type);
@ -298,7 +298,7 @@ void compilePrintInt(UCompState *state, UASTNode *node) {
state->pushed-=2;
}
void compileShort(UCompState *state, UASTNode *node) {
void compileInt(UCompState *state, UASTNode *node) {
UVarType type;
UASTVarNode *var = (UASTVarNode*)node;
UVar *rawVar = getVarByID(state, var->scope, var->var);
@ -306,9 +306,9 @@ void compileShort(UCompState *state, UASTNode *node) {
/* if there's no assignment, the default value will be scary undefined memory :O */
if (node->left) {
type = compileExpression(state, node->left);
if (type != TYPE_SHORT)
cError(state, "Cannot assign type '%s' to %.*s of type 'short'", getTypeName(type), rawVar->len, rawVar->name);
setShortVar(state, var->scope, var->var);
if (type != TYPE_INT)
cError(state, "Cannot assign type '%s' to %.*s of type 'int'", getTypeName(type), rawVar->len, rawVar->name);
setIntVar(state, var->scope, var->var);
}
}
@ -327,7 +327,7 @@ void compileAST(UCompState *state, UASTNode *node) {
while (node) {
switch(node->type) {
case NODE_STATE_PRNT: compilePrintInt(state, node); break;
case NODE_STATE_SHORT: compileShort(state, node); break;
case NODE_STATE_INT: compileInt(state, node); break;
case NODE_STATE_EXPR: compileExpression(state, node->left); break;
case NODE_STATE_SCOPE: compileScope(state, node); break;
default:

4
src/ulex.c

@ -8,8 +8,8 @@ typedef struct {
} UReservedWord;
UReservedWord reservedWords[] = {
{TOKEN_BYTE, "byte", 4},
{TOKEN_SHORT, "short", 5},
{TOKEN_CHAR, "char", 4},
{TOKEN_INT, "int", 3},
{TOKEN_VOID, "void", 4},
{TOKEN_PRINTINT, "prntint", 7}
};

4
src/ulex.h

@ -3,8 +3,8 @@
typedef enum {
/* keywords */
TOKEN_BYTE,
TOKEN_SHORT,
TOKEN_CHAR,
TOKEN_INT,
TOKEN_VOID,
TOKEN_PRINTINT,

24
src/uparse.c

@ -64,7 +64,7 @@ UASTNode *newNode(UParseState *state, UASTNodeType type, UASTNode *left, UASTNod
}
UASTNode *newNumNode(UParseState *state, UASTNode *left, UASTNode *right, int num) {
UASTIntNode *node = (UASTIntNode*)newBaseNode(state, sizeof(UASTIntNode), NODE_SHORTLIT, left, right);
UASTIntNode *node = (UASTIntNode*)newBaseNode(state, sizeof(UASTIntNode), NODE_INTLIT, left, right);
node->num = num;
return (UASTNode*)node;
}
@ -212,8 +212,8 @@ UASTNode* identifer(UParseState *state, UASTNode *left, Precedence currPrec) {
ParseRule ruleTable[] = {
/* keywords */
{NULL, NULL, PREC_NONE}, /* TOKEN_BYTE */
{NULL, NULL, PREC_NONE}, /* TOKEN_SHORT */
{NULL, NULL, PREC_NONE}, /* TOKEN_CHAR */
{NULL, NULL, PREC_NONE}, /* TOKEN_INT */
{NULL, NULL, PREC_NONE}, /* TOKEN_VOID */
{NULL, NULL, PREC_NONE}, /* TOKEN_PRINTINT */
@ -290,7 +290,7 @@ UASTNode* printStatement(UParseState *state) {
return newNode(state, NODE_STATE_PRNT, expression(state), NULL);
}
UASTNode* shortStatement(UParseState *state) {
UASTNode* intStatement(UParseState *state) {
UASTVarNode *node;
int var;
@ -299,10 +299,10 @@ UASTNode* shortStatement(UParseState *state) {
error(state, "Expected identifer!");
/* define the variable */
var = newVar(state, TYPE_SHORT, state->previous.str, state->previous.len);
var = newVar(state, TYPE_INT, state->previous.str, state->previous.len);
/* if it's assigned a value, evaluate the expression & set the left node, if not set it to NULL */
node = (UASTVarNode*)newBaseNode(state, sizeof(UASTVarNode), NODE_STATE_SHORT, (match(state, TOKEN_EQUAL)) ? expression(state) : NULL, NULL);
node = (UASTVarNode*)newBaseNode(state, sizeof(UASTVarNode), NODE_STATE_INT, (match(state, TOKEN_EQUAL)) ? expression(state) : NULL, NULL);
node->var = var;
node->scope = state->sCount-1;
return (UASTNode*)node;
@ -336,8 +336,8 @@ UASTNode* statement(UParseState *state) {
/* find a statement match */
if (match(state, TOKEN_PRINTINT)) {
node = printStatement(state);
} else if (match(state, TOKEN_SHORT)) {
node = shortStatement(state);
} else if (match(state, TOKEN_INT)) {
node = intStatement(state);
} else if (match(state, TOKEN_LEFT_BRACE)) {
node = scopeStatement(state);
} else {
@ -359,10 +359,10 @@ void printNode(UASTNode *node) {
case NODE_MUL: printf("MUL"); break;
case NODE_DIV: printf("DIV"); break;
case NODE_ASSIGN: printf("ASSIGN"); break;
case NODE_SHORTLIT: printf("[%d]", ((UASTIntNode*)node)->num); break;
case NODE_INTLIT: printf("[%d]", ((UASTIntNode*)node)->num); break;
case NODE_STATE_PRNT: printf("PRNT"); break;
case NODE_STATE_SCOPE: printf("SCPE"); break;
case NODE_STATE_SHORT: printf("SHRT"); break;
case NODE_STATE_INT: printf("INT"); break;
case NODE_VAR: printf("VAR[%d]", ((UASTVarNode*)node)->var); break;
case NODE_STATE_EXPR: printf("EXPR"); break;
default: break;
@ -382,8 +382,8 @@ void printTree(UASTNode *node, int indent) {
const char* getTypeName(UVarType type) {
switch(type) {
case TYPE_SHORT: return "short";
case TYPE_BYTE: return "byte";
case TYPE_INT: return "int";
case TYPE_CHAR: return "char";
default:
return "<errtype>";
}

8
src/uparse.h

@ -13,7 +13,7 @@ typedef enum {
NODE_SUB,
NODE_MUL,
NODE_DIV,
NODE_SHORTLIT,
NODE_INTLIT,
NODE_VAR,
NODE_ASSIGN, /* node->left holds Var node, node->right holds expression */
/*
@ -21,15 +21,15 @@ typedef enum {
node->left holds expression tree, node->right holds the next statement
*/
NODE_STATE_PRNT,
NODE_STATE_SHORT,
NODE_STATE_INT,
NODE_STATE_EXPR,
/* scopes are different, node->left holds the statement tree for the scope, node->right holds the next statement */
NODE_STATE_SCOPE,
} UASTNodeType;
typedef enum {
TYPE_BYTE,
TYPE_SHORT,
TYPE_CHAR,
TYPE_INT,
TYPE_NONE
} UVarType;

2
test.uxnc

@ -1,4 +1,4 @@
byte sub(byte a, byte b) {
int sub(int a, int b) {
return a - b;
}

Loading…
Cancel
Save