diff --git a/simple/bytebeat/example.sh b/simple/bytebeat/example.sh index 4ebce02..ab4da58 100755 --- a/simple/bytebeat/example.sh +++ b/simple/bytebeat/example.sh @@ -1,3 +1,5 @@ +#./bytebeat 0 'sin(t*0.5)*sin(t*0.0005)*127+128' 'sin(t*0.5)*cos(t*0.0005)*127+128' | aplay -c2 #./bytebeat 0 '10*(t>>6|t|t>>(t>>16))+(7&t>>11)' | aplay -./bytebeat 0 '(t<<3)*[8/9,1,9/8,6/5,4/3,3/2,0][[0xd2d2c8,0xce4088,0xca32c8,0x8e4009][t>>14&3]>>(0x3dbe4688>>((t>>10&15)>9?18:t>>10&15)*3&7)*3&7]' | aplay +#./bytebeat 0 '(t<<3)*[8/9,1,9/8,6/5,4/3,3/2,0][[0xd2d2c8,0xce4088,0xca32c8,0x8e4009][t>>14&3]>>(0x3dbe4688>>((t>>10&15)>9?18:t>>10&15)*3&7)*3&7]' | aplay +./bytebeat 0 '((t<<3)*[8/9,1,9/8,6/5,4/3,3/2,0][[0xd2d2c8,0xce4088,0xca32c8,0x8e4009][t>>14&3]>>(0x3dbe4688>>((t>>10&15)>9?18:t>>10&15)*3&7)*3&7])%255*.25*(1+sin(t*.0001))+(10*(t>>6|t|t>>(t>>16))+(7&t>>11))%255*.25*(1-sin(t*.0001))' | aplay diff --git a/simple/bytebeat/funcs.c b/simple/bytebeat/funcs.c index 7f74dc1..f69ca2b 100644 --- a/simple/bytebeat/funcs.c +++ b/simple/bytebeat/funcs.c @@ -9,13 +9,13 @@ typedef enum { typedef enum { M_VALUE = 0, + M_FUNC, + M_FUNC2, M_ARRAY, M_INDEX, M_PREFIX, M_BINARY, M_TERNARY, - M_FUNC, - M_FUNC2, } Mode; typedef struct { @@ -56,9 +56,9 @@ static inline int isflt2(Var a, Var b) { return isflt(a) || isflt(b); } static inline U32 asint(Var v) - { return isflt(v) ? (U32)v.f : v.u; } + { return isflt(v) ? (U32)(I32)v.f : v.u; } static inline F32 asflt(Var v) - { return isflt(v) ? v.f : (F32)v.u; } + { return isflt(v) ? v.f : (F32)(I32)v.u; } @@ -124,11 +124,18 @@ Var f_boolor(Params p) Var f_cond(Params p) { return isflt2(p.b, p.c) ? (p.a.u ? varf(asflt(p.b)) : varf(asflt(p.c))) : (p.a.u ? vari(asint(p.b)) : vari(asint(p.c))); } +Var f_sin(Params p) + { return varf(sinf(asflt(p.a))); } +Var f_cos(Params p) + { return varf(cosf(asflt(p.a))); } + OpDesc ops[] = { { M_VALUE, 0, "" }, { M_VALUE, 0, "t", f_var }, + { M_FUNC, 0, "sin", f_sin }, + { M_FUNC, 0, "cos", f_cos }, { M_ARRAY, 0, "[" }, { M_INDEX, 1, "[" }, diff --git a/simple/bytebeat/lexer.c b/simple/bytebeat/lexer.c index 008668a..866d92c 100644 --- a/simple/bytebeat/lexer.c +++ b/simple/bytebeat/lexer.c @@ -8,6 +8,7 @@ +typedef int I32; typedef unsigned int U32; typedef float F32; diff --git a/simple/bytebeat/main.c b/simple/bytebeat/main.c index b8dc007..685f098 100644 --- a/simple/bytebeat/main.c +++ b/simple/bytebeat/main.c @@ -8,8 +8,10 @@ void run(U32 count, const char *code) { //lexChainPrint(l, stderr); fprintf(stderr, "\n"); Op *op = opParse(l); //opPrint(op, stderr, 0); - for(U32 t = 0; !count || t < count; ++t) + for(U32 t = 0; !count || t < count; ++t) { fputc(asint(opCall(op, t)) & 0xff, stdout); + //printf("t%d %d\n", t, asint(opCall(op, t)) & 0xff); + } opDestroy(op); free(l); } diff --git a/simple/bytebeat/parser.c b/simple/bytebeat/parser.c index 1099a15..f23988e 100644 --- a/simple/bytebeat/parser.c +++ b/simple/bytebeat/parser.c @@ -142,6 +142,41 @@ Op* opParseValue(const Lex **p, const OpDesc *d) { } +Op* opParseFunc(const Lex **p, const OpDesc *d) { + const Lex *l = *p; + if (iskey(l++, d->k) && iskey(l++, K_POPEN)) { + Op *sub = opParsePart(&l, INT_MAX); + if (sub && iskey(l++, K_PCLOSE)) { + Op *op = opCreate(d); + op->a = sub; + return *p = l, op; + } + opDestroy(sub); + } + return NULL; +} + + +Op* opParseFunc2(const Lex **p, const OpDesc *d) { + const Lex *l = *p; + if (iskey(l++, d->k) && iskey(l++, K_POPEN)) { + Op *a = opParsePart(&l, INT_MAX); + if (a && iskey(l++, K_COMMA)) { + Op *b = opParsePart(&l, INT_MAX); + if (b && iskey(l++, K_PCLOSE)) { + Op *op = opCreate(d); + op->a = a; + op->b = b; + return *p = l, op; + } + opDestroy(b); + } + opDestroy(a); + } + return NULL; +} + + Op* opParseArray(const Lex **p, const OpDesc *d) { const Lex *l = *p; if (iskey(l++, d->k)) { @@ -231,41 +266,6 @@ Op* opParseTernary(const Lex **p, Op *a, const OpDesc *d) { } -Op* opParseFunc(const Lex **p, const OpDesc *d) { - const Lex *l = *p; - if (iskey(l++, d->k) && iskey(l++, K_POPEN)) { - Op *sub = opParsePart(&l, INT_MAX); - if (sub && iskey(l++, K_PCLOSE)) { - Op *op = opCreate(d); - op->a = sub; - return *p = l, op; - } - opDestroy(sub); - } - return NULL; -} - - -Op* opParseFunc2(const Lex **p, const OpDesc *d) { - const Lex *l = *p; - if (iskey(l++, d->k) && iskey(l++, K_POPEN)) { - Op *a = opParsePart(&l, INT_MAX); - if (a && iskey(l++, K_COMMA)) { - Op *b = opParsePart(&l, INT_MAX); - if (b && iskey(l++, K_PCLOSE)) { - Op *op = opCreate(d); - op->a = a; - op->b = b; - return *p = l, op; - } - opDestroy(b); - } - opDestroy(a); - } - return NULL; -} - - Op* opParsePart(const Lex **p, int priority) { //static int level = 0; //printf("opParsePart:%d:%d:", level, priority); @@ -283,6 +283,12 @@ Op* opParsePart(const Lex **p, int priority) { case M_VALUE: if (!op) op = opParseValue(&l, d); break; + case M_FUNC: + if (!op) op = opParseFunc(&l, d); + break; + case M_FUNC2: + if (!op) op = opParseFunc2(&l, d); + break; case M_ARRAY: if (!op) op = opParseArray(&l, d); break; @@ -307,12 +313,6 @@ Op* opParsePart(const Lex **p, int priority) { if (o) { op = o; i = 0; } } break; - case M_FUNC: - if (!op) op = opParseFunc(&l, d); - break; - case M_FUNC2: - if (!op) op = opParseFunc2(&l, d); - break; } } if (op) *p = l;