General fixes for cWEB and updated WL

This commit is contained in:
2025-10-16 15:48:49 +02:00
parent 0da40c6659
commit 04fcad87f0
12 changed files with 500 additions and 243 deletions
+11 -6
View File
@@ -761,14 +761,19 @@ static Node *parse_html(Parser *p)
bool no_body = false;
Scanner *s = &p->s;
for (;;) {
for (int quote = 0;;) {
int off = s->cur;
bool quotes = false;
while (s->cur < s->len && s->src[s->cur] != '\\' && (quotes || (s->src[s->cur] != '/' && s->src[s->cur] != '>'))) {
if (s->src[s->cur] == '"')
quotes = !quotes;
while (s->cur < s->len && s->src[s->cur] != '\\' && (quote || (s->src[s->cur] != '/' && s->src[s->cur] != '>'))) {
if (quote) {
if (quote == s->src[s->cur])
quote = 0;
} else {
char c = s->src[s->cur];
if (c == '"' || c == '\'')
quote = c;
}
s->cur++;
}
@@ -1432,7 +1437,7 @@ static Node *parse_expr_inner(Parser *p, Node *left, int min_prec, int flags)
Token t1 = next_token_or_newline(p);
if (precedence(t1, flags) < min_prec) {
p->s = saved;
break;
break;
}
Node *right = parse_atom(p);
+86 -11
View File
@@ -1,6 +1,8 @@
#include <stdint.h>
#include <stdbool.h>
#define WL_STR(X) ((WL_String) { (X), (int) sizeof(X)-1 })
typedef struct WL_Runtime WL_Runtime;
typedef struct WL_Compiler WL_Compiler;
@@ -45,17 +47,90 @@ typedef struct {
WL_String str;
} WL_EvalResult;
WL_Compiler* wl_compiler_init (WL_Arena *arena);
WL_AddResult wl_compiler_add (WL_Compiler *compiler, WL_String path, WL_String content);
int wl_compiler_link (WL_Compiler *compiler, WL_Program *program);
WL_String wl_compiler_error (WL_Compiler *compiler);
int wl_dump_ast (WL_Compiler *compiler, char *dst, int cap);
void wl_dump_program (WL_Program program);
// Creates a compilation unit for a program
// The provided arena (which can't be NULL) is
// used for all memory allocations until a
// program is produced or an error occurs.
// If not enough memory is provided, NULL is
// returned.
WL_Compiler *wl_compiler_init(WL_Arena *arena);
WL_Runtime* wl_runtime_init (WL_Arena *arena, WL_Program program);
WL_EvalResult wl_runtime_eval (WL_Runtime *rt);
WL_String wl_runtime_error (WL_Runtime *rt);
void wl_runtime_dump (WL_Runtime *rt);
// Adds a file to the current compilation unit
// and returns
//
// WL_ADD_ERROR if an error occurred
//
// WL_ADD_AGAIN if the file included a different
// file that also needs to be added to the unit,
// in which case the file name is in the "path"
// field of the return value.
//
// WL_ADD_LINK all sources were processed and
// the unit is ready for linking
//
// Note that the source of all files needs to
// stay alive until the program is linked as the
// compiler may keep references to it until then.
WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String path, WL_String content);
// Links a compilation unit producing an executable.
// The output program is just an array of bytes
// allocated using the compiler's arena memory and
// may be written to disk or any other external system
// for caching.
//
// On error, -1 is returned and the error text can
// be retrieved using wl_compiler_error. On success,
// 0 is returned.
int wl_compiler_link(WL_Compiler *compiler, WL_Program *program);
// Returns the null-terminated error string for a
// compilation unit that failed.
WL_String wl_compiler_error(WL_Compiler *compiler);
// Serializes the AST of the source parsed in this
// compilation unit by writing the string to the
// rovided buffer and returning the number of bytes
// written to it.
//
// If the provided buffer is too small, the function
// fills it up and returns the number of bytes that
// would have been written if the buffer was large
// enough. On error, -1 is returned.
int wl_dump_ast(WL_Compiler *compiler, char *dst, int cap);
// Writes the bytecode of a program to stdout as a
// human-readable string.
void wl_dump_program(WL_Program program);
// Creates an evaluation context for a bytecode program
// All memory used while running the program will be
// allocated from the provided arena.
//
// If not enough memory was provided or the program is
// invalid, NULL is returned.
WL_Runtime *wl_runtime_init(WL_Arena *arena, WL_Program program);
// Run the program associated to this runtime until an
// event happens. The event may be one of:
//
// WL_EVAL_DONE if execution is complete
//
// WL_EVAL_ERROR if execution failed
//
// WL_EVAL_OUTPUT if data is available for output,
// in which case the field "str" points to it
//
// WL_EVAL_SYSVAR if the program requested the value
// of an external symbol.
//
// WL_EVAL_SYSCALL
//
WL_EvalResult wl_runtime_eval(WL_Runtime *rt);
WL_String wl_runtime_error(WL_Runtime *rt);
void wl_runtime_dump(WL_Runtime *rt);
bool wl_streq (WL_String a, char *b, int blen);
int wl_arg_count (WL_Runtime *rt);
@@ -87,4 +162,4 @@ void wl_push_array (WL_Runtime *rt, int cap);
void wl_push_map (WL_Runtime *rt, int cap);
void wl_push_arg (WL_Runtime *rt, int idx);
void wl_insert (WL_Runtime *rt);
void wl_append (WL_Runtime *rt);
void wl_append (WL_Runtime *rt);