-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cpp
1253 lines (1130 loc) · 26.7 KB
/
Program.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Program.cpp
// Evaluator
//
// Created by Damien Quartz on 10/9/17
//
//
// required to get M_PI on windows
#define _USE_MATH_DEFINES
#include "Program.h"
#include <chrono>
#include <ctype.h>
#include <deque>
#include <math.h>
#include <map>
const std::map<Program::Char, Program::Op::Code> UnaryOperators =
{
{ '@', Program::Op::PEK },
{ 'F', Program::Op::FRQ },
{ '#', Program::Op::SQR },
{ '$', Program::Op::SIN },
{ 'T', Program::Op::TRI },
{ '+', Program::Op::NOP },
{ '-', Program::Op::NEG },
{ '~', Program::Op::COM },
{ '!', Program::Op::NOT },
{ 'C', Program::Op::CCV },
{ 'V', Program::Op::VCV },
{ 'R', Program::Op::RND }
};
// used to store const values relating to [*],
// which allows for reading the sum of all inputs or writing the same value to all outputs.
namespace Wildcard
{
const Program::Char Char = '*';
const Program::Value Value = -1;
}
Program::Program(const std::vector<Op>& inOps, const size_t userMemorySize)
: ops(inOps)
, userMemSize(userMemorySize)
, memSize(userMemorySize + 256) // 256 to enough room for all possible values of Char
, rng(std::chrono::system_clock::now().time_since_epoch().count())
{
mem = new Value[memSize];
memset(mem, 0, sizeof(Value)*memSize);
// initialize cc memory space - we want to accurately represent the midi device
memset(cc, 0, sizeof(cc));
memset(vc, 0, sizeof(vc));
// default sample rate so the F operator will function
Set('~', 44100);
}
Program::~Program()
{
delete[] mem;
}
// static
Program::Value Program::GetAddress(const Char var, const size_t userMemorySize)
{
// we static cast to unsigned char because we want to convert the variable
// to a memory offset that begins at 0.
return userMemorySize + static_cast<unsigned char>(var);
}
//////////////////////////////////////////////////////////////////////////
// COMPILATION
//////////////////////////////////////////////////////////////////////////
#pragma region Compilation
const char * Program::GetErrorString(Program::CompileError error)
{
switch (error)
{
case Program::CE_NONE:
return "None";
case Program::CE_MISSING_PAREN:
return "Mismatched parens";
case Program::CE_MISSING_BRACKET:
return "Missing ']'";
case Program::CE_MISSING_BRACE:
return "Missing '}'";
case Program::CE_MISSING_COLON_IN_TERNARY:
return "Incomplete ternary statement - expected ':'";
case Program::CE_UNEXPECTED_CHAR:
return "Unexpected character";
case Program::CE_FAILED_TO_PARSE_NUMBER:
return "Failed to parse a numeric value";
case Program::CE_ILLEGAL_ASSIGNMENT:
return "Left side of '=' must be assignable.\n(a variable, address, or output)";
case Program::CE_ILLEGAL_STATEMENT_TERMINATION:
return "Illegal statement termination.\n"
"Semi-colon may not appear within parens,\nbrackets, braces, or ternary conditionals.";
case Program::CE_ILLEGAL_VARIABLE_NAME:
return "Illegal variable name.\n(uppercase letters are reserved for operators)";
case Program::CE_MISSING_PUT:
return "The program does not output any values.\n"
"Assign something to [0], [1], or [*].";
default:
return "Unknown";
}
}
// used during compilation to keep track of things
struct CompilationState
{
const Program::Char* const source;
const size_t userMemSize;
int parsePos;
int parenCount;
int bracketCount;
int parseDepth;
Program::CompileError error;
std::vector<Program::Op> ops;
CompilationState(const Program::Char* inSource, const size_t userMemorySize)
: source(inSource)
, userMemSize(userMemorySize)
, parsePos(0)
, parenCount(0)
, bracketCount(0)
, parseDepth(0)
, error(Program::CE_NONE)
{
}
// some helpers
Program::Char operator*() const { return source[parsePos]; }
size_t Push(Program::Op::Code code, Program::Value value = 0) { ops.push_back(Program::Op(code, value)); return ops.size()-1; }
void SkipWhitespace()
{
while (isspace(source[parsePos]))
{
++parsePos;
}
// also skip any commented text while we are at it
if (source[parsePos] == '/' && source[parsePos + 1] == '/')
{
parsePos += 2;
// read to the end of line or end of file
while (source[parsePos] != '\n' && source[parsePos] != '\0')
{
++parsePos;
}
// if we reached end of line and this is not end of file
// then we might have more whitespace to skip on the next line
if (source[parsePos] != '\0')
{
SkipWhitespace();
}
}
}
};
// forward declare Parse so that we can recurse back to it from anywhere.
static int Parse(CompilationState& state);
static int ParseAtom(CompilationState& state)
{
// Skip spaces
state.SkipWhitespace();
std::stack<Program::Op::Code> unaryOps;
// see if the current character is a unary operator
// and push the appropriate opcode onto the unaryOps stack.
// we don't push a NOP because it's pointless to have any.
while( UnaryOperators.count(*state) )
{
const Program::Op::Code code = UnaryOperators.find(*state)->second;
if ( code != Program::Op::NOP )
{
unaryOps.push(code);
}
state.parsePos++;
}
// Check if there is parenthesis
if (*state == '(')
{
state.parsePos++;
state.parenCount++;
if (Parse(state)) return 1;
if (*state != ')')
{
// Unmatched opening parenthesis
state.error = Program::CE_MISSING_PAREN;
return 1;
}
state.parsePos++;
state.parenCount--;
while (!unaryOps.empty())
{
state.Push(unaryOps.top());
unaryOps.pop();
}
return 0;
}
// check for bracket '['
if (*state == '[')
{
state.parsePos++;
state.bracketCount++;
// check for wildcard before attempting to parse an expression
state.SkipWhitespace();
if (*state == Wildcard::Char)
{
state.parsePos++;
state.Push(Program::Op::PSH, Wildcard::Value);
state.SkipWhitespace();
}
else if (Parse(state))
{
return 1;
}
if (*state != ']')
{
state.error = Program::CE_MISSING_BRACKET;
return 1;
}
state.parsePos++;
state.bracketCount--;
state.Push(Program::Op::GET);
while (!unaryOps.empty())
{
state.Push(unaryOps.top());
unaryOps.pop();
}
return 0;
}
if (isalpha(*state))
{
if (islower(*state))
{
const Program::Char var = *state;
// push the address of the variable, which peek will need
const Program::Value varAddress = Program::GetAddress(var, state.userMemSize);
state.Push(Program::Op::PSH, varAddress);
state.Push(Program::Op::PEK);
state.parsePos++;
}
else
{
state.error = Program::CE_ILLEGAL_VARIABLE_NAME;
return 1;
}
}
else // parse a numeric value
{
const Program::Char* startPtr = state.source + state.parsePos;
Program::Char* endPtr = nullptr;
Program::Value res = (Program::Value)strtoull(startPtr, &endPtr, 0);
// failed to parse a number
if (endPtr == startPtr)
{
state.error = Program::CE_FAILED_TO_PARSE_NUMBER;
return 1;
}
state.Push(Program::Op::PSH, res);
// advance our index based on where the end pointer wound up
state.parsePos += (endPtr - startPtr) / sizeof(Program::Char);
}
while (!unaryOps.empty())
{
state.Push(unaryOps.top());
unaryOps.pop();
}
return 0;
}
static int ParseFactors(CompilationState& state)
{
if (ParseAtom(state)) return 1;
for (;;)
{
state.SkipWhitespace();
// Save the operation and position
Program::Char op = *state;
if (op != '/' && op != '*' && op != '%')
{
return 0;
}
state.parsePos++;
if (ParseAtom(state)) return 1;
// Perform the saved operation
if (op == '/')
{
state.Push(Program::Op::DIV);
}
else if (op == '%')
{
state.Push(Program::Op::MOD);
}
else
{
state.Push(Program::Op::MUL);
}
}
}
static int ParseSummands(CompilationState& state)
{
if (ParseFactors(state)) return 1;
for (;;)
{
state.SkipWhitespace();
Program::Char op = *state;
if (op != '-' && op != '+')
{
return 0;
}
state.parsePos++;
if (ParseFactors(state)) return 1;
switch (op)
{
case '-': state.Push(Program::Op::SUB); break;
case '+': state.Push(Program::Op::ADD); break;
}
}
}
static int ParseCmpOrShift(CompilationState& state)
{
if (ParseSummands(state)) return 1;
for (;;)
{
state.SkipWhitespace();
Program::Char op = *state;
if (op != '<' && op != '>')
{
return 0;
}
state.parsePos++;
Program::Char op2 = *state;
// not a bitshift, so do compare
if (op2 != op)
{
// for <= and >= we need to eat the equals character and push a different opcode
bool isEqual = op2 == '=';
if (isEqual)
{
state.parsePos++;
}
if (ParseSummands(state)) return 1;
switch (op)
{
case '<': state.Push(isEqual ? Program::Op::CLE : Program::Op::CLT); break;
case '>': state.Push(isEqual ? Program::Op::CGE : Program::Op::CGT); break;
}
}
else
{
// is a bitshift, so eat it and continue
state.parsePos++;
if (ParseSummands(state)) return 1;
switch (op)
{
case '<': state.Push(Program::Op::BSL); break;
case '>': state.Push(Program::Op::BSR); break;
}
}
}
}
static int ParseCEQ(CompilationState& state)
{
if (ParseCmpOrShift(state)) return 1;
for (;;)
{
state.SkipWhitespace();
const Program::Char op = *state;
if (op != '=' && op != '!')
{
return 0;
}
state.parsePos++;
if (*state != '=')
{
// don't want to eat the first character if it is not followed by an equals, otherwise we will mess up assignments and negation
state.parsePos--;
return 0;
}
// eat the equals sign
state.parsePos++;
if (ParseCmpOrShift(state)) return 1;
switch (op)
{
case '=': state.Push(Program::Op::CEQ); break;
case '!': state.Push(Program::Op::CNE); break;
}
}
}
static int ParseAND(CompilationState& state)
{
if (ParseCEQ(state)) return 1;
for (;;)
{
state.SkipWhitespace();
Program::Char op = *state;
if (op != '&')
{
return 0;
}
state.parsePos++;
if (ParseCEQ(state)) return 1;
state.Push(Program::Op::AND);
}
}
static int ParseXOR(CompilationState& state)
{
if (ParseAND(state)) return 1;
for (;;)
{
state.SkipWhitespace();
Program::Char op = *state;
if (op != '^')
{
return 0;
}
state.parsePos++;
if (ParseAND(state)) return 1;
state.Push(Program::Op::XOR);
}
}
static int ParseOR(CompilationState& state)
{
if (ParseXOR(state)) return 1;
for (;;)
{
state.SkipWhitespace();
Program::Char op = *state;
if (op != '|')
{
return 0;
}
state.parsePos++;
if (ParseXOR(state)) return 1;
state.Push(Program::Op::OR);
}
}
static int ParseCND(CompilationState& state)
{
if (ParseOR(state)) return 1;
for (;;)
{
state.SkipWhitespace();
Program::Char op = *state;
if (op != '?')
{
return 0;
}
state.parsePos++;
// result of the expression before the ? will be on the top of the stack now,
// the CND instruction needs to check that value and jump over the next expression if it is false.
// we won't know where to jump until after generating the instructions for the expression,
// so we stash where in the the ops list our CND op needs to go, which allows us to insert it when we have the address.
size_t cndOpAddr = state.Push(Program::Op::CND);
// parse expression following the ?
// we decrement parseDepth before calling Parse because it's OK if the expression ends with a semi-colon.
// this will make the statement behave like an if statement.
state.parseDepth--;
if (Parse(state)) return 1;
state.parseDepth++;
state.SkipWhitespace();
// this means it ended with a semi-colon, we need to insert some instructions before this, so we remove it and add it back
bool hasPop = state.ops.back().code == Program::Op::POP;
if (hasPop)
{
state.ops.pop_back();
}
// add a JMP instruction so we can skip what comes next, which is the "false" part of the expression
size_t jmpOpAddr = state.Push(Program::Op::JMP);
// CND needs to jump to the instruction that follows the JMP
state.ops[cndOpAddr].val = state.ops.size();
// if there is a colon, the user has provided code to execute for "false".
// if there isn't, then we need to provide the result of the expression, which will simply be 0.
// in other words:
// a = b ? c;
// is simply syntactic sugar for:
// a = b ? c : 0;
if (*state == ':')
{
// if they put a semi-colon before the colon, Parse won't have caught it, so we do so here
if (hasPop)
{
state.error = Program::CE_ILLEGAL_STATEMENT_TERMINATION;
return 1;
}
// eat the colon
state.parsePos++;
// when parsing what follows the colon, we decrement parse depth before recursing to Parse.
// this is so that if the line terminates with a semi-colon, we won't get an
// illegal statement termination error, unless we were already within parens.
state.parseDepth--;
if (Parse(state)) return 1;
state.parseDepth++;
}
else
{
state.Push(Program::Op::PSH, 0);
// include the semi-colon that is in the source
if (hasPop)
{
state.Push(Program::Op::POP);
}
}
// if the statement terminated in a semi-colon we will have a POP on the end of the list.
// we need to jump directly to that POP because it *might* be replaced with a PEK or PUT.
if (state.ops.back().code == Program::Op::POP)
{
state.ops[jmpOpAddr].val = state.ops.size() - 1;
}
else
{
// when there's no POP we need to JMP to the instruction that will follow it.
state.ops[jmpOpAddr].val = state.ops.size();
}
}
}
static int ParsePOK(CompilationState& state)
{
if (ParseCND(state)) return 1;
for (;;)
{
state.SkipWhitespace();
Program::Char op = *state;
if (op != '=')
{
return 0;
}
state.parsePos++;
// PEK and GET work by popping a value from the stack to use as the lookup address.
// so when we want to POK or PUT, we can use that same address to know where in memory to assign the result of the right side.
// we just need to remove the existing instruction so the address will still be on the stack after the instructions generated by ParseTRN complete.
// we require the presence of a PEK or GET instruction because we don't want to allow statements like '5 = 4'
// instead, we accept '@5 = 4', which means "set memory address 5 to the value 4".
// similarly, 'a = 4' will assign 4 to the memory address reserved for the variable 'a' (see ParseAtom).
// [0] = 5 will put the value 5 into first output result
Program::Op::Code code = state.ops.back().code;
if (code == Program::Op::PEK || code == Program::Op::GET)
{
state.ops.pop_back();
}
else
{
state.error = Program::CE_ILLEGAL_ASSIGNMENT;
return 1;
}
state.SkipWhitespace();
// how many values to POK or PUT
int pcount = 0;
// check for the beginning of an "array" on the right side of the equals sign.
if (*state == '{')
{
state.parsePos++;
for(;;)
{
if (Parse(state)) return 1;
++pcount;
if (*state == ',')
{
state.parsePos++;
continue;
}
// if we didn't find a comma, we *should* find the closing brace
if (*state == '}')
{
state.parsePos++;
break;
}
else
{
state.error = Program::CE_MISSING_BRACE;
return 1;
}
}
}
else // no array, so it's just a single expression
{
// decrement the parse depth before recursing because it's
// OK if the expression on the right hand side terminates in a semi-colon
state.parseDepth--;
if (Parse(state)) return 1;
state.parseDepth++;
pcount = 1;
}
// the statement on the right side of the '=' might have ended with a semi-colon,
// which means the last op will be a POP. we need to POK or PUT before that.
const bool hasPOP = state.ops.back().code == Program::Op::POP;
if (hasPOP)
{
state.ops.pop_back();
}
switch (code)
{
case Program::Op::PEK:
state.Push(Program::Op::POK, pcount);
break;
case Program::Op::GET:
state.Push(Program::Op::PUT, pcount);
break;
// fix warning in osx
default:
break;
}
if (hasPOP)
{
state.Push(Program::Op::POP);
}
}
}
// we start here so that we don't have to change the forward declare for ParseAtom when we add another level
static int Parse(CompilationState& state)
{
state.parseDepth++;
{
if (ParsePOK(state)) return 1;
// check for statement termination
state.SkipWhitespace();
if (*state == ';')
{
// if we have recursed into Parse due to opening parens
// or due to parsing a section of a ternary operator,
// we should throw an error if we encounter a semi-colon
// because those constructs will not evaluate correctly
// if a POP appears in the middle of the instructions.
if (state.parseDepth != 1)
{
state.error = Program::CE_ILLEGAL_STATEMENT_TERMINATION;
return 1;
}
state.parsePos++;
state.Push(Program::Op::POP);
// skip space immediately after statement termination
// in case this is the last symbol of the program but there is trailing whitespace
state.SkipWhitespace();
}
}
state.parseDepth--;
return 0;
}
Program* Program::Compile(const Char* source, const size_t userMemorySize, CompileError& outError, int& outErrorPosition)
{
Program* program = nullptr;
CompilationState state(source, userMemorySize);
while (*state != '\0')
{
if (Parse(state)) break;
// if we aren't at the end yet, we should have a POP as the last op.
if (*state != '\0' && state.ops.back().code != Op::POP)
{
state.error = CE_UNEXPECTED_CHAR;
break;
}
}
// final error checking
if (state.error == CE_NONE)
{
// Now, expr should point to '\0', and _paren_count should be zero
if (state.parenCount != 0 || *state == ')')
{
state.error = CE_MISSING_PAREN;
}
else if (*state != '\0')
{
state.error = CE_UNEXPECTED_CHAR;
}
// make sure the program includes at least one PUT.
// we consider it a compilation error if it doesn't
// since the resulting program would output nothing
// and therefore be useless.
else
{
bool hasPut = false;
for (auto& op : state.ops)
{
hasPut = hasPut || op.code == Op::PUT;
}
if (!hasPut)
{
state.error = CE_MISSING_PUT;
}
}
}
// now create a program or don't
if (state.error == CE_NONE)
{
outError = CE_NONE;
outErrorPosition = -1;
program = new Program(state.ops, userMemorySize);
}
else
{
outError = state.error;
outErrorPosition = state.parsePos;
}
return program;
}
#pragma endregion
//////////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////////
#pragma region Execution
const char * Program::GetErrorString(Program::RuntimeError error)
{
switch (error)
{
case RE_NONE:
return "None";
case RE_DIVIDE_BY_ZERO:
return "Divide by zero";
case RE_MISSING_OPERAND:
return "Missing operand";
case RE_MISSING_OPCODE:
return "Unimplemented opcode";
case RE_INCONSISTENT_STACK:
return "Inconsistent stack";
case RE_EMPTY_PROGRAM:
return "Empty program (instruction count is zero)";
case RE_GET_OUT_OF_BOUNDS:
return "Input access is out of bounds";
case RE_PUT_OUT_OF_BOUNDS:
return "Output access is out of bounds";
default:
return "Unknown";
}
}
Program::RuntimeError Program::Run(Value* results, const size_t size)
{
RuntimeError error = RE_NONE;
const uint64_t icount = GetInstructionCount();
if (icount > 0)
{
pc = 0;
for (; pc < icount && error == RE_NONE; ++pc)
{
error = Exec(ops[pc], results, size);
}
// under error-free execution we should have either 1 or 0 values in the stack.
// 1 when a program terminates with the result of an expression (eg: t*Fn)
// 0 when a program terminates with a POP (eg: t*Fn;)
// in the case of the POP, the value of the expression will already be in result.
if (error == RE_NONE)
{
if (stack.size() > 1)
{
error = RE_INCONSISTENT_STACK;
}
}
// clear the stack so it doesn't explode in size due to continual runtime errors
while (stack.size() > 0)
{
stack.pop();
}
}
else
{
error = RE_EMPTY_PROGRAM;
}
return error;
}
#define POP1 if ( stack.size() < 1 ) goto bad_stack; Value a = stack.top(); stack.pop();
#define POP2 if ( stack.size() < 2 ) goto bad_stack; Value b = stack.top(); stack.pop(); Value a = stack.top(); stack.pop();
#define POP3 if ( stack.size() < 3 ) goto bad_stack; Value c = stack.top(); stack.pop(); Value b = stack.top(); stack.pop(); Value a = stack.top(); stack.pop();
#define POP(n) if (stack.size() < n) goto bad_stack; std::vector<Value> args; for(int i = 0; i < n; ++i) { args.push_back(stack.top()); stack.pop(); }
// perform the operation
Program::RuntimeError Program::Exec(const Op& op, Value* results, size_t size)
{
RuntimeError error = RE_NONE;
switch (op.code)
{
// no operands - result is pushed to the stack
case Op::PSH:
stack.push(op.val);
break;
case Op::POP:
{
stack.pop();
// stack should now be empty, if it isn't that's an error
if (stack.size() > 0)
{
error = RE_INCONSISTENT_STACK;
}
}
break;
// one operand - operand value is popped from the stack, result is pushed back on
case Op::PEK:
{
POP1;
stack.push(Peek(a));
}
break;
case Op::GET:
{
POP1;
Value v = 0;
// wildcard GET should return the sum of all channels
if (a == Wildcard::Value)
{
for (size_t i = 0; i < size; ++i)
{
v += results[i];
}
}
else if (a < size)
{
v = results[a];
}
else
{
error = RE_GET_OUT_OF_BOUNDS;
}
stack.push(v);
}
break;
case Op::NEG:
{
POP1;
stack.push(-a);
}
break;
case Op::SIN:
{
POP1;
Value r = Get('w');
Value hr = r / 2;
r += 1;
double s = sin(2 * M_PI * ((double)(a%r) / r));
stack.push(Value(s*hr + hr));
}
break;
case Op::SQR:
{
POP1;
const Value r = Get('w');
const Value v = a%r < r / 2 ? 0 : r - 1;
stack.push(v);
}
break;
case Op::FRQ:
{
POP1;
if (a == 0)
{
stack.push(0);
}
else
{
// 3.023625 is a magic number arrived at by comparing our output to the Saw Wave in ReaSynth.
// 3.0 is what we'd expect to see if we were operating in floating point,
// but if we use 3.0 here, the pitch winds up being a little bit flat.
double f = round(4.0 * 3.023625 * pow(2.0, (double)a / 12.0) * (44100.0 / Get('~')));
stack.push((Value)f);
}
}
break;
case Op::TRI:
{
POP1;
a *= 2;
const Value r = Get('w');
const Value v = a*((a / r) % 2) + (r - a - 1)*(1 - (a / r) % 2);
stack.push(v);
}
break;
case Op::RND:
{
POP1;
stack.push(rng() % a);
}
break;
case Op::CCV:
{
POP1;
stack.push(GetCC(a));
}
break;
case Op::VCV:
{
POP1;
stack.push(GetVC(a));
}
break;
case Op::NOT:
{
POP1;
stack.push(!a);
}
break;
case Op::COM:
{
POP1;
stack.push(~a);
}
break;
// two operands - both are popped from the stack, result is pushed back on
case Op::MUL:
{
POP2;
stack.push(a*b);
}
break;
case Op::DIV:
{
POP2;
Value v = 0;
if (b) { v = a / b; }
else { error = RE_DIVIDE_BY_ZERO; }
stack.push(v);
}
break;
case Op::MOD:
{
POP2;
Value v = 0;
if (b) { v = a%b; }
else { error = RE_DIVIDE_BY_ZERO; }
stack.push(v);
}
break;
case Op::ADD:
{
POP2;
stack.push(a + b);
}
break;
case Op::SUB:
{
POP2;