Skip to content

Instantly share code, notes, and snippets.

@lyxal
Created September 24, 2019 07:59
Show Gist options
  • Save lyxal/633eda6540b6e580d4dc5d8cdd4048c7 to your computer and use it in GitHub Desktop.
Save lyxal/633eda6540b6e580d4dc5d8cdd4048c7 to your computer and use it in GitHub Desktop.
Keg2py

Keg to Python Reference

It's official. Keg is now a transpiled language, as interpreting it was just too hard. Here is a guide of how everything now works

graph TB
First[Preprocessor] --> Second
Second[Uncompressor] --> Third
Third[Parser] --> Fourth
Fourth[Translator] --> Fifth
Fifth[Execution] --> Output
Loading

Command Glossary

Alphanumeric characters

Characters in the range of a-z and A-Z will be pushed as so:

letter()

As such, the program ABC would be transpiled as:

A(); B(); C()

And Hello World would become:

H(); e(); l(); l(); o(); space(); W(); o(); r(); l(); d()

This allows for letters to be redefined as macros if that makes sense. (More on that later)

Numbers in the range of 0-9 would follow as such:

0: zero()
1: one()
2: two()
3: three()
4: four()
5: five()
6: six()
7: seven()
8: eight()
9: nine()

Thenceforth, 89 would become:

eight(); nine()

Mathematical operators

Quite simple really:

+: add(stack.pop(), stack.pop())
-: minus(stack.pop(), stack.pop())
*: times(stack.pop(), stack.pop())
/: divide(stack.pop(), stack.pop())
%: modulo(stack.pop(), stack.pop())

Conditional Operators

I'mma start using a new way of writing these. All functions assume the parameters stack.pop(), stack.pop()

=: eq()
≠: nq()
>: gt()
<: lt()
≥: ge()
≤: le()
≬: g0()

Built-in FNS

!: length()
$: swap()
^: reverse()
:: duplicate()
": r_shift()
': l_shift()
,: nice()
.: raw()
?: _input()
~: random()
_: stack.pop()

If Statements

Unlike all prior sections, this section shan't be so brief. Why? Because the humble [...|...] isn't just a function you see.

The general form will become:

if bool(stack.pop):
	...
else:
	...

But what if there is only one section? (i.e. [...])

if bool(stack.pop()):
	...

But what if there is an empty ifTrue section but a filled ifFalse section? (i.e. [|...])

if bool(stack.pop()):
	pass
else:
	...

Although maybe this is more appropriate:

if not bool(stack.pop()):
	...

I mean, really, the only person bothering with this is those making transpilers.

For Loops

These are a bit harder, as, well, Keg loops are a little different. Given the normal counted loop (no vars, integer as condition):

for _ in _loop_eval(stack.pop())
	...

It is important to note that _loop_eval() is defined as such:

def _loop_eval(expr):
	if type(expr) in [int, chr]:
		return range(expr)
	else:
		return expr

But what if there are three parts? (i.e. (count|var|code))

for var in _loop_eval(count):
	code

But what if there isn't a loop condition?

length()
for _ in _loop_eval(stack.pop()):
	...

Written with StackEdit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment