Support expressions of sums.

This commit is contained in:
Lawrence Kesteloot 2018-08-02 00:45:58 -07:00
parent b9b4b6fded
commit d6093b9678
4 changed files with 67 additions and 8 deletions

View File

@ -17,8 +17,8 @@ $(ROM): a.out
run: $(ROM)
$(APPLE2E) $(ROM)
a.out: main.o interrupt.o vectors.o platform.o apple2rom.cfg $(LIB)
$(CC65)/ld65 -C apple2rom.cfg -m main.map --dbgfile main.dbg interrupt.o vectors.o platform.o main.o $(LIB)
a.out: main.o interrupt.o vectors.o exporter.o platform.o apple2rom.cfg $(LIB)
$(CC65)/ld65 -C apple2rom.cfg -m main.map --dbgfile main.dbg interrupt.o vectors.o exporter.o platform.o main.o $(LIB)
awk -f rom_usage.awk < main.map
clean:
@ -37,6 +37,7 @@ platform.s: platform.c
platform.o: platform.s
interrupt.o: interrupt.s
vectors.o: vectors.s
exporter.o: exporter.s
crt0.o: crt0.s
$(LIB): crt0.o supervision.lib

10
exporter.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __EXPORTER_H__
#define __EXPORTER_H__
// Defines functions exported in exporter.s.
extern void pushax();
extern void popax();
extern void tosaddax();
#endif // __EXPORTER_H__

14
exporter.s Normal file
View File

@ -0,0 +1,14 @@
; ---------------------------------------------------------------------------
; exporter.s
; ---------------------------------------------------------------------------
;
; Exports cc65-internal routines (such as "pushax") as C-visible ones ("_pushax").
; See the companion header file exporter.h.
.import pushax
.import popax
.import tosaddax
.export _pushax := pushax
.export _popax := popax
.export _tosaddax := tosaddax

46
main.c
View File

@ -1,3 +1,4 @@
#include "exporter.h"
#include "platform.h"
uint8_t *title = "Apple IIa";
@ -352,6 +353,43 @@ static uint16_t parse_uint16(uint8_t **s_ptr) {
return value;
}
/**
* Parse an expression, generating code to compute it, leaving the
* result in AX.
*/
static uint8_t *compile_expression(uint8_t *s) {
int plus_count = 0;
while (1) {
if (*s >= '0' && *s <= '9') {
// Parse number.
uint16_t value = parse_uint16(&s);
g_compiled[g_compiled_length++] = I_LDX;
g_compiled[g_compiled_length++] = value >> 8;
g_compiled[g_compiled_length++] = I_LDA;
g_compiled[g_compiled_length++] = value & 0xFF;
// Push on the number stack.
add_call(pushax);
} else if (*s == '+') {
plus_count += 1;
s += 1;
} else {
break;
}
}
// Pop the last value from the number stack.
add_call(popax);
while (plus_count > 0) {
add_call(tosaddax);
plus_count -= 1;
}
return s;
}
/**
* Tokenize a string in place. Returns (and removes) any line number, or 0xFFFF
* if there's none.
@ -453,13 +491,9 @@ static void process_input_buffer() {
} else if (*s == T_PRINT) {
s += 1;
if (*s >= '0' && *s <= '9') {
if (*s >= '0' && *s <= '9') { // TODO: Add negative sign and open parenthesis.
// Parse expression.
uint16_t value = parse_uint16(&s);
g_compiled[g_compiled_length++] = I_LDX;
g_compiled[g_compiled_length++] = value >> 8;
g_compiled[g_compiled_length++] = I_LDA;
g_compiled[g_compiled_length++] = value & 0xFF;
s = compile_expression(s);
add_call(print_int);
}