Remove semicolons and use \ instead of $

This commit is contained in:
2025-08-03 02:18:20 +02:00
parent f0be8a1bd5
commit 6d98b4eee5
9 changed files with 61 additions and 40 deletions
+12 -14
View File
@@ -1,5 +1,4 @@
title = "My Website"
title = "My Website";
posts = [ posts = [
{ {
@@ -18,29 +17,28 @@ posts = [
{ author: "User 2", date: "2 Jan 2025", content: "Second comment" }, { author: "User 2", date: "2 Jan 2025", content: "Second comment" },
] ]
}, },
]; ]
fun render_comment(c) fun render_comment(c)
<div class="comment"> <div class="comment">
<a>$c.author; ($c.date;)</a> <a>\{c.author} (\c.date)</a>
<p>$c.content;</p> <p>\c.content</p>
<div class="comment-children"> <div class="comment-children">
$ for child in c.children: \for child in c.children:
render_comment(child); render_comment(child)
</div> </div>
</div> </div>
export <html> <html>
<head> <head>
<title>$title;</title> <title>\title</title>
</head> </head>
<body> <body>
$ for post in posts: { \for post in posts:
<div class="post"> <div class="post">
<a>$post.title; ($post.date;)</a> <a>\{post.title} (\post.date)</a>
$ for comment in post.comments: \for comment in post.comments:
render_comment(comment); render_comment(comment)
</div> </div>
}
</body> </body>
</html> </html>
+2
View File
@@ -3,8 +3,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifndef WL_AMALGAMATION
#include "parse.h" #include "parse.h"
#include "assemble.h" #include "assemble.h"
#endif
#define MAX_SCOPES 32 #define MAX_SCOPES 32
#define MAX_VARS 1024 #define MAX_VARS 1024
+2
View File
@@ -1,7 +1,9 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#ifndef WL_AMALGAMATION
#include "basic.h" #include "basic.h"
#endif
b32 is_space(char c) b32 is_space(char c)
{ {
+3
View File
@@ -1,4 +1,7 @@
#ifndef WL_AMALGAMATION
#include "eval.h" #include "eval.h"
#endif
#define FRAME_LIMIT 128 #define FRAME_LIMIT 128
#define EVAL_STACK_LIMIT 128 #define EVAL_STACK_LIMIT 128
+2
View File
@@ -9,7 +9,9 @@
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
#ifndef WL_AMALGAMATION
#include "file.h" #include "file.h"
#endif
int file_open(String path, File *handle, int *size) int file_open(String path, File *handle, int *size)
{ {
+2
View File
@@ -1,7 +1,9 @@
#ifndef WL_FILE_INCLUDED #ifndef WL_FILE_INCLUDED
#define WL_FILE_INCLUDED #define WL_FILE_INCLUDED
#ifndef WL_AMALGAMATION
#include "basic.h" #include "basic.h"
#endif
#if _WIN32 #if _WIN32
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
+3 -2
View File
@@ -14,8 +14,8 @@ int main(void)
} }
char err[1<<9]; char err[1<<9];
char mem[1<<14]; char *mem = malloc(1<<20);
Arena a = { mem, COUNT(mem), 0 }; Arena a = { mem, 1<<20, 0 };
ParseResult result = parse(src, &a, err, COUNT(err)); ParseResult result = parse(src, &a, err, COUNT(err));
if (result.node == NULL) { if (result.node == NULL) {
@@ -27,5 +27,6 @@ int main(void)
fflush(stdout); fflush(stdout);
free(src.ptr); free(src.ptr);
free(mem);
return 0; return 0;
} }
+32 -24
View File
@@ -2,7 +2,9 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#ifndef WL_AMALGAMATION
#include "parse.h" #include "parse.h"
#endif
typedef struct { typedef struct {
char *src; char *src;
@@ -43,7 +45,7 @@ typedef enum {
TOKEN_DOT, TOKEN_DOT,
TOKEN_COMMA, TOKEN_COMMA,
TOKEN_COLON, TOKEN_COLON,
TOKEN_SEMICOLON, TOKEN_NEWLINE,
} TokType; } TokType;
typedef struct { typedef struct {
@@ -149,7 +151,8 @@ String tok2str(Token token, char *buf, int max)
case TOKEN_DOT: return S("."); case TOKEN_DOT: return S(".");
case TOKEN_COMMA: return S(","); case TOKEN_COMMA: return S(",");
case TOKEN_COLON: return S(":"); case TOKEN_COLON: return S(":");
case TOKEN_SEMICOLON: return S(";");
case TOKEN_NEWLINE: return S("\\n");
} }
return S("???"); return S("???");
@@ -378,12 +381,25 @@ Token next_token(Parser *p)
if (consume_str(&p->s, S("."))) return (Token) { .type=TOKEN_DOT }; if (consume_str(&p->s, S("."))) return (Token) { .type=TOKEN_DOT };
if (consume_str(&p->s, S(","))) return (Token) { .type=TOKEN_COMMA }; if (consume_str(&p->s, S(","))) return (Token) { .type=TOKEN_COMMA };
if (consume_str(&p->s, S(":"))) return (Token) { .type=TOKEN_COLON }; if (consume_str(&p->s, S(":"))) return (Token) { .type=TOKEN_COLON };
if (consume_str(&p->s, S(";"))) return (Token) { .type=TOKEN_SEMICOLON };
report(p, "Invalid character '%c'", c); report(p, "Invalid character '%c'", c);
return (Token) { .type=TOKEN_ERROR }; return (Token) { .type=TOKEN_ERROR };
} }
Token next_token_or_newline(Parser *p)
{
int peek = p->s.cur;
while (peek < p->s.len && is_space(p->s.src[peek]) && p->s.src[peek] != '\n')
peek++;
if (peek < p->s.len && p->s.src[peek] == '\n') {
p->s.cur = peek+1;
return (Token) { .type=TOKEN_NEWLINE };
}
return next_token(p);
}
enum { enum {
IGNORE_GRT = 1 << 0, IGNORE_GRT = 1 << 0,
IGNORE_LSS = 1 << 1, IGNORE_LSS = 1 << 1,
@@ -398,7 +414,9 @@ Node *parse_html(Parser *p)
Token t = next_token(p); Token t = next_token(p);
if (t.type != TOKEN_IDENT) { if (t.type != TOKEN_IDENT) {
report(p, "HTML tag doesn't start with a name"); char buf[1<<8];
String ts = tok2str(t, buf, COUNT(buf));
report(p, "HTML tag doesn't start with a name (got '%.*s' instead)", ts.len, ts.ptr);
return NULL; return NULL;
} }
String tagname = t.sval; String tagname = t.sval;
@@ -455,7 +473,7 @@ Node *parse_html(Parser *p)
for (;;) { for (;;) {
int off = p->s.cur; int off = p->s.cur;
while (p->s.cur < p->s.len && p->s.src[p->s.cur] != '<' && p->s.src[p->s.cur] != '$') while (p->s.cur < p->s.len && p->s.src[p->s.cur] != '<' && p->s.src[p->s.cur] != '\\')
p->s.cur++; p->s.cur++;
if (p->s.cur > off) { if (p->s.cur > off) {
@@ -474,7 +492,7 @@ Node *parse_html(Parser *p)
if (p->s.cur == p->s.len || p->s.src[p->s.cur] == '<') if (p->s.cur == p->s.len || p->s.src[p->s.cur] == '<')
break; break;
p->s.cur++; // Consume $ p->s.cur++; // Consume \
{ {
Node *child = parse_stmt(p, IGNORE_LSS); Node *child = parse_stmt(p, IGNORE_LSS);
@@ -950,7 +968,7 @@ Node *parse_expr_inner(Parser *p, Node *left, int min_prec, int flags)
for (;;) { for (;;) {
Scanner saved = p->s; Scanner saved = p->s;
Token t1 = next_token(p); Token t1 = next_token_or_newline(p);
if (precedence(t1, flags) < min_prec) { if (precedence(t1, flags) < min_prec) {
p->s = saved; p->s = saved;
break; break;
@@ -963,7 +981,7 @@ Node *parse_expr_inner(Parser *p, Node *left, int min_prec, int flags)
for (;;) { for (;;) {
saved = p->s; saved = p->s;
Token t2 = next_token(p); Token t2 = next_token_or_newline(p);
int p1 = precedence(t1, flags); int p1 = precedence(t1, flags);
int p2 = precedence(t2, flags); int p2 = precedence(t2, flags);
p->s = saved; p->s = saved;
@@ -1023,14 +1041,6 @@ Node *parse_expr_stmt(Parser *p, int opflags)
if (e == NULL) if (e == NULL)
return NULL; return NULL;
if (e->type != NODE_VALUE_HTML) {
Token t = next_token(p);
if (t.type != TOKEN_SEMICOLON) {
report(p, "Missing ';' after expression");
return NULL;
}
}
return e; return e;
} }
@@ -1317,21 +1327,19 @@ Node *parse_var_decl(Parser *p, int opflags)
} }
String name = t.sval; String name = t.sval;
Scanner saved = p->s;
t = next_token(p); t = next_token(p);
Node *value = NULL; Node *value;
if (t.type == TOKEN_OPER_ASS) { if (t.type == TOKEN_OPER_ASS) {
value = parse_expr(p, opflags); value = parse_expr(p, opflags);
if (value == NULL) if (value == NULL)
return NULL; return NULL;
t = next_token(p); } else {
} p->s = saved;
value = NULL;
if (t.type != TOKEN_SEMICOLON) {
report(p, "Missing ';' at the end of a variable declaration");
return NULL;
} }
Node *parent = alloc_node(p); Node *parent = alloc_node(p);
@@ -1630,8 +1638,8 @@ void print_node(Node *node)
node->func_name.ptr); node->func_name.ptr);
Node *arg = node->func_args; Node *arg = node->func_args;
while (arg) { while (arg) {
arg = arg->next;
print_node(arg); print_node(arg);
arg = arg->next;
if (arg) if (arg)
printf(", "); printf(", ");
} }
+3
View File
@@ -2,7 +2,10 @@
#define WL_PARSE_INCLUDED #define WL_PARSE_INCLUDED
#include <stdint.h> #include <stdint.h>
#ifndef WL_AMALGAMATION
#include "basic.h" #include "basic.h"
#endif
typedef enum { typedef enum {
NODE_FUNC_DECL, NODE_FUNC_DECL,