From 0968fdd2d173fed7aefb8c4ff43dc514505ce068 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Mon, 7 Dec 2020 18:06:29 +0100 Subject: [PATCH] :bug: Fix scoping --- src/emit/emit.c | 4 +- src/lib/lib_func.cnano | 196 +++++++++++++++++++++++++++++++++++++++++ src/lib/object.c | 1 + src/lib/object_op.c | 14 --- 4 files changed, 199 insertions(+), 16 deletions(-) create mode 100644 src/lib/lib_func.cnano diff --git a/src/emit/emit.c b/src/emit/emit.c index 4ce3bfa5..1e2f334d 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1173,9 +1173,10 @@ static inline void stack_alloc(const Emitter emit) { // maybe vararg could use t static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) { ++emit->env->scope->depth; - emit_push_scope(emit); const Instr gc = emit_add_instr(emit, NoOp); + emit_push_scope(emit); const m_bool ret = emit_stmt(emit, stmt, pop); + emit_pop_scope(emit); if(ret > 0) { const m_bool pure = !vector_back(&emit->info->pure); if(!pure) { @@ -1183,7 +1184,6 @@ static m_bool scoped_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) emit_add_instr(emit, GcEnd); } } - emit_pop_scope(emit); --emit->env->scope->depth; return ret; } diff --git a/src/lib/lib_func.cnano b/src/lib/lib_func.cnano new file mode 100644 index 00000000..d524d5cd --- /dev/null +++ b/src/lib/lib_func.cnano @@ -0,0 +1,196 @@ +#require Std +#require Math +#require Modules + +enum Pitch { C, D, E, F, G, A, B } + +class MusicObject { + fun UGen update() {} + fun UGen onStart() {} + fun UGen onEnd() {} +} + +var SinOsc osc; + +class MyTone extends MusicObject { + var float freq; + + fun UGen onStart() { + freq => osc.freq; + return osc => dac; + } + + fun UGen onEnd() { + return osc =< dac; + } +} + +fun MyTone tone(float freq) { + var MyTone t; + 660 => t.freq; + return t; +} + +class Note extends MusicObject { + var Pitch pitch; + var int accidental; + var int octave; + + + fun int toMidi() { + var int p; + + match (pitch) { + case C: 0 => p; + case D: 2 => p; + case E: 4 => p; + case F: 5 => p; + case G: 7 => p; + case A: 9 => p; + case B: 11 => p; + } + + return p + (12 * (octave+1)) + accidental; + } + + fun UGen onStart() { + toMidi() => Std.mtof => osc.freq; + return osc => dac; + } + + fun UGen onEnd() { + osc =< dac; + } +} + +fun Note note(Pitch pitch, int accidental, int octave) { + var Note n; + A => n.pitch; + 0 => n.accidental; + 4 => n.octave; + return n; +} + +class Coord { + var dur start; + var dur end; +} + +fun Coord coord(dur start, dur end) { + const Coord c; + start => c.start; + end => c.end; + return c; +} + +class TLEvent { + fun dur getStart() {} + fun void doIt() {} +} + +class StartTLEvent extends TLEvent { + var dur start; + var MusicObject obj; + fun dur getStart() { return start; } + fun void doIt() { + obj.onStart(); + } +} + +fun StartTLEvent startevent(dur start, MusicObject obj) { + const StartTLEvent evt; + start => evt.start; + obj @=> evt.obj; + return evt; +} + +class EndTLEvent extends TLEvent { + var dur start; + var MusicObject obj; + fun dur getStart() { return start; } + fun void doIt() { + obj.onEnd(); + } +} + +fun EndTLEvent endevent(dur start, MusicObject obj) { + const EndTLEvent evt; + start => evt.start; + obj @=> evt.obj; + return evt; +} + +class DTimeline { + var TLEvent events[0]; + + fun void add(TLEvent event) { + for (0 => var int i; i < events.size(); ++i) { + if (event.getStart() < events[i].getStart()) { + for (events.size()-1 => var int j; j >= i; --j) { + events[j-1] @=> const TLEvent temp; + events[j] @=> events[j-1]; + temp @=> events[j]; + } + return; + } + } + events << event; + } + + fun void perform() { + 0::second => var dur current; + foreach(evt:events) { + evt.getStart() - current => now; + evt.getStart() => current; + evt.doIt(); + } + } +} + +class Frame { + var int test; + var Coord coord; + var MusicObject objs[0]; +} + +class Timeline { + const Frame frames[0]; + + fun void add(Coord c, MusicObject obj) { + var Frame toAdd; + for (0 => var int i; i < frames.size(); ++i) { + if ((c.start >= frames[i].coord.start) && (c.start <= frames[i].coord.start) + && (c.end >= frames[i].coord.end) && (c.end <= frames[i].coord.end)) { + frames[i].objs << obj; + return; + } + } + var Coord c2; + c.start => c2.start; + c.end => c2.end; + toAdd.objs << obj; + c2 @=> toAdd.coord; + frames << toAdd; + } + + fun DTimeline toDTimeline() { + var DTimeline theTL; + foreach(frame:frames) { + foreach(obj:frame.objs) { + theTL.add(startevent(frame.coord.start, obj)); + theTL.add(endevent(frame.coord.end, obj)); + } + } + return theTL; + } + + fun void perform() { + this.toDTimeline().perform(); + } +} + +var Timeline t; +t.add(coord(0::second, 2::second), note(A,0,4)); +t.add(coord(2::second,4::second), note(C,1,5)); +<<< t.frames[0].objs.size() >>>; +t.perform(); diff --git a/src/lib/object.c b/src/lib/object.c index 01bd4edd..115f8084 100644 --- a/src/lib/object.c +++ b/src/lib/object.c @@ -62,6 +62,7 @@ ANN static void handle_dtor(const M_Object o, const VM_Shred shred) { __attribute__((hot)) ANN void __release(const M_Object o, const VM_Shred shred) { + vector_rem2(&shred->gc, (vtype)o); MemPool p = shred->info->mp; Type t = o->type_ref; do { diff --git a/src/lib/object_op.c b/src/lib/object_op.c index e9ca5822..4e868d71 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -67,20 +67,6 @@ static OP_CHECK(at_object) { const Exp_Binary* bin = (Exp_Binary*)data; const Type l = bin->lhs->info->type; const Type r = bin->rhs->info->type; - - if(bin->lhs->exp_type == ae_exp_unary && bin->lhs->d.exp_unary.op == insert_symbol(emit->gwion->st, "new")) { - for(m_uint i = vector_size(&emit->code->instr); --i;) { - const Instr instr = (Instr)vector_at(&emit->code->instr, i - 1); - if(instr->opcode == (m_uint)GcAdd) { - vector_rem(&emit->code->instr, i - 1); - mp_free(emit->gwion->mp, Instr, instr); - } - } - } else if(bin->lhs->exp_type != ae_exp_primary || bin->lhs->d.prim.prim_type != ae_prim_array) { - const Instr instr = emit_add_instr(emit, RegAddRef); - instr->m_val = -SZ_INT*2; - } - if(nonnull_check(l, r)) { const Instr instr = emit_add_instr(emit, GWOP_EXCEPT); instr->m_val = SZ_INT; -- 2.43.0