2020-12-27 16:42:22 +03:00
|
|
|
#!/home/klange/Projects/kuroko/kuroko
|
2021-01-03 03:49:19 +03:00
|
|
|
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:
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Kuroko has Python-style syntax")
|
|
|
|
print("with significant whitespace.")
|
2020-12-27 04:32:23 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Blank lines are ignored.")
|
2020-12-27 04:32:23 +03:00
|
|
|
# Comments should work, too.
|
2021-01-04 17:33:43 +03:00
|
|
|
print("None of this should print, since it's in an `if False:`")
|
2020-12-27 04:32:23 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("This is the first line that should print.")
|
2020-12-27 04:32:23 +03:00
|
|
|
|
2021-02-14 02:25:53 +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.
|
2021-01-04 17:33:43 +03:00
|
|
|
print(4.2 * 9.7) # Should be 40.74
|
|
|
|
print(1 + 2 + 3)
|
2020-12-27 04:32:23 +03:00
|
|
|
|
|
|
|
# Other bases:
|
2021-02-14 02:25:53 +03:00
|
|
|
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...
|
2021-01-08 11:14:42 +03:00
|
|
|
for i = 0; i < 10; i = i + 1:
|
2021-02-14 02:25:53 +03:00
|
|
|
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...
|
2021-01-04 17:33:43 +03:00
|
|
|
print("This is a function that does a thing!")
|
2020-12-27 04:32:23 +03:00
|
|
|
if arg == "demo": # Or here...
|
2021-01-04 17:33:43 +03:00
|
|
|
print("You passed 'demo' as an argument!")
|
2020-12-27 04:32:23 +03:00
|
|
|
else: # And definitely here.
|
2021-01-04 17:33:43 +03:00
|
|
|
print("You passed something else.")
|
2020-12-27 04:32:23 +03:00
|
|
|
return 42
|
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
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")
|
2021-02-14 02:25:53 +03:00
|
|
|
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.
|
2021-01-03 03:49:19 +03:00
|
|
|
result = time.sleep(0.1)
|
2020-12-26 12:39:29 +03:00
|
|
|
|
2021-02-14 02:25:53 +03:00
|
|
|
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 07:02:26 +03:00
|
|
|
|
2020-12-27 10:45:34 +03:00
|
|
|
# This is some stuff to test closures
|
|
|
|
class Funcs:
|
|
|
|
|
|
|
|
let funcs = Funcs()
|
2020-12-27 07:02:26 +03:00
|
|
|
if True:
|
|
|
|
let a = 1
|
|
|
|
def f():
|
2021-01-04 17:33:43 +03:00
|
|
|
print(a)
|
2020-12-27 07:02:26 +03:00
|
|
|
let b = 2
|
|
|
|
def g():
|
2021-01-04 17:33:43 +03:00
|
|
|
print(b)
|
2020-12-27 07:02:26 +03:00
|
|
|
let c = 3
|
|
|
|
def h():
|
2021-01-04 17:33:43 +03:00
|
|
|
print(c)
|
2020-12-27 10:45:34 +03:00
|
|
|
funcs.f = f
|
|
|
|
funcs.g = g
|
|
|
|
|
|
|
|
funcs.f()
|
|
|
|
funcs.g()
|
2020-12-27 07:02:26 +03:00
|
|
|
|
2020-12-27 11:53:46 +03:00
|
|
|
def outer(): # test
|
|
|
|
#foo
|
|
|
|
#multiple lines
|
2020-12-27 07:02:26 +03:00
|
|
|
let x = "outside"
|
|
|
|
def inner():
|
2021-01-04 17:33:43 +03:00
|
|
|
print(x)
|
2020-12-27 07:02:26 +03:00
|
|
|
return inner
|
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Function is defined, creating it...")
|
2020-12-27 07:02:26 +03:00
|
|
|
let closure = outer()
|
2021-01-04 17:33:43 +03:00
|
|
|
print("And executing the result...")
|
2020-12-27 10:45:34 +03:00
|
|
|
# This should correctly print "outside"
|
2020-12-27 07:02:26 +03:00
|
|
|
closure()
|
2020-12-27 10:45:34 +03:00
|
|
|
|
|
|
|
# This is surprisingly similar to Python already...
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Let's do some classes.")
|
2020-12-27 10:45:34 +03:00
|
|
|
class Test: # This is a test class
|
2020-12-27 11:53:46 +03:00
|
|
|
# `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():
|
2021-01-04 17:33:43 +03:00
|
|
|
print("yay: " + self.foo)
|
2020-12-27 10:45:34 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print(Test)
|
2020-12-27 10:45:34 +03:00
|
|
|
let test = Test()
|
2021-01-04 17:33:43 +03:00
|
|
|
#print(test) # Removed because of pointer output
|
2020-12-27 11:53:46 +03:00
|
|
|
test.doAThing()
|
2020-12-27 10:45:34 +03:00
|
|
|
test.foo = "bar"
|
2021-01-04 17:33:43 +03:00
|
|
|
print(test.foo)
|
|
|
|
print(test.doAThing)
|
2020-12-27 11:53:46 +03:00
|
|
|
test.doAThing()
|
|
|
|
|
2020-12-27 16:40:35 +03:00
|
|
|
|
|
|
|
class SuperClass():
|
|
|
|
def __init__(self):
|
|
|
|
self.a = "class"
|
|
|
|
def aMethod(self):
|
2021-01-04 17:33:43 +03:00
|
|
|
print("This is a great " + self.a + "!")
|
2020-12-27 16:40:35 +03:00
|
|
|
def __str__(self):
|
|
|
|
return "(I am a " + self.a + ")"
|
|
|
|
def __get__(self, ind):
|
2021-02-14 02:25:53 +03:00
|
|
|
return "(get[" + str(ind) + "])"
|
2020-12-27 16:40:35 +03:00
|
|
|
def __set__(self, ind, val):
|
2021-02-14 02:25:53 +03:00
|
|
|
print("(set[" + str(ind) + "] = " + val + ")")
|
2020-12-27 16:40:35 +03:00
|
|
|
|
|
|
|
class SubClass(SuperClass):
|
|
|
|
def __init__(self):
|
|
|
|
self.a = "teapot"
|
|
|
|
|
|
|
|
let subclass = SubClass()
|
|
|
|
subclass.aMethod()
|
|
|
|
|
2020-12-27 11:53:46 +03:00
|
|
|
# Nope
|
2021-01-04 17:33:43 +03:00
|
|
|
#print(self)
|
2020-12-27 11:53:46 +03:00
|
|
|
# Also nope
|
|
|
|
#def notAMethod():
|
2021-01-04 17:33:43 +03:00
|
|
|
# print(self)
|
2020-12-27 11:53:46 +03:00
|
|
|
# Definitely nope
|
|
|
|
#def notAMethoDeither(self):
|
2021-01-04 17:33:43 +03:00
|
|
|
# print(self)
|
2020-12-27 10:45:34 +03:00
|
|
|
|
2021-02-14 02:25:53 +03:00
|
|
|
print("Subclass says:", subclass)
|
2020-12-27 16:40:35 +03:00
|
|
|
|
|
|
|
subclass.__get__(123)
|
2021-01-04 17:33:43 +03:00
|
|
|
print(subclass[123])
|
2020-12-27 16:40:35 +03:00
|
|
|
subclass[456] = "test"
|
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Let's make a hashmap:")
|
2020-12-28 14:38:26 +03:00
|
|
|
let hash = dict()
|
2020-12-27 16:40:35 +03:00
|
|
|
hash["hello"] = "world"
|
2021-01-04 17:33:43 +03:00
|
|
|
print(hash["hello"])
|
2020-12-27 16:40:35 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Let's make some lists:")
|
2020-12-28 13:01:28 +03:00
|
|
|
let l = list()
|
2021-02-14 02:25:53 +03:00
|
|
|
print("Length before:", len(l))
|
2020-12-28 13:01:28 +03:00
|
|
|
l.append(1)
|
|
|
|
l.append(2)
|
|
|
|
l.append(3)
|
2021-02-14 02:25:53 +03:00
|
|
|
print("Length after:", len(l))
|
2021-01-08 11:14:42 +03:00
|
|
|
for j = 0; j < len(l); j = j + 1:
|
2021-02-14 02:25:53 +03:00
|
|
|
print("j=" + str(j) + ", list[j]=" + str(l[j]))
|
2020-12-28 13:01:28 +03:00
|
|
|
|
2021-01-04 17:33:43 +03:00
|
|
|
print("Can we call properties of strings?".__len__()) # Of course we can.
|
2020-12-28 06:16:44 +03:00
|
|
|
|
|
|
|
return 0
|