all repos — nand2tetris @ 6247187186509c07904eebced94f63dc08def9c7

my nand2tetris progress

Misc. clean-up of A/C-type parse structure
x1phosura x1phosura@x1phosura.zone
Fri, 25 Nov 2022 19:20:43 -0800
commit

6247187186509c07904eebced94f63dc08def9c7

parent

1a738e55ade7983f9c5b158ff325c19d5f5c7672

1 files changed, 17 insertions(+), 18 deletions(-)

jump to
M projects/06/assembler1/assembler1.cprojects/06/assembler1/assembler1.c

@@ -19,24 +19,27 @@ {

fprintf(stderr, "%s", err_msg); } -/* -uint16_t parse_a_type(const char *line) +bool parse_a_type(const char *line, uint16_t *instruction) { + DEBUG("parse_a_type()\n"); // eventually, if error, die("error: TODO explain...\n", -1); - DEBUG("parse_a_type()\n"); - return 0x0000; // STUB, A-type MSB == 0 anyway + *instruction = 0x0000; // STUB, A-type MSB == 0 anyway + return true; // STUB, A-type MSB == 0 anyway } -uint16_t parse_c_type(const char *line) +bool parse_c_type(const char *line, uint16_t *instruction) { DEBUG("parse_c_type()\n"); - return 0x8000; // STUB, C-type MSB == 1 anyway + *instruction = 0x8000; // STUB, C-type MSB == 1 anyway + return true; // STUB, C-type MSB == 1 anyway } +// */ // does not care about line line length; exits at first newline or after // relevant portion parsed (allows for syntactically-incorrect lines, I know) -uint16_t parse_instruction_w_type(const char *line, uint16_t *instruction) +bool parse_instruction_w_type(const char *line, uint16_t *instruction) { + bool ret; char c; size_t i = 0;

@@ -44,10 +47,10 @@ while ((c = line[i]) != '\n') {

if (c == ' ' || c == '\t') ; // skip whitespace else if (c == '@') { - instruction = parse_a_type(line); + ret = parse_a_type(line, instruction); break; } else if (c >= '!' && c < '~') { - instruction = parse_c_type(line); + ret = parse_c_type(line, instruction); break; } else { die("error: TODO explain...\n", -1);

@@ -56,14 +59,14 @@

++i; } - ld.line_parsed = true; - return ld; + return ret; } -*/ +// */ bool parse_next_instruction(const char *line, uint16_t *instruction) { *instruction = 1337; // STUB, should be 0x539, 0b0000010100111001 + return parse_instruction_w_type(line, instruction); return true; }

@@ -116,7 +119,7 @@ "@98\n",

"// this is another comment\n", "/ this is a broken comment\n", "D=M;JNE\n"}; -size_t test_lines_len = 8; // TODO calculate +size_t num_test_lines = (sizeof(test_lines) / sizeof(test_lines[0])); int main() {

@@ -129,8 +132,7 @@ // New organization allows code to eventually work with lines fetched

// from a 'fgets()' loop or something // for testing - for (size_t i = 0; i < test_lines_len; ++i) { - printf("testing %lu: line to assemble: %s", i, test_lines[i]); + for (size_t i = 0; i < num_test_lines; ++i) { test_line_len = strlen(test_lines[i]); result = parse_line(test_lines[i], test_line_len, &instruction); if (result) {

@@ -138,9 +140,6 @@ printf("instruction: 0x%x | ", instruction);

bindump_word16(instruction); // output instruction as binary putchar('\n'); } - /* else { - printf("\tcomment or invalid syntax encountered\n"); - }*/ } return 0;