150 likes | 257 Views
Stacky. By Andrew Horn And Ryan Kluck. Language Features. The basic data structure that this language utilizes is a stack Each function gets its own stack All variables are assigned memory locations All operations and functions can only be evaluated by being pushed onto the stack.
E N D
Stacky By Andrew Horn And Ryan Kluck
Language Features The basic data structure that this language utilizes is a stack Each function gets its own stack All variables are assigned memory locations All operations and functions can only be evaluated by being pushed onto the stack
Types of Arguments Integers Function calls Memory calls Operator calls Condition calls
Things you can push Numbers Functions Function declarations (lambda expressions) Operators (including conditionals) push(1,2,3); push(:swap); push(func::{return 0}); push(1,2,+); push(1,2,<);
Return Statements A return statement pushes the returned values back onto the stack the returning function was called from in the order specified in the return statement The code on the right pushes 2 and 5 onto the stack, then calls swap which returns [5,2] which is concatenated onto the end of the stack in main func:swap:2{ return(0,1); } func:main:{ push(2,5); push(:swap); }
Basic Syntax The example on the right is a properly formatted program that push 1 onto the stack in main, and then pushes the copy function onto the stack Copy pops 1 parameter off the main stack and returns that element and a copy of that element. Both the original element and the copy are pushed onto the main stack func:copy:1{ return(0,0); } func:main:{ push(1); push(:copy); }
Access To The Stack A called function may access any number of values from the stack Any values passed to a function will be popped from the stack
Memory Memory locations can be assigned to hold any argument as variable The code on the right pushes swap onto the stack, and then pops it off and stores it in memory. Then it pushes 1,2,3, and 4 onto the stack It then pops 4 off the stack and stores it in memory location 1, and pulls swap in from memory location 0 func:swap:2{ return(0,1); } func:main:{ push(:swap,>[0]); push(1,2,3,4,>[1],<[0]); }
Functions A function is declared by func:<functionName>:<number of parameters>{} If number of parameters is not present this value is 0 by default The number of parameters is how many parameters are popped off of the stack when the function is pushed onto the stack func:swap:2{ return(0,1); } func:two:{ push(2); return(0); }
Functions Continued If a function is pushed onto a stack without enough parameters for the function to run, it will remain on stack until it can run, is popped off, or the main function terminates func:swap:2{ return(0,1); } func:main:{ push(1); push(:swap); push(2); push(:swap); }
If Statements An if statement peeks at the values in the stack without popping them If statements are post fix The code on the right tests if location the second element in the stack is greater than the top element and if so swaps them func:main:{ push(2,1); if(1 0 >){ push(:swap); } }
While Loops While loops test if a stack location is 0 and iterate until this happens The code on the right pops values off the stack with each iteration, until the top value is 0 func:pop:1{} func:main:{ push(0,2,3,4); while(0){ push(:pop); } }
While Loops Continued While loops can also use conditionals The code on the right tests if the top two elements on the stack are equal, and if not keeps popping elements off until this condition is met func:pop:1{} func:main:{ push(1,2,2,3,4); while(0 1 !=){ push(:pop); } }
Potential Future Improvements Strings (should be implemented as a list of characters on the stack) Lazy evaluation Overloading SQL