Skip to content

Commit

Permalink
Includes
Browse files Browse the repository at this point in the history
  • Loading branch information
RoPi0n committed Jan 26, 2019
1 parent 0669629 commit 86f548a
Show file tree
Hide file tree
Showing 13 changed files with 1,199 additions and 251 deletions.
126 changes: 126 additions & 0 deletions bin_win32/inc/algs.mash
@@ -1,6 +1,7 @@
// Module with some algorithms

uses <bf>
uses <threads>

/*
* Quick sorting algorithm implementation.
Expand Down Expand Up @@ -42,6 +43,64 @@ proc QuickSort(arr):
FromToQSort(arr, 0, len(arr) - 1)
end

/*
* Multithreaded Quick sorting algorithm implementation.
*/

proc FromToThrQSort(arr, left, right, lvl, maxlvl):
i ?= left
j ?= right
pivot ?= arr[(left + right) \ 2]

while i < (j + 1):
while arr[i] < pivot:
i++
end

while arr[j] > pivot:
j--
end

if i < (j + 1):
tmp ?= arr[i]
arr[i] ?= arr[j]
arr[j] ?= tmp
i++
j--
end
end

if lvl < maxlvl:
if left < j:
ThrL ?= Parallel(FromToThrQSort, arr, copy(left), copy(j), lvl + 1, maxlvl)
end

if i < right:
ThrR ?= Parallel(FromToThrQSort, arr, copy(i), copy(right), lvl + 1, maxlvl)
end

try:
ThrL -> WaitFor()
ThrR -> WaitFor()
ThrL -> Free()
ThrR -> Free()
catch:
pop
end
else:
if left < j:
FromToThrQSort(arr, left, j, lvl, maxlvl)
end

if i < right:
FromToThrQSort(arr, i, right, lvl, maxlvl)
end
end
end

proc ThrQuickSort(arr, maxthr):
FromToThrQSort(arr, 0, len(arr) - 1, 0, maxthr \ 2)
end

/*
* Heap sorting algorithm implementation.
Expand Down Expand Up @@ -93,3 +152,70 @@ proc HeapSort(arr):
end
arr[0] ?= t
end

/*
* Binary search
*/

func FromToBinSearch(arr, left, right, key):
while true:
midd ?= (left + right) \ 2

if key < arr[midd]:
right ?= midd - 1
else:
if key > arr[midd]:
left ?= midd + 1
else:
return midd
end
end

if left > right:
return -1
end
end
end

func BinSearch(arr, key):
l ?= len(arr)
if l > 0:
return FromToBinSearch(arr, 0, l - 1, key)
else:
return -1
end
end

func FromToBinNext(arr, left, right, key):
while true:
midd ?= (left + right) \ 2

if key < arr[midd]:
right ?= midd - 1
else:
if key > arr[midd]:
left ?= midd + 1
else:
return midd
end
end

if left > right:
return midd
end
end
end

func BinNext(arr, key):
l ?= len(arr)
if l > 0:
r ?= FromToBinNext(arr, 0, l - 1, key)
if arr[r] > key:
return r
else:
return r + 1
end
else:
return 0
end
end
22 changes: 15 additions & 7 deletions bin_win32/inc/bf.mash
Expand Up @@ -20,7 +20,7 @@ int false 0

var null = "null"

func BoolToStr(b):
func b2s(b):
if b == true:
return "true"
else:
Expand Down Expand Up @@ -59,7 +59,8 @@ proc SizeOf(obj):
gpm
end

enum Types [TypeNull, TypeWord, TypeInt, TypeReal, TypeStr, TypeArray]
enum Types [TypeNull, TypeWord, TypeInt, TypeReal, TypeStr, TypeArray, TypeClass]
word TypePtr 255

proc TypeOf(obj):
push obj
Expand Down Expand Up @@ -120,7 +121,6 @@ end
{_end_}

func _op_in(v, array):
var i, ln
i ?= 0
ln ?= len(array)
while i < ln:
Expand All @@ -132,16 +132,24 @@ func _op_in(v, array):
return false
end

func ptr(a):
return @a
end

func temp():
return 0
end

proc GetError():
//err already in stack & already markered
func if(bool, a, b):
if bool:
return a
else:
return b
end
end

proc Exit():
jr
proc GetError():
//err already in stack & already markered
end

var ParamCount, ParamList
Expand Down
132 changes: 9 additions & 123 deletions bin_win32/inc/classes.mash
@@ -1,128 +1,14 @@
// Mash lang classes unit
// Code version: 10
// Code version: 1.0

uses <bf>

class class: // for free class-methods declaration
end // like: proc class::MyProc() -- for that --> MyClass->MyMethod = class::MyProc
uses <classes\types>

////////////////////////////////////////////////////////////////////////////////
// Point
////////////////////////////////////////////////////////////////////////////////

class point:
public:
var x, y
proc Create, Free, Set, Compare
end

proc point::Create(x, y):
$x ?= new(x)
$y ?= new(y)
end

proc point::Free():
Free($x, $y, $)
end

proc point::Set(x, y):
$x = x
$y = y
end

func point::Compare(p):
return ($x == p->x) & ($y == p->y)
end

class point3(point):
public:
var z
proc Create, Free
func Compare
end

proc point3::Create(x, y, z):
point::Create$(x, y)
$z ?= new(z)
end

proc point3::Free():
Free($z)
point::Free$()
end

func point3::Compare(p):
return ($z == p->z) & point::Compare$(p)
end

////////////////////////////////////////////////////////////////////////////////
// Vector
////////////////////////////////////////////////////////////////////////////////

class vector:
public:
var Items
proc Create, Free, Push_Back, Rem_Back, Rem_First
func Pop_Back, Pop_First, Peek_Back, Peek_First, At, Size
end

proc vector::Create():
$Items ?= new[1]
$Items[0] ?= new(0)
end

proc vector::Push_Back(Object):
SetLen($Items, $Items[0] + 2)
$Items[0]++
$Items[$Items[0]] ?= Object
end

func vector::Pop_Back():
var r = $Items[$Items[0]]
SetLen($Items, $Items[0])
$Items[0]--
return r
end

func vector::Pop_First():
var r = $Items[1]
for(var i = copy(1); i < $Items[0]; i++):
$Items[i] ?= $Items[i + 1]
end
SetLen($Items, $Items[0])
$Items[0]--
return r
end

func vector::Peek_Back():
return $Items[$Items[0]]
end

func vector::Peek_First():
return $Items[1]
end

proc vector::Rem_First():
for(var i = copy(1); i < $Items[0]; i++):
$Items[i] ?= $Items[i + 1]
end
SetLen($Items, $Items[0])
$Items[0]--
end

proc vector::Rem_Back():
SetLen($Items, $Items[0])
$Items[0]--
end

func vector::At(Index):
return $Items[Index - 1]
end

func vector::Size():
return $Items[0]
end

proc vector::Free():
Free($Items[0], $Items, $)
end
uses <classes\points>
uses <classes\vector>
uses <classes\list>
uses <classes\streams>
uses <classes\stringlist>
uses <classes\map>
uses <classes\tree>

0 comments on commit 86f548a

Please sign in to comment.