all repos — nand2tetris @ 27dad78bd494934f751c0db6ecce7d304945e18d

my nand2tetris progress

Reorganize base code, add util.h
x1phosura x1phosura@x1phosura.zone
Mon, 08 May 2023 02:31:42 -0700
commit

27dad78bd494934f751c0db6ecce7d304945e18d

parent

6b59b2d1aa8d1c2c01d77b7a7d383be5fcfb4adc

M projects/07/src/codewriter.hprojects/07/src/codewriter.h

@@ -1,7 +1,7 @@

#ifndef _CODEWRITER_H #define _CODEWRITER_H -// 'parser.h' roughly corresponds to the 'Parser' module specified in +// 'codewriter.h' roughly corresponds to the 'CodeWriter' module specified in // nand2tetris, with a few liberties taken. #include <stdbool.h>

@@ -11,10 +11,9 @@ #include <stdlib.h>

#include <string.h> #include "parser.h" +#include "util.h" -#define DBGLOG printf -//#define DBGLOG(...) -#define MAX_LINE_LEN 256 // TODO need to be redefined here? (remove me) +#define _DEBUG bool write_instruction(struct vm_instruction_t *vm_instr, FILE *fp)
M projects/07/src/parser.hprojects/07/src/parser.h

@@ -10,9 +10,10 @@ #include <stdio.h>

#include <stdlib.h> #include <string.h> -#define DBGLOG printf -//#define DBGLOG(...) -#define MAX_LINE_LEN 256 // TODO need to be redefined here? (remove me) +#include "util.h" + +#define _DEBUG + enum vm_command_t { C_ARITHMETIC,
A projects/07/src/util.h

@@ -0,0 +1,40 @@

+#ifndef _UTIL_H +#define _UTIL_H + +// Contains shared constants/macros and miscellaneous useful functions. + +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define _DEBUG // TODO: find way to remove this/restructure so ifdef only in + // this file + +#ifdef _DEBUG +#define DBGLOG printf +#else +#define DBGLOG(...) +#endif + +#define err(...) (fprintf(stderr, __VA_ARGS__), \ + fprintf(stderr, "%lu | %s\n", file_line_no, file_line), false) +#define die(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while (0) +#define is_symbol_char(c) (('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') \ + || ('a' <= c && c <= 'z') || c == '_' || c == '.' \ + || c == '$' || c == ':') +#define is_whitespace(c) ((c == ' ' || c == '\t' || c == '\n' || c == '\r')) +#define is_number(c) (('0' <= c && c <= '9')) + +#define MAX_LINE_LEN 256 + + +size_t skip_whitespace(const char *line, size_t n) +{ + size_t i; + for (i = 0; is_whitespace(line[i]) && i < n; ++i); + return i; +} + +#endif // _UTIL_H
M projects/07/src/vmtranslator.cprojects/07/src/vmtranslator.c

@@ -4,40 +4,15 @@ #include <stdio.h>

#include <stdlib.h> #include <string.h> +#include "codewriter.h" #include "parser.h" -#include "codewriter.h" +#include "util.h" #define _DEBUG -#ifdef _DEBUG -#define DBGLOG printf -#else -#define DBGLOG(...) -#endif - -#define err(...) (fprintf(stderr, __VA_ARGS__), \ - fprintf(stderr, "%lu | %s\n", file_line_no, file_line), false) -#define die(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while (0) - -#define is_symbol_char(c) (('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') \ - || ('a' <= c && c <= 'z') || c == '_' || c == '.' \ - || c == '$' || c == ':') -#define is_whitespace(c) ((c == ' ' || c == '\t' || c == '\n' || c == '\r')) -#define is_number(c) (('0' <= c && c <= '9')) - -#define MAX_LINE_LEN 256 // TODO: in/excludes NULL terminator? -#define MAX_SYMBOL_LEN MAX_LINE_LEN - 2 + 1 // + 1 for NULL terminator - char *file_line; // reference to currently-read line (for convenience) size_t file_line_no; // line number, regardless of line content - -static size_t skip_whitespace(const char *line, size_t n) -{ - size_t i; - for (i = 0; is_whitespace(line[i]) && i < n; ++i); - return i; -} bool translate(FILE *in_file) {

@@ -67,7 +42,6 @@ if (line_len > 1) {

if (line[0] == '/' && line[1] == '/') { // if comment continue; } else { - // parser will error specifically (TODO remove me) if (!parse_line(line, &vm_instr)) return false; if (!write_instruction(&vm_instr, in_file))