Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e46c6ff52d | |||
| 594e33efd6 | |||
| ec1a69f3dd |
@@ -1,9 +0,0 @@
|
|||||||
Copilot / contributor instructions for v0
|
|
||||||
|
|
||||||
- When adding new source or test files for v0, do NOT rely on wildcards. Add the file path explicitly to v0/include.mk in either V0_SRC (for library/source files) or V0_TEST (for test files beginning with `test_`).
|
|
||||||
- v0/include.mk is included by the top-level Makefile and is a dependency for object builds. Modifying v0/include.mk will force appropriate recompilation.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
V0_SRC := v0/buffer.c v0/main.c
|
|
||||||
V0_TEST := v0/test.c v0/test_buffer.c
|
|
||||||
|
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef PARSER_H
|
||||||
|
#define PARSER_H
|
||||||
|
|
||||||
|
#include "ast.h"
|
||||||
|
#include "token.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a stream of tokens into a module.
|
||||||
|
*
|
||||||
|
* @param ts The TokenStream to read.
|
||||||
|
* @returns The parsed module.
|
||||||
|
*/
|
||||||
|
Module* parser_parse(TokenStream* ts);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frees the parsed AST.
|
||||||
|
*
|
||||||
|
* @param module The AST return by parser_parse.
|
||||||
|
*/
|
||||||
|
void parser_free(Module* module);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "test.h"
|
#include "test.h"
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
static jmp_buf s_testJmp;
|
static jmp_buf s_testJmp;
|
||||||
static const char* s_failMsg;
|
static const char* s_failMsg;
|
||||||
@@ -10,6 +11,18 @@ void fail(const char* msg) {
|
|||||||
longjmp(s_testJmp, 1);
|
longjmp(s_testJmp, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void assert_not_null(void* ptr, const char* msg) {
|
||||||
|
if (ptr == NULL) {
|
||||||
|
fail(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void assert_str(const char* expected, const char* actual, const char* msg) {
|
||||||
|
if (expected == NULL || actual == NULL || strcmp(expected, actual) != 0) {
|
||||||
|
fail(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char* name;
|
const char* name;
|
||||||
Test func;
|
Test func;
|
||||||
|
|||||||
+10
-4
@@ -89,16 +89,22 @@ static void test_tokenstream_info(void) {
|
|||||||
tokenstream_info(ts, &info1);
|
tokenstream_info(ts, &info1);
|
||||||
if (t1 != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
if (t1 != TOKEN_MODULE) fail("expected TOKEN_MODULE");
|
||||||
if (info1.token != TOKEN_MODULE) fail("info: expected TOKEN_MODULE");
|
if (info1.token != TOKEN_MODULE) fail("info: expected TOKEN_MODULE");
|
||||||
if (info1.text_length != 6) fail("info: expected length 6");
|
|
||||||
if (strncmp(info1.text, "module", 6) != 0) fail("info: expected 'module'");
|
char buf1[32];
|
||||||
|
memcpy(buf1, info1.text, info1.text_length);
|
||||||
|
buf1[info1.text_length] = '\0';
|
||||||
|
assert_str("module", buf1, "info: expected 'module'");
|
||||||
|
|
||||||
Token t2 = tokenstream_next(ts);
|
Token t2 = tokenstream_next(ts);
|
||||||
TokenInfo info2;
|
TokenInfo info2;
|
||||||
tokenstream_info(ts, &info2);
|
tokenstream_info(ts, &info2);
|
||||||
if (t2 != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
|
if (t2 != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
|
||||||
if (info2.token != TOKEN_IDENTIFIER) fail("info: expected TOKEN_IDENTIFIER");
|
if (info2.token != TOKEN_IDENTIFIER) fail("info: expected TOKEN_IDENTIFIER");
|
||||||
if (info2.text_length != 4) fail("info: expected length 4");
|
|
||||||
if (strncmp(info2.text, "main", 4) != 0) fail("info: expected 'main'");
|
char buf2[32];
|
||||||
|
memcpy(buf2, info2.text, info2.text_length);
|
||||||
|
buf2[info2.text_length] = '\0';
|
||||||
|
assert_str("main", buf2, "info: expected 'main'");
|
||||||
|
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user