Activate a lot of dc instructions
diff --git a/include/lang.h b/include/lang.h
index 0d2d83c..e930b7e 100644
--- a/include/lang.h
+++ b/include/lang.h
@@ -95,6 +95,10 @@
BC_INST_HALT,
+#ifdef DC_CONFIG
+ BC_INST_INVALID = -1,
+#endif // DC_CONFIG
+
} BcInst;
typedef struct BcEntry {
@@ -187,6 +191,7 @@
#define bc_array_free bc_vec_free
extern const char bc_inst_chars[];
+extern const uint8_t bc_inst_operands[];
extern const char bc_func_main[];
extern const char bc_func_read[];
diff --git a/include/lex.h b/include/lex.h
index f540c79..39fca97 100644
--- a/include/lex.h
+++ b/include/lex.h
@@ -28,6 +28,7 @@
#include <status.h>
#include <vector.h>
+#include <lang.h>
// BC_LEX_OP_NEGATE is not used in lexing; it is only for parsing.
typedef enum BcLexType {
@@ -177,6 +178,7 @@
// ** Exclude start. **
extern const BcLexType dc_lex_tokens[];
+extern const BcInst dc_parse_insts[];
// Common code.
diff --git a/src/data.c b/src/data.c
index 1df8553..73657a8 100644
--- a/src/data.c
+++ b/src/data.c
@@ -144,6 +144,12 @@
"edED_^*/%+-=;?~<>!|&`{}@[],NVMACaI.LlrOqpQsSJjPR$H";
#endif // NDEBUG
+const uint8_t bc_inst_operands[] = {
+ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
+ 1, 1, 1, 1, 0, 1, 1, 1, 0, 0,
+};
+
#ifdef BC_CONFIG
const BcLexKeyword bc_lex_kws[20] = {
BC_LEX_KW_ENTRY("auto", 4, true),
@@ -237,6 +243,32 @@
BC_LEX_OP_REL_GE, BC_LEX_OP_MODEXP, BC_LEX_INVALID, BC_LEX_OP_DIVMOD,
BC_LEX_INVALID
};
+
+const BcInst dc_parse_insts[] = {
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_NEG, BC_INST_POWER, BC_INST_MULTIPLY, BC_INST_DIVIDE,
+ BC_INST_MODULUS, BC_INST_PLUS, BC_INST_MINUS,
+ BC_INST_REL_EQ, BC_INST_REL_LE, BC_INST_REL_GE, BC_INST_REL_NE,
+ BC_INST_REL_LT, BC_INST_REL_GT,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_ASSIGN,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_ARRAY_ELEM, BC_INST_INVALID,
+ BC_INST_STR, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_IBASE,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_LENGTH, BC_INST_INVALID,
+ BC_INST_OBASE, BC_INST_PRINT, BC_INST_HALT, BC_INST_READ,
+ BC_INST_INVALID, BC_INST_SCALE, BC_INST_SQRT, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_POP,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_INVALID, BC_INST_PRINT,
+ BC_INST_INVALID, BC_INST_INVALID, BC_INST_SCALE_FUNC,
+};
#endif // DC_CONFIG
const char bc_num_hex_digits[] = "0123456789ABCDEF";
diff --git a/src/dc/parse.c b/src/dc/parse.c
index ee78484..1568910 100644
--- a/src/dc/parse.c
+++ b/src/dc/parse.c
@@ -27,9 +27,9 @@
#include <program.h>
#include <vm.h>
-BcStatus dc_parse_push(BcParse *p, BcVec *code, size_t n, BcInst inst) {
+BcStatus dc_parse_push(BcParse *p, BcVec *code, BcInst inst) {
BcStatus s;
- if (p->nbraces >= n) s = bc_vec_pushByte(code, inst);
+ if (p->nbraces >= bc_inst_operands[inst]) s = bc_vec_pushByte(code, inst);
else s = BC_STATUS_PARSE_BAD_EXP;
return s;
}
@@ -39,42 +39,31 @@
BcStatus s;
BcLexType t = p->lex.t.t;
BcInst prev;
+ BcInst inst;
(void) flags;
(void) next;
- for (; t != BC_LEX_EOF; t = p->lex.t.t){
+ for (; t != BC_LEX_EOF; t = p->lex.t.t) {
- switch (t) {
+ if ((inst = dc_parse_insts[t]) != BC_INST_INVALID) {
+ s = dc_parse_push(p, code, inst);
+ }
+ else {
- case BC_LEX_NUMBER:
- {
- s = bc_parse_number(p, code, &prev, &p->nbraces);
- break;
- }
+ switch (t) {
- case BC_LEX_PRINT_NEWLINE:
- {
- s = dc_parse_push(p, code, 1, BC_INST_PRINT);
- break;
- }
+ case BC_LEX_NUMBER:
+ {
+ s = bc_parse_number(p, code, &prev, &p->nbraces);
+ break;
+ }
- case BC_LEX_KEY_QUIT:
- {
- s = bc_vec_pushByte(code, BC_INST_HALT);
- break;
- }
-
- case BC_LEX_POP:
- {
- s = dc_parse_push(p, code, 1, BC_INST_POP);
- break;
- }
-
- default:
- {
- s = BC_STATUS_PARSE_BAD_TOKEN;
- break;
+ default:
+ {
+ s = BC_STATUS_PARSE_BAD_TOKEN;
+ break;
+ }
}
}