From f99b95fcd2f2f870da3411e4de8cdd043a92fd30 Mon Sep 17 00:00:00 2001 From: Luxferre Date: Wed, 10 Aug 2022 16:49:46 +0300 Subject: [PATCH] Implemented variable-length literals and strings --- equi.c | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/equi.c b/equi.c index 49f4dca..81c6101 100644 --- a/equi.c +++ b/equi.c @@ -40,8 +40,6 @@ #define ushort unsigned short /* basic 16-bit integer */ #define uchar unsigned char /* basic 8-bit integer */ #define WS sizeof(ushort) /* Equi word size in bytes */ -#define CLT_ENTRY_LEN 6u /* Amount of significant compiled word characters */ -#define CLT_ENTRY_SIZE (CLT_ENTRY_LEN + WS) /* Full size in bytes taken by one CLT entry */ #define BS 8u /* Backspace character code */ #define CR 13u /* Character return code */ #define LF 10u /* Line feed code */ @@ -263,24 +261,14 @@ uchar a2d(uchar a) { return (a < 0x3aU) ? (a - 0x30U) : (a - 55U); } -/* shape 2-byte vlaue on the main stack from the 4 values of the literal stack */ -void pushLitVal(strict) { - uchar p1, p2, p3, p4; - if(ram.lsp < 4U) { /* if we don't strictly expect 4 bytes, do nothing */ - if(strict) trapout(STACK_UNDERFLOW); - else { - ram.lsp = 0; /* clear the literal stack */ - return; - } - } - else { - p4 = a2d(popLit()); - p3 = a2d(popLit()); - p2 = a2d(popLit()); - p1 = a2d(popLit()); - pushMain((p1<<12U) | (p2<<8U) | (p3<<4U) | p4); - ram.lsp = 0; /* clear the literal stack */ - } +/* shape 2-byte vlaue on the main stack from up to 4 values of the literal stack */ +void pushLitVal() { + uchar p[4U] = {0,0,0,0}, i, thr = 4U; + if(ram.lsp < 4U) thr = ram.lsp; + for(i=0;i 0 && instr != INS_LITSTR && instr != INS_LITCALL && instr != INS_LITINT && instr != INS_CMSTART) - pushLitVal(0); + pushLitVal(); switch(instr) { /* then perform all main interpretation logic */ case INS_IISTART: /* instruction ignore start */ ram.II = 1; /* raise II flag */ @@ -363,7 +351,7 @@ void equi_main_loop() { case INS_QUIT: /* gracefully quit the interpretation mode */ goto brx; case INS_LITINT: /* literal stack -> main stack as short */ - pushLitVal(1U); + pushLitVal(); break; case INS_LITSTR: /* literal stack -> each char at main stack as short */ while(ram.lsp) @@ -371,9 +359,9 @@ void equi_main_loop() { ram.lsp = 0; break; case INS_LITCALL: /* call the saved word from the literal */ - if(ram.lsp < CLT_ENTRY_LEN) + if(ram.lsp < 1) trapout(STACK_UNDERFLOW); - lhash = crc16(&ram.literal_stack[ram.lsp-CLT_ENTRY_LEN], CLT_ENTRY_LEN); + lhash = crc16(&ram.literal_stack[0], ram.lsp); for(pbuf=0;pbuf'); cputc(' ');