Browse Source

Added some boilerplate to print the evaluated expression to the console

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

31
out.tal

@ -1,4 +1,29 @@
|10 @Console [ &pad $8 &char $1 &byte $1 &short $2 &string $2 ]
|0000
@number [ &started $1 ]
|0100
#02 #04 MUL
#06 ADD
BRK
#0006 #0002 #0015 MUL2
#0003 #0006 MUL2
ADD2
ADD2
;print-decimal JSR2
BRK
@print-decimal
#00 .number/started STZ
DUP2 #2710 DIV2 DUP2 ,&digit JSR #2710 MUL2 SUB2
DUP2 #03e8 DIV2 DUP2 ,&digit JSR #03e8 MUL2 SUB2
DUP2 #0064 DIV2 DUP2 ,&digit JSR #0064 MUL2 SUB2
DUP2 #000a DIV2 DUP2 ,&digit JSR #000a MUL2 SUB2
,&digit JSR
.number/started LDZ ,&end JCN
LIT '0 .Console/char DEO
&end
JMP2r
&digit
SWP POP
DUP .number/started LDZ ORA #02 JCN
POP JMP2r
LIT '0 ADD .Console/char DEO
#01 .number/started STZ
JMP2r

2
src/main.c

@ -2,7 +2,7 @@
#include "uasm.h"
int main() {
UASTNode *tree = UP_parseSource("2 * 4 + 6");
UASTNode *tree = UP_parseSource("6 + 2 * 21 + 3 * 6");
UA_genTal(tree, fopen("out.tal", "w"));
return 0;

42
src/uasm.c

@ -1,13 +1,35 @@
#include "uasm.h"
static const char preamble[] =
static const char preamble[] =
"|10 @Console [ &pad $8 &char $1 &byte $1 &short $2 &string $2 ]\n"
"|0000\n"
"@number [ &started $1 ]\n"
"|0100\n";
static const char postamble[] =
"BRK";
void writeLit(FILE *out, int lit) {
fprintf(out, "#%.2x ", lit);
"\n;print-decimal JSR2\n"
"BRK\n"
"@print-decimal\n"
"\t#00 .number/started STZ\n"
"\tDUP2 #2710 DIV2 DUP2 ,&digit JSR #2710 MUL2 SUB2\n"
"\tDUP2 #03e8 DIV2 DUP2 ,&digit JSR #03e8 MUL2 SUB2\n"
"\tDUP2 #0064 DIV2 DUP2 ,&digit JSR #0064 MUL2 SUB2\n"
"\tDUP2 #000a DIV2 DUP2 ,&digit JSR #000a MUL2 SUB2\n"
"\t,&digit JSR\n"
"\t.number/started LDZ ,&end JCN\n"
"\tLIT '0 .Console/char DEO\n"
"\t&end\n"
"JMP2r\n"
"\t&digit\n"
"\tSWP POP\n"
"\tDUP .number/started LDZ ORA #02 JCN\n"
"\tPOP JMP2r\n"
"\tLIT '0 ADD .Console/char DEO\n"
"\t#01 .number/started STZ\n"
"JMP2r\n";
void writeShortLit(FILE *out, int lit) {
fprintf(out, "#%.4x ", lit);
}
void compileAST(FILE *out, UASTNode *node) {
@ -18,11 +40,11 @@ void compileAST(FILE *out, UASTNode *node) {
compileAST(out, node->right);
switch(node->type) {
case NODE_ADD: fwrite("ADD\n", 4, 1, out); break;
case NODE_SUB: fwrite("SUB\n", 4, 1, out); break;
case NODE_MUL: fwrite("MUL\n", 4, 1, out); break;
case NODE_DIV: fwrite("DIV\n", 4, 1, out); break;
case NODE_INTLIT: writeLit(out, node->num); break;
case NODE_ADD: fwrite("ADD2\n", 5, 1, out); break;
case NODE_SUB: fwrite("SUB2\n", 5, 1, out); break;
case NODE_MUL: fwrite("MUL2\n", 5, 1, out); break;
case NODE_DIV: fwrite("DIV2\n", 5, 1, out); break;
case NODE_INTLIT: writeShortLit(out, node->num); break;
default:
printf("Compiler error! unknown AST node!! [%d]\n", node->type);
exit(0);

1
src/uasm.h

@ -1,6 +1,7 @@
#ifndef UASM_H
#define UASM_H
#include "uxncle.h"
#include "uparse.h"
#include <stdio.h>

2
src/ulex.c

@ -9,7 +9,7 @@ typedef struct {
UReservedWord reservedWords[] = {
{TOKEN_BYTE, "byte", 4},
{TOKEN_BYTE16, "byte16", 6},
{TOKEN_SHORT, "short", 5},
{TOKEN_VOID, "void", 4}
};

2
src/ulex.h

@ -4,7 +4,7 @@
typedef enum {
/* keywords */
TOKEN_BYTE,
TOKEN_BYTE16,
TOKEN_SHORT,
TOKEN_VOID,
/* literals */

1
src/uparse.c

@ -33,7 +33,6 @@ int str2int(char *str, int len) {
return ret;
}
/* ==================================[[ generic helper functions ]]================================== */
UASTNode *newNode(UParseState *state, UASTNodeType type, UASTNode *left, UASTNode *right) {

2
test.uxnc

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

Loading…
Cancel
Save