kuroko/test/test.krk

171 lines
4.5 KiB
Python
Raw Normal View History

2020-12-27 16:42:22 +03:00
#!/home/klange/Projects/kuroko/kuroko
import time
2020-12-27 04:32:23 +03:00
# You may want to look at this in an editor with the syntax highlighting
# set to Python. Not even bim has a highlighter for Kuroko yet.
if False:
print("Kuroko has Python-style syntax")
print("with significant whitespace.")
2020-12-27 04:32:23 +03:00
print("Blank lines are ignored.")
2020-12-27 04:32:23 +03:00
# Comments should work, too.
print("None of this should print, since it's in an `if False:`")
2020-12-27 04:32:23 +03:00
print("This is the first line that should print.")
2020-12-27 04:32:23 +03:00
# Only str can be concatenated with str
print("We can do simple concatenation " + str(123) + ".")
2020-12-27 04:32:23 +03:00
# Lox only has a 'Number' type for numerical values, but we have
# Integer and Floating to separate the two.
print(4.2 * 9.7) # Should be 40.74
print(1 + 2 + 3)
2020-12-27 04:32:23 +03:00
# Other bases:
print("Hex:", 0xFF, "Octal:", 0o123, "Binary:", 0b1010)
2020-12-27 04:32:23 +03:00
# This `for init, cond, step:` syntax is possibly temporary? I do intend to
# implement iterators and `for VAR in ITER:` like in Python, but C-style for
# loops are also useful...
for i = 0; i < 10; i = i + 1:
print("i =", i)
2020-12-27 04:32:23 +03:00
# Functions work like in Python, though currently no default values.
def function(arg): # And of course the parser will handle comments here...
print("This is a function that does a thing!")
2020-12-27 04:32:23 +03:00
if arg == "demo": # Or here...
print("You passed 'demo' as an argument!")
2020-12-27 04:32:23 +03:00
else: # And definitely here.
print("You passed something else.")
2020-12-27 04:32:23 +03:00
return 42
print("This code is after the function definition")
2020-12-27 04:32:23 +03:00
# While I'm following the book, variable declarations are explicit with `let`.
# I don't know if I want to implement Python's scoping rules, which are a bit
# ... different from other languages in that lots of control flow that you
# would normally think of as introducing scope does not do so in Python. For
# now we're following traditional scoping rules, and a simple `let foo` at
# the head of the appropriate block should work okay.
let result = function("demo")
print("The function call returned:", result)
2020-12-26 14:39:47 +03:00
2020-12-27 04:32:23 +03:00
# `sleep()` is a native function bind. Lox has `clock` as an example, but I
# figured something with arguments would be more useful? The purpose of this
# language is to be used for writing syntax highlighters, configs, and also
# plugins for bim, so native bindings are going to be very important.
result = time.sleep(0.1)
print("Call to sleep returned:", result)
2020-12-26 14:39:47 +03:00
2020-12-27 04:32:23 +03:00
function("something else")
2020-12-27 10:45:34 +03:00
# This is some stuff to test closures
class Funcs:
let funcs = Funcs()
if True:
let a = 1
def f():
print(a)
let b = 2
def g():
print(b)
let c = 3
def h():
print(c)
2020-12-27 10:45:34 +03:00
funcs.f = f
funcs.g = g
funcs.f()
funcs.g()
def outer(): # test
#foo
#multiple lines
let x = "outside"
def inner():
print(x)
return inner
print("Function is defined, creating it...")
let closure = outer()
print("And executing the result...")
2020-12-27 10:45:34 +03:00
# This should correctly print "outside"
closure()
2020-12-27 10:45:34 +03:00
# This is surprisingly similar to Python already...
print("Let's do some classes.")
2020-12-27 10:45:34 +03:00
class Test: # This is a test class
# `self` is actually optional - it's implictly passed.
# If you include it in a parameter list, it's completely ignored.
def __init__(self):
self.foo = "bax"
# Look, a method!
def doAThing():
print("yay: " + self.foo)
2020-12-27 10:45:34 +03:00
print(Test)
2020-12-27 10:45:34 +03:00
let test = Test()
#print(test) # Removed because of pointer output
test.doAThing()
2020-12-27 10:45:34 +03:00
test.foo = "bar"
print(test.foo)
print(test.doAThing)
test.doAThing()
class SuperClass():
def __init__(self):
self.a = "class"
def aMethod(self):
print("This is a great " + self.a + "!")
def __str__(self):
return "(I am a " + self.a + ")"
def __get__(self, ind):
return "(get[" + str(ind) + "])"
def __set__(self, ind, val):
print("(set[" + str(ind) + "] = " + val + ")")
class SubClass(SuperClass):
def __init__(self):
self.a = "teapot"
let subclass = SubClass()
subclass.aMethod()
# Nope
#print(self)
# Also nope
#def notAMethod():
# print(self)
# Definitely nope
#def notAMethoDeither(self):
# print(self)
2020-12-27 10:45:34 +03:00
print("Subclass says:", subclass)
subclass.__get__(123)
print(subclass[123])
subclass[456] = "test"
print("Let's make a hashmap:")
let hash = dict()
hash["hello"] = "world"
print(hash["hello"])
print("Let's make some lists:")
2020-12-28 13:01:28 +03:00
let l = list()
print("Length before:", len(l))
2020-12-28 13:01:28 +03:00
l.append(1)
l.append(2)
l.append(3)
print("Length after:", len(l))
for j = 0; j < len(l); j = j + 1:
print("j=" + str(j) + ", list[j]=" + str(l[j]))
2020-12-28 13:01:28 +03:00
print("Can we call properties of strings?".__len__()) # Of course we can.
2020-12-28 06:16:44 +03:00
return 0