Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cosine and pow. Examples folder #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/grammar.bnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Entry
E | vec3(C, C, C)
;

# X, Y and T is biased so we displaced it by a random value
X | mult(add(x, random), random);
Y | mult(add(y, random), random);
T | mult(add(t, random), random);
ST | sin(T);
CT | cos(T);

# Terminal
A || random
|| x
|| y
||| X
||| Y
||| CT
||| ST
;

# Expressions
C |||| add(C, C)
|||| mult(C, C)
|| A
| add(mult(A, A), mult(A, A))
;
87 changes: 80 additions & 7 deletions src/randomart.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ typedef enum {
NK_SQRT,
NK_ABS,
NK_SIN,
NK_COS,
NK_POW,
NK_ADD,
NK_MULT,
NK_MOD,
Expand All @@ -40,7 +42,7 @@ typedef enum {
COUNT_NK,
} Node_Kind;

static_assert(COUNT_NK == 16, "Amount of nodes have changed");
static_assert(COUNT_NK == 18, "Amount of nodes have changed");
const char *nk_names[COUNT_NK] = {
[NK_X] = "x",
[NK_Y] = "y",
Expand All @@ -49,8 +51,10 @@ const char *nk_names[COUNT_NK] = {
[NK_RANDOM] = "random",
[NK_NUMBER] = "number",
[NK_SQRT] = "sqrt",
[NK_SIN] = "sin",
[NK_SIN] = "sin",
[NK_COS] = "cos",
[NK_ABS] = "abs",
[NK_POW] = "pow",
[NK_ADD] = "add",
[NK_MULT] = "mult",
[NK_MOD] = "mod",
Expand Down Expand Up @@ -191,6 +195,13 @@ void node_print(Node *node)
case NK_NUMBER:
printf("%f", node->as.number);
break;
case NK_POW:
printf("pow(");
node_print(node->as.binop.lhs);
printf(", ");
node_print(node->as.binop.rhs);
printf(")");
break;
case NK_ADD:
printf("add(");
node_print(node->as.binop.lhs);
Expand Down Expand Up @@ -249,6 +260,11 @@ void node_print(Node *node)
node_print(node->as.unop);
printf(")");
break;
case NK_COS:
printf("cos(");
node_print(node->as.unop);
printf(")");
break;
case NK_ABS:
printf("abs(");
node_print(node->as.unop);
Expand Down Expand Up @@ -324,6 +340,12 @@ Node *eval(Node *expr, float x, float y, float t)
if (!expect_number(rhs)) return NULL;
return node_number_loc(expr->file, expr->line, sqrtf(rhs->as.number));
}
case NK_COS: {
Node *rhs = eval(expr->as.unop, x, y, t);
if (!rhs) return NULL;
if (!expect_number(rhs)) return NULL;
return node_number_loc(expr->file, expr->line, cosf(rhs->as.number));
}
case NK_SIN: {
Node *rhs = eval(expr->as.unop, x, y, t);
if (!rhs) return NULL;
Expand All @@ -336,6 +358,15 @@ Node *eval(Node *expr, float x, float y, float t)
if (!expect_number(rhs)) return NULL;
return node_number_loc(expr->file, expr->line, fabsf(rhs->as.number));
}
case NK_POW: {
Node *lhs = eval(expr->as.binop.lhs, x, y, t);
if (!lhs) return NULL;
if (!expect_number(lhs)) return NULL;
Node *rhs = eval(expr->as.binop.rhs, x, y, t);
if (!rhs) return NULL;
if (!expect_number(rhs)) return NULL;
return node_number_loc(expr->file, expr->line, powf(lhs->as.number, rhs->as.number));
}
case NK_ADD: {
Node *lhs = eval(expr->as.binop.lhs, x, y, t);
if (!lhs) return NULL;
Expand Down Expand Up @@ -502,6 +533,11 @@ Node *gen_node(Grammar grammar, Node *node, int depth)
if (!rhs) return NULL;
return node_unop_loc(node->file, node->line, node->kind, rhs);
}
case NK_COS: {
Node *rhs = gen_node(grammar, node->as.unop, depth);
if (!rhs) return NULL;
return node_unop_loc(node->file, node->line, node->kind, rhs);
}
case NK_SIN: {
Node *rhs = gen_node(grammar, node->as.unop, depth);
if (!rhs) return NULL;
Expand All @@ -513,6 +549,7 @@ Node *gen_node(Grammar grammar, Node *node, int depth)
return node_unop_loc(node->file, node->line, node->kind, rhs);
}

case NK_POW:
case NK_ADD:
case NK_MULT:
case NK_MOD:
Expand Down Expand Up @@ -699,6 +736,11 @@ bool compile_node_into_fragment_expression(String_Builder *sb, Node *expr, size_
if (!compile_node_into_fragment_expression(sb, expr->as.unop, level + 1)) return false;
sb_append_cstr(sb, ")");
break;
case NK_COS:
sb_append_cstr(sb, "cos(");
if (!compile_node_into_fragment_expression(sb, expr->as.unop, level + 1)) return false;
sb_append_cstr(sb, ")");
break;
case NK_SIN:
sb_append_cstr(sb, "sin(");
if (!compile_node_into_fragment_expression(sb, expr->as.unop, level + 1)) return false;
Expand All @@ -711,6 +753,14 @@ bool compile_node_into_fragment_expression(String_Builder *sb, Node *expr, size_
sb_append_cstr(sb, ")");
break;

case NK_POW:
sb_append_cstr(sb, "pow(");
if (!compile_node_into_fragment_expression(sb, expr->as.binop.lhs, level + 1)) return false;
sb_append_cstr(sb, ",");
if (!compile_node_into_fragment_expression(sb, expr->as.binop.rhs, level + 1)) return false;
sb_append_cstr(sb, ")");
break;

case NK_ADD:
sb_append_cstr(sb, "(");
if (!compile_node_into_fragment_expression(sb, expr->as.binop.lhs, level + 1)) return false;
Expand Down Expand Up @@ -782,7 +832,7 @@ bool compile_node_func_into_fragment_shader(String_Builder *sb, Node *f)
sb_append_cstr(sb, "{\n");
sb_append_cstr(sb, " float x = fragTexCoord.x*2.0 - 1.0;\n");
sb_append_cstr(sb, " float y = fragTexCoord.y*2.0 - 1.0;\n");
sb_append_cstr(sb, " float t = sin(time);\n");
sb_append_cstr(sb, " float t = time;\n");
sb_append_cstr(sb, " finalColor = map_color(");
if (!compile_node_into_fragment_expression(sb, f, 0)) return false;
sb_append_cstr(sb, ");\n");
Expand Down Expand Up @@ -871,8 +921,17 @@ bool parse_node(Alexer *l, Node **node)
{
Alexer_Token t = {0};
alexer_get_token(l, &t);
if (!alexer_expect_id(l, t, ALEXER_SYMBOL)) return false;
if (alexer_token_text_equal_cstr(t, "random")) {
uint64_t ids[] = {
ALEXER_ID(ALEXER_INT, 0),
ALEXER_ID(ALEXER_SYMBOL, 0),
};
if (!alexer_expect_one_of_ids(l, t, ids, ARRAY_LEN(ids))) {
return false;
}
else if (ALEXER_KIND(t.id) == ALEXER_INT) {
*node = node_number_loc(t.loc.file_path, t.loc.row, t.int_value);
}
else if (alexer_token_text_equal_cstr(t, "random")) {
*node = node_loc(t.loc.file_path, t.loc.row, NK_RANDOM);
} else if (alexer_token_text_equal_cstr(t, "x")) {
*node = node_loc(t.loc.file_path, t.loc.row, NK_X);
Expand All @@ -888,6 +947,16 @@ bool parse_node(Alexer *l, Node **node)
if (!parse_node(l, &unop)) return false;
*node = node_unop_loc(t.loc.file_path, t.loc.row, NK_SQRT, unop);

alexer_get_token(l, &t);
if (!alexer_expect_id(l, t, ALEXER_ID(ALEXER_PUNCT, PUNCT_CPAREN))) return false;
} else if (alexer_token_text_equal_cstr(t, "cos")) {
alexer_get_token(l, &t);
if (!alexer_expect_id(l, t, ALEXER_ID(ALEXER_PUNCT, PUNCT_OPAREN))) return false;

Node *unop;
if (!parse_node(l, &unop)) return false;
*node = node_unop_loc(t.loc.file_path, t.loc.row, NK_COS, unop);

alexer_get_token(l, &t);
if (!alexer_expect_id(l, t, ALEXER_ID(ALEXER_PUNCT, PUNCT_CPAREN))) return false;
} else if (alexer_token_text_equal_cstr(t, "sin")) {
Expand All @@ -910,6 +979,10 @@ bool parse_node(Alexer *l, Node **node)

alexer_get_token(l, &t);
if (!alexer_expect_id(l, t, ALEXER_ID(ALEXER_PUNCT, PUNCT_CPAREN))) return false;
} else if (alexer_token_text_equal_cstr(t, "pow")) {
Node *lhs, *rhs;
if (!parse_pair(l, &lhs, &rhs)) return false;
*node = node_binop_loc(t.loc.file_path, t.loc.row, NK_POW, lhs, rhs);
} else if (alexer_token_text_equal_cstr(t, "add")) {
Node *lhs, *rhs;
if (!parse_pair(l, &lhs, &rhs)) return false;
Expand Down Expand Up @@ -1002,8 +1075,8 @@ int main(int argc, char **argv)

int depth = 30;
int seed = time(0);
int width = 16*100;
int height = 9*100;
int width = 16*80;
int height = 9*80;
int fps = 60;

while (argc > 0) {
Expand Down