all repos — nand2tetris @ 044076b7d21c865c7c6a63e8339be2a6067bd3fe

my nand2tetris progress

projects/07/src/util.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
#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

extern char  *file_line;     // reference to currently-read line (for convenience)
extern size_t file_line_no;  // line number, regardless of line content

#define err(...) (fprintf(stdout, __VA_ARGS__), \
                  fprintf(stdout, "%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