Skip to content

Adding Classes to scarpet. #306

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c0997d7
Added classes.scl
Ghoulboy78 Apr 3, 2022
f5fce43
Added max heap example
Ghoulboy78 Apr 3, 2022
0d7eb44
renamed heap.sc to min_heap.sc
Ghoulboy78 Apr 3, 2022
9ecdf1c
Renaming max_heap.sc to max_heap.scl
Ghoulboy78 Apr 3, 2022
2e1c112
Adding docs
Ghoulboy78 Apr 3, 2022
010ff89
Making a copy of the class map
Ghoulboy78 Apr 5, 2022
28b87bf
Adding inheritance
Ghoulboy78 Apr 7, 2022
1c7cdc8
Checking for own class when doing instanceof
Ghoulboy78 Apr 7, 2022
90b839a
Updating instance_of to account for inputting non-class values
Ghoulboy78 Apr 8, 2022
aaf504b
Adding .bak files to gitignore
Ghoulboy78 Apr 8, 2022
b063328
Adding object class
Ghoulboy78 Apr 8, 2022
2e56d8f
Adding interface and iterator class
Ghoulboy78 Apr 10, 2022
893e686
Removing unfinished sentence
Ghoulboy78 Apr 11, 2022
26a7c63
Adding anonymous classes
Ghoulboy78 Apr 29, 2022
02cb871
Adding limit to while loop
Ghoulboy78 May 6, 2022
6e2cde1
Checking for a null next() function to break out of loop
Ghoulboy78 May 7, 2022
b8f29f6
Added detailed Readme.md
Ghoulboy78 May 18, 2022
d6e60ce
Adding headings
Ghoulboy78 May 18, 2022
7c3ff11
Adding interface docs
Ghoulboy78 May 18, 2022
35103f0
Updating documentation in max_heap.scl
Ghoulboy78 Jul 17, 2022
0849558
Updating docs in Readme.md
Ghoulboy78 Jul 17, 2022
5730ed7
Merge branch 'classes' of https://github.com/gnembon/scarpet into cla…
Ghoulboy78 Jul 17, 2022
f4cf11e
Update classes.scl
Ghoulboy78 Oct 24, 2022
3f52786
Making docs more verbose
Ghoulboy78 Dec 9, 2022
921dfec
Update max_heap.scl
Ghoulboy78 Dec 9, 2022
38ce6ba
Update README.md
Ghoulboy78 Dec 11, 2022
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
60 changes: 60 additions & 0 deletions programs/fundamentals/classes.scl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//Library functions which add classes to scarpet
//
//Only use this if you understand the syntax, and if you have little other choice, as it adds a lot of syntactic parameters
//which may make your code harder to read.
//
//This implementation makes maps as classes, and each map has to have a __init__ function.
//All functions must take at least one parameter (like in Python 'self'), and then any additional parameters.
//If the function should return nothing, then return just the object itself (with any modifications that the function adds)
//If the function should return something, return a list with the object as the first element, and return value as the second
//For fields, just access and set them like you would access and set values in a normal map
//
//When declaring methods in the class, you can optionally add a _ infront of them to make them private.
//This means that they cannot be called using call_function(), telling the programmer that they're potentially dangerous.
//When calling functions within the class methods, just use call(self:function_name, self, args).
//This allows you to access private functions within the object.
//
//When calling a function, use call_function(), with the object as the first parameter, function name as the second
//and then any arguments at the end. If the function has a return value, then call_function() will return it, otherwise
//it will return null.
//
//
//For an example of all this, see max_heap.scl
//By: Ghoulboy


//Mostly just a decorator function tbh, but it allows user to be sure that if it passes through here
//then it's a valid class
new_class(name, declarer)->(
if(!has(declarer, '__init__') || type(declarer:'__init__')!='function', throw('missing_constructor', 'value_exception', declarer));
declarer:'__name__' = name;
declarer
);

is_class(class)->
has(class, '__init__') && type(class:'__init__')=='function' && has(class, '__name__');

check_class(class)->
if(!is_class(class), throw('invalid_class', 'value_exception', class));

//object is literally the same as class, except we have called initializer
new_object(class, ...args)->(
check_class(class);
call(class:'__init__', class, ...args);
);

//This is for calling public functions (which don't begin with _)
//You can use call() directly, but this gives return value of functions neatly and blocks you from using private methods
call_function(object, function, ...args)->(
check_class(object);
if(!has(object, function), throw('unknown_method', 'value_exception', function));
if(split(function):0 == '_', throw('hidden_method', 'value_exception', function));
cb = call(object:function, object, ...args);
if(type(cb)=='map',
object = cb; null,
type(cb)=='list',
object=cb:0; cb:1,
// must either return modified object, or a list pair of the object and the return value of the functoion
throw('invalid_function_return', 'value_exception', cb)
)
);
86 changes: 86 additions & 0 deletions programs/fundamentals/max_heap.scl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//A library script which contains the class of max heaps, implemented using classes.scl
//If you want to know how a max heap works, see documentation of min_heaps.sc (which works very differently)
//
//To use this, import it into your script, and then import 'call_function' from classes.scl, so you can use the methods
//At the bottom, commented out, is an example script showing how it works and how to use it.
//
//By: Ghoulboy

import('classes', 'new_class', 'new_object', 'call_function');

max_heap_class = new_class('MaxHeap', {
'__init__'->_(self, max_size)->(
self:'max_size' = max_size;
self:'size' = 0;
_Heap = [];
loop(max_size, _Heap+=0);
self:'_Heap' = _Heap;
self:'_FRONT' = 0;
self
),
'_parent'->_(self, pos)->[self, bitwise_shift_right(pos, 1)],
'_leftChild'->_(self, pos)->[self, bitwise_shift_left(pos, 1)],
'_rightChild'->_(self, pos)->[self, bitwise_shift_left(pos, 1) + 1],
'_isLeaf'->_(self, pos)->[self, pos*2 > self:'size'],
'_swap'->_(self, fpos, spos)->(
temp = self:'_Heap':fpos;
self:'_Heap':fpos = self:'_Heap':spos;
self:'_Heap':spos = temp;
self
),
'_maxHeapify'->_(self, pos) -> (
//Getting all this stuff here cos it doesn't change and clutters up the code like crazy
left = call(self:'_leftChild', self, pos):1;
right = call(self:'_rightChild', self, pos):1;
selfpos = self:'_Heap':pos;
leftchild = self:'_Heap':left;
rightchild = self:'_Heap':right;
if(!call(self:'_isLeaf', self, pos):1 && (selfpos < leftchild || selfpos < rightchild),
heapifypos = if(leftchild>rightchild, left, right);
call(self:'_swap', self, pos, heapifypos);
call(self:'_maxHeapify', self, heapifypos)
);
self
),
'insert'->_(self, element) -> (
if(self:'size' < self:'max_size',
self:'size' += 1;
self:'_Heap':(self:'size') = element;
current = self:'size';
while(self:'_Heap':current > self:'_Heap':(call(self:'_parent', self, current):1), self:'max_size',
call(self:'_swap', self, current, call(self:'_parent', self, current):1);
current = call(self:'_parent', self, current):1
);
);
self
),
'remove'->_(self)->(
popped = self:'_Heap':(self:'_FRONT');
self:'_Heap':(self:'_FRONT') = self:'_Heap':(self:'size');
self:'size' += -1;
call(self:'_maxHeapify', self, self:'_FRONT');
[self, popped]
),
});

max_heap(outer(max_heap_class), maxsize) -> new_object(max_heap_class, maxsize);

//This is an example of using a MaxHeap
//heap = new_object(max_heap, 5);
//print(heap:'_Heap'); => [0, 0, 0, 0, 0]
//call_function(heap, 'insert', 1);
//print(heap:'_Heap'); => [1, 0, 0, 0, 0]
//call_function(heap, 'insert', 3);
//print(heap:'_Heap'); => [3, 1, 0, 0, 0]
//call_function(heap, 'insert', 3);
//print(heap:'_Heap'); => [3, 3, 0, 1, 0]
//print(call_function(heap, 'remove')); => 3
//print(heap:'_Heap'); => [3, 1, 0, 1, 0] //The extra 1 here is becase we don't remove it from the list, rather overwrite it later
//call_function(heap, 'insert', 5);
//print(heap:'_Heap'); => [5, 3, 0, 1, 0] //The first 1 from before has overwritten the trailing 1
//call_function(heap, 'insert', 6);
//print(heap:'_Heap'); => [6, 5, 3, 1, 0]
//print(call_function(heap, 'remove'));
//print(heap:'_Heap'); => [5, 3, 0, 1, 0]
//
//print(heap); => {remove: _, __name__: MaxHeap, _Heap: [5, 3, 0, 1, 0], _leftChild: _, _isLeaf: _, _rightChild: _, _maxHeapify: _, __init__: _, _swap: _, insert: _, _parent: _, _FRONT: 0, size: 3, max_size: 5}
File renamed without changes.