Browse Source

Moved all sol files to programs directory

master
Benjamin Lannon 6 years ago
parent
commit
7f34aeb132
  1. 42
      programs/dump.sol
  2. 86
      programs/interp.sol
  3. 472
      programs/monty.sol
  4. 1
      programs/subtest.sol
  5. 411
      programs/test.sol
  6. 11
      programs/test_monty.sol

42
programs/dump.sol

@ -0,0 +1,42 @@
func dump(obj, indent)
if None == indent then
indent = 0
seen = {}
end
io.stdout:write(" "*indent)
if type(obj) == "list" then
buf = buffer.fromobject(obj)
addr = buf:address()
if None != seen[obj] then
print("...("+addr+")")
return
end
seen[obj] = 1
print("[")
for elem in obj do
dump(elem, indent+2)
end
print(" "*indent+"] =("+addr+")")
return
end
if type(obj) == "map" then
buf = buffer.fromobject(obj)
addr = buf:address()
if None != seen[obj] then
print("...("+addr+")")
return
end
seen[obj] = 1
print("{")
for key in obj do
io.stdout:write(" "*(indent+2))
prepr(key, ":")
dump(obj[key], indent+4)
end
print(" "*indent+"} =("+addr+")")
return
end
prepr(obj)
end
dump.closure.seen = {}

86
programs/interp.sol

@ -0,0 +1,86 @@
-- The Solterpreter! A simple command-line interface for the compiler.
print('Solterpreter/Viperpreter v0.1')
print('(Runtime version ', debug.version, ')')
__interp = {
running = 1,
buffer = '',
ps1 = '>>> ',
ps2 = '... ',
stmt_stack=0,
}
func exit()
__interp.running=0
end
quit = exit
while __interp.running do
if #__interp.buffer then
__interp.prompt = __interp.ps2
else
__interp.prompt = __interp.ps1
end
__interp.line = readline.readline(__interp.prompt)
if #__interp.line then readline.add_history(__interp.line) end
--prepr(__interp.line)
--prepr(__interp)
if (__interp.line:sub(-4, None)=="then") then
__interp.buffer+=__interp.line+" "
__interp.stmt_stack+=1
else
if (__interp.line:sub(-2, None)=="do") then
__interp.buffer+=__interp.line+" "
__interp.stmt_stack-=1
else
if __interp.line:sub(-1, None)=="\" then
__interp.buffer+=__interp.line:sub(0, -1)+" "
else
__interp.buffer+=__interp.line+" "
if __interp.line:sub(-3, None)=="end" then
__interp.stmt_stack-=1
end
if __interp.stmt_stack<=0 then
__interp.stmt_stack=0
__interp.program = try(parse, __interp.buffer)
if !__interp.program[0] then
print('Syntax error')
else
if !(try(func() __interp.program[1].stmtlist[0].type end)[0]) then
print('NULL program error')
else
if __interp.program[1].stmtlist[0].type == ast.ST_EXPR then
__interp.program[1] = __interp.program[1].stmtlist[0].expr
__interp.isexpr = 1
else
__interp.isexpr = 0
end
__interp.result = try(__interp.program[1])
if !__interp.result[0] then
print(__interp.result[1])
print(__interp.result[2])
for ent in __interp.result[2] do
st = ent[0]
scope = ent[1]
if st.type == ast.ST_LIST then continue end
print('In', st, 'at', st.loc.line, ',', st.loc.col, ':')
ast.print(st)
print(scope)
print('---')
end
else
if __interp.isexpr then
prepr(__interp.result[1])
_ = __interp.result[1]
end
end
end
end
__interp.buffer=''
end
end
end
end
end

472
programs/monty.sol

@ -0,0 +1,472 @@
TOK = {LPAREN = 1, RPAREN = 2, INT = 3, BOOL = 4, NAME = 5, QUOTE = 6, EOF = 7}
keys = []
for k in TOK do keys:insert(#keys, k) end
for k in keys do TOK[TOK[k]]=k end
token = {
new = func (type, value)
return {type = type, value = value, __index = token}
end,
pretty = func(self)
tname = TOK[self.type]
tval = tostring(self.value)
return '{'+tname+':'+tval+'}'
end
}
tokenizer = {
WS = " "+chr(8)+chr(9)+chr(10),
NAMESET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=+-*/.<>?!@$%^~",
DIGITS = "0123456789",
EOF = {},
new = func (str)
res = {str = str, pushed = None, __index = tokenizer}
res:init()
return res
end,
init = func(self)
--print('In init, self is', self)
res.cur = res:token()
res.next = res:token()
end,
next_char = func(self)
if self.pushed == None then
--print('In next_char, self is', self)
--print('In next_char, self.str is', self.str)
if self.str:eof() then return self.EOF end
res = self.str:read(1)
else
--print('Retrieving from pushback', self.pushed)
res = self.pushed[0]
self.pushed = self.pushed:sub(1)
if self.pushed == "" then self.pushed = None end
end
--print(res)
return res
end,
push_back = func(self, s)
--print('Pushing back', s)
if s == self.EOF then print('WARNING: Attempted to push_back EOF'); return end
if self.pushed == None then
self.pushed = s
else
self.pushed = s + self.pushed
end
--print('self.pushed:', self.pushed)
end,
token = func (self)
--print('In token, self is', self)
--print('In token, self.str is', self.str)
c = self:next_char()
while !(c == self.EOF) do
if c == "" then return token.new(TOK.EOF, None) end
if c == "(" then return token.new(TOK.LPAREN, c) end
if c == ")" then return token.new(TOK.RPAREN, c) end
if self.NAMESET:find(c) >= 0 then
--print('{NAME}')
name = c
c = self:next_char()
while 1 do
found = 0
if self.NAMESET:find(c) >= 0 then found = 1 end
if self.DIGITS:find(c) >= 0 then found = 1 end
if !found then break end
name += c
c = self:next_char()
if c == self.EOF then continue end
end
self:push_back(c)
return token.new(TOK.NAME, name)
end
if self.DIGITS:find(c) >= 0 then
val = c
c = self:next_char()
while self.DIGITS:find(c) >= 0 do
val += c
c = self:next_char()
if c == self.EOF then continue end
end
self:push_back(c)
return token.new(TOK.INT, toint(val))
end
if c == "#" then
c = self:next_char()
if c == "t" then return token.new(TOK.BOOL, 1) end
if c == "f" then return token.new(TOK.BOOL, 0) end
error("Invalid value for bool literal: "+c)
end
if c == "'" then return token.new(TOK.QUOTE, c) end
if self.WS:find(c) >= 0 then
c = self:next_char()
continue
end
if c == ";" then
c = self:next_char()
while 1 do
if c == chr(10) then break end
c = self:next_char()
end
c = self:next_char()
continue
end
error("Invalid character in token stream: "+c)
end
return token.new(TOK.EOF, None)
end,
advance = func(self)
self.cur = self.next
self.next = self:token()
end
}
ttreegen = {
new = func(tok)
return {tok = tok, __index = ttreegen}
end,
generate = func(self, consume)
res = self.TT_DISPATCH[self.tok.cur.type](self, self.tok.cur)
if None == consume then self.tok:advance() end
return res
end,
TT_DISPATCH = {
[TOK.LPAREN] = func(self, tok)
toklist = []
self.tok:advance()
tok = self.tok.cur
while 1 do
if tok.type == TOK.RPAREN then break end
if tok.type == TOK.EOF then error('Encountered EOF while matching delimiter') end
toklist:insert(#toklist, self.TT_DISPATCH[tok.type](self, tok))
self.tok:advance()
tok = self.tok.cur
end
return toklist
end,
[TOK.RPAREN] = func(self, tok)
error("Unexpected right parenthesis")
end,
[TOK.INT] = func(self, tok)
return tok
end,
[TOK.BOOL] = func(self, tok)
return tok
end,
[TOK.NAME] = func(self, tok)
return tok
end,
[TOK.QUOTE] = func(self, tok)
self.tok:advance()
tok.quoting = self:generate(0)
return tok
end,
[TOK.EOF] = func(self, tok)
return None
end
}
}
EX = {CALL=1, ASSIGN=2, FUNCDECL=3, SCOPE=4, IFELSE=5, DATUM=6, LIT=7, REF=8, LIST=9}
keys = []
for k in EX do keys:insert(#keys, k) end
for k in keys do EX[EX[k]]=k end
node = {
new = func(type, value)
return {type=type, value=value, __index=node}
end,
pretty = func(self) return self.PRETTY_DISPATCH[self.type](self) end
PRETTY_DISPATCH = {
[EX.CALL] = func(self)
return '<CALL NAME='+(self.value.name)+' ARGS='+tostring(self.value.args:copy():map(func(i) if !(None == i) then return i:pretty() else return tostring(i) end end))+'>'
end,
[EX.ASSIGN] = func(self)
return '<ASSIGN NAME='+(self.value.name)+' VALUE='+(self.value.value:pretty())+'>'
end,
[EX.FUNCDECL] = func(self)
return '<FUNCDECL PARAMS='+tostring(self.value.params)+' BODY='+(self.value.body:pretty())+'>'
end,
[EX.SCOPE] = func(self)
return '<SCOPE '+(self.value:copy():map(func(i) return i:pretty() end))+'>'
end,
[EX.IFELSE] = func(self)
return '<IFELSE COND='+(self.value.cond:pretty())+' IFT='+(self.value.ift:pretty())+' IFF='+(self.value.iff:pretty())+'>'
end,
[EX.DATUM] = func(self)
return '#'+tostring(self.value)
end,
[EX.LIT] = func(self)
if type(self.value) == 'list' then
return '/'+tostring(self.value:copy():map(func(i) return i:pretty() end))
end
return '/'+tostring(self.value)
end,
[EX.REF] = func(self)
--print('In EX.REF, self is', self)
res = '@'+tostring(self.value)
--print('In EX.REF, returning', res)
return res
end,
[EX.LIST] = func(self)
--print('In EX.LIST, self is', self)
return '<LIST '+(self.value:copy():map(func(i) return i:pretty() end))+'>'
end
}
}
parser = {
new = func(ttgen)
return {ttgen = ttgen, __index = parser}
end,
parse = func(self, tt)
if type(tt) == 'map' then
--print('In parse, self is', self)
--print('In parse, tt is', tt)
--print('In parse, dispatch to', self.TT_PARSE_DISPATCH[tt.type])
res = self.TT_PARSE_DISPATCH[tt.type](self, tt)
else
name = tt[0]
if !(name.type == TOK.NAME) then
error('Expected name as first element of expression-list')
end
rest = tt:copy()
rest:remove(0)
sc = self.SCALL_DISPATCH[name.value]
if !(None == sc) then
sc = None
res = self.SCALL_DISPATCH[name.value](self, rest)
else
res = node.new(EX.CALL, {name=name.value, args=rest:map(func(i) return self:parse(i) end)})
end
end
--print('In parse, returning', res:pretty())
return res
end,
TT_PARSE_DISPATCH = {
[TOK.INT] = func(self, tok)
return node.new(EX.LIT, tok.value)
end,
[TOK.BOOL] = func(self, tok)
return node.new(EX.LIT, tok.value)
end,
[TOK.NAME] = func(self, tok)
--print('In TOK.NAME, tok is', tok)
res = node.new(EX.REF, tok.value)
--print('In TOK.NAME, returning', res)
return res
end,
[TOK.QUOTE] = func(self, tok)
return self:parse_datum(tok.quoting)
end
},
SCALL_DISPATCH = {
define = func(self, args)
name = args[0]
if !(name.type == TOK.NAME) then error('Define: expected name as first argument') end
value = self:parse(args[1])
return node.new(EX.ASSIGN, {name=name.value, value=value})
end,
["if"] = func(self, args)
cond = self:parse(args[0])
ift = self:parse(args[1])
iff = self:parse(args[2])
return node.new(EX.IFELSE, {cond=cond, ift=ift, iff=iff})
end,
begin = func(self, args)
args:map(func(i) return self:parse(i) end)
return node.new(EX.LIST, args)
end,
lambda = func(self, args)
--print('Lambda args:', args)
params = args[0]
if !(type(params) == 'list') then error('Lambda: expected parameters as first argument (got '+tostring(params)+')') end
params:map(func(i)
if !(type(i) == 'map') then error('Lambda: expected name token in argument list (got sublist)') end
if !(i.type == TOK.NAME) then error('Lambda: expected name token in argument list (got '+(i:pretty())+')') end
return i.value
end)
body = args:copy()
body:remove(0)
--print('Lambda body:', body)
body:map(func(i) return self:parse(i) end)
return node.new(EX.FUNCDECL, {params=params, body=node.new(EX.LIST, body)})
end,
let = func(self, args)
defs = args[0]
if !(type(defs) == 'list') then error('Let: expected list of bindings are first argument') end
defs:map(func(i)
if !(type(i) == 'list') then error('Let: expected a binding entry') end
return self.SCALL_DISPATCH.define(self, i)
end)
body = args:copy()
body:remove(0)
body:map(func(i) return self:parse(i) end)
return node.new(EX.SCOPE, defs+body)
end,
letrec = func(self, args)
defs = args[0]
if !(type(defs) == 'list') then error('Let: expected list of bindings are first argument') end
defs:map(func(i)
if !(type(i) == 'list') then error('Let: expected a binding entry') end
return self.SCALL_DISPATCH.define(self, i)
end)
body = args:copy()
body:remove(0)
body:map(func(i) return self:parse(i) end)
return node.new(EX.LIST, defs+body)
end
}
parse_datum = func(self, tt)
if type(tt) == 'map' then
return self.TT_PARSE_DATUM_DISPATCH[tt.type](self, tt)
else
list = []
for tok in tt do
list:insert(#list, self:parse_datum(tok))
end
return node.new(EX.LIT, list)
end
end,
TT_PARSE_DATUM_DISPATCH = {
[TOK.INT] = func(self, tok)
return node.new(EX.LIT, tok.value)
end,
[TOK.BOOL] = func(self, tok)
return node.new(EX.LIT, tok.value)
end,
[TOK.NAME] = func(self, tok)
return node.new(EX.LIT, tok.value)
end,
[TOK.QUOTE] = func(self, tok)
return self:parse(tok.quoting)
end
},
run = func(self)
tt = self.ttgen:generate()
list = []
while 1 do
if None == tt then break end
--if type(tt) == 'list' then
list:insert(#list, self:parse(tt))
--end
tt = self.ttgen:generate()
end
--print('In run, list is', list)
return node.new(EX.LIST, list)
end
}
converter = {
new = func(p)
return {parser=p, __index = converter}
end,
make = func(self, node)
--print('In make, node is a', EX[node.type], 'of value', node)
res = self.MAKE_DISPATCH[node.type](self, node)
--print('In make, returning', res)
if type(res) == "astnode" then ast.print(res) end
return res
end,
MAKE_DISPATCH = {
[EX.CALL] = func(self, node)
e = parse('f()').stmtlist[0].expr
e.expr.ident = node.value.name
args = node.value.args:copy():map(func(i) return self:make(i) end)
--print('In EX.CALL, replacement args are', args)
e.args = args
--print('In EX.CALL, args are', e.args)
return e
end,
[EX.ASSIGN] = func(self, node)
e = parse('a = b').stmtlist[0].expr
e.ident = node.value.name
e.value = self:make(node.value.value)
return e
end,
[EX.FUNCDECL] = func(self, node)
e = parse('func() None None end').stmtlist[0].expr
params = node.value.params
--print('In EX.FUNCDECL, params are', params)
e.args = params
--print('In EX.FUNCDECL, args are', e.args)
e.body.stmtlist = self:make(node.value.body)
return e
end,
[EX.SCOPE] = func(self, node)
e = parse('(func() None None end)()').stmtlist[0].expr
node.type = EX.LIST
e.expr.body.stmtlist = self:make(node)
node.type = EX.SCOPE
return e
end,
[EX.IFELSE] = func(self, node)
e = parse('(func() if None then return None else return None end end)()').stmtlist[0].expr
e.expr.body.stmtlist[0].cond = self:make(node.value.cond)
e.expr.body.stmtlist[0].iftrue.stmtlist[0].ret = self:make(node.value.ift)
e.expr.body.stmtlist[0].iffalse.stmtlist[0].ret = self:make(node.value.iff)
return e
end,
[EX.DATUM] = func(self, node) error('EX.DATUM: Not implemented') end,
[EX.LIT] = func(self, node)
if type(node.value) == 'list' then
e = parse('[None]').stmtlist[0].expr
e.list = node.value:copy():map(func(i) return self:make(i) end)
else
e = parse('None').stmtlist[0].expr
if type(node.value) == "int" then
e.littype = ast.LIT_INT
e.ival = node.value
end
if type(node.value) == "string" then
e.littype = ast.LIT_STRING
e.str = node.value
end
end
return e
end,
[EX.REF] = func(self, node)
e = parse('a').stmtlist[0].expr
e.ident = node.value
return e
end,
[EX.LIST] = func(self, node)
e = parse('func() None end').stmtlist[0].expr
l = node.value:copy()
l:map(func(i)
s = parse('None').stmtlist[0]
s.expr = self:make(i)
return s
end)
lastidx = (#l) - 1
r = parse('return None').stmtlist[0]
r.ret = l[lastidx].expr
l[lastidx] = r
--print('In EX.LIST, e is now' e)
e.body.stmtlist = l
return e.body.stmtlist
end
},
run = func(self)
list = self:make(self.parser:run())
res = parse('(func() None None end)()')
--print('In run, list is', list)
--for i in list do ast.print(i) end
res.stmtlist[0].expr.expr.body.stmtlist = list
return res
end
}
_G = debug.globals()
_G['+'] = func(a, b) return a + b end
_G['-'] = func(a, b) return a - b end
_G['*'] = func(a, b) return a * b end
_G['/'] = func(a, b) return a / b end
_G['<'] = func(a, b) return a < b end
_G['>'] = func(a, b) return a > b end
_G['<='] = func(a, b) return a <= b end
_G['>='] = func(a, b) return a >= b end
_G['=='] = func(a, b) return a == b end
_G['eq'] = _G['==']
_G['or'] = func(a, b) return a || b end
_G['and'] = func(a, b) return a && b end

1
programs/subtest.sol

@ -0,0 +1 @@
print("Hello from subtest!")

411
programs/test.sol

@ -0,0 +1,411 @@
print('--- Empty functions')
func f() end
print(f())
print('--- While loop')
a = 1
while a < 10 do
print(a)
a += 1
end
print("--- Range")
func mul9(b)
for i in range(#b) do
b[i] *= 9
end
end
l = [1 2 3 4 5]
print(range(#l))
print("--- Iter list")
for i in l do print(i) end
print("--- Index list")
for i in range(#l) do print(i, l[i]) end
print('--- mul9')
mul9(l)
for i in l do print(i) end
print('--- Iter mul9')
for i in range(#l) do print(i, l[i]) end
print("--- Mapgen")
PI = 3.14159265358979
d = {
integer = 1
string = "hello"
submap = {
stamina = 100
health = 42.0
}
sublist = [1 1 2 3 5 8],
["this time with spaces"] = PI*2,
[1 + 5] = 1 + 5,
[1 + 9] = 1 + 9
}
print(d)
print('--- Map iter')
for i in d do print(i, d[i]) end
print('--- try')
func bad(x)
print(x)
return x.c()
end
test1 = {c = func() return 15 end}
test2 = {}
print(try(bad, test1))
print(try(bad, test2))
print(bad(test1))
--print(bad(test2))
print('--- Induced errors')
func raise(x)
error(x)
end
print(try(raise, "lp0 on fire"))
print('--- Indexing')
print(d["integer"])
print(d.integer)
d.integer += 5
print(d.integer)
print('--- Function binding')
func outer(a)
func inner(b)
return a+b
end
inner.closure.a = a
return inner
end
i = outer(5)
print(i(3), i(4), i(5))
j = outer(8)
print(j(3), j(4), j(5))
print('--- Iterators')
func myiter()
if i>10 then return StopIteration end
i+=1
return i-1
end
myiter.closure.i = 1
for i in myiter do print(i) end
print('--- Method calls')
d = {a = func(a, b) print(a, b) end}
d.a(1, 2)
d:a(3)
print('--- Special methods')
d = {__index = func(obj, key) print('Index', obj, key) return key end,
__setindex = func(obj, key, val) print('SetIndex', obj, key, val) end,
__call = func(obj, arg1, arg2) print('Call', obj, arg1, arg2) return arg1 end}
print(d[3], d[5])
d.a = 7
print(d("q", "r"))
e = {a=1, b=2}
d = {__index = e, __setindex = e}
print(d, d.a, d.b)
d.c = 5
d.b = 7
print(d, e)
print('--- Data sharing')
d = {}
e = [1 2 3 4 5]
d.a = e
d.b = e
print(d)
e[2]=7
e[3]="c"
print(d)
d.a:insert(#(d.a), "q")
d.b:remove(1)
d.a[3]="f"
print(d)
print('--- Arithmetic structure operations')
print('ab'+'cd')
print('l'+'ol'*32)
print([1 2 3]+[4 5])
print([1 2 3]*5)
print({a=1 b=2}+{c=3})
print('--- Map/filter')
l = [1 2 3 4 5]
print(l)
l:map(func (i) return i*3 end)
print(l)
l:filter(func (i) return i & 1 end)
print(l)
print('--- Map/filter chain')
print([1 2 3 4 5]:map(func (i) return i * 3 end):filter(func (i) return i & 1 end))
print('--- Exec/eval')
exec('print("Hello from exec!")')
print(eval('5 + 3'))
execfile('subtest.sol')
print('--- Modulus')
print(5%3)
print(13%5)
print(15%15)
print('--- Special function manipulation')
func foo(x)
return x
end
print(foo)
foo.name = "bar"
print(foo)
func something()
return i
end
something.closure = {i=[1, 2, 3]}
print(something())
cl = something.closure
cl.i:insert(0, "b")
print(something())
print('--- Function body swapping')
func a()
return 0
end
func b()
return 2
end
print(a, a())
print(b, b())
print(a.stmt)
print(b.stmt)
temp = a.stmt
a.stmt = b.stmt
b.stmt = temp
print(a, a())
print(b, b())
newbody = parse('return 4')
print(newbody)
a.stmt = newbody
b.stmt = newbody
print(a, a())
print(b, b())
print('--- More complicated ASTs')
print(outer, outer.stmt, outer.stmt.stmtlist)
print('--- Exec- and eval-by-parse')
parse('print("Hello from parse()!")')()
print(parse('5 + 3').stmtlist[0].expr())
print('--- Mutating ASTs')
func f()
return 5 + 7
end
print(f, f.stmt, f())
f.stmt.stmtlist[0].ret.right.ival = 11
print(f, f.stmt, f())
func g()
a=1
b=2
print("a=", a, ", b=", b)
end
print(g, g.stmt, g())
g.stmt.stmtlist[1].expr.value = parse('a').stmtlist[0].expr
print(g, g.stmt, g())
print('--- AST Environments')
code = parse('print("a is", a, "and b is", b"); a = 4; b = 5')
print(code)
code()
d = {a=1, b=2}
print(d)
code(d)
print(d)
e = {a="hello", b=["world"]}
print(e)
code(e)
print(e)
e = {a=1, b=2}
d = {__index = e}
print(d)
print(e)
code(d)
print(d)
print(e)
print('--- Basic buffers')
print('(buffer.fromstring = ', buffer.fromstring, ')')
b = buffer.fromstring("Hello, world!")
print(b)
print('(b.get = ', b.get, ')')
print(b:get(buffer.type.cstr))
b:set(buffer.type.char, "Q")
b:set(buffer.type.char, "L", 2)
print(b:get(buffer.type.cstr))
print(b:get(buffer.type.cstr, 5))
print(b:get(buffer.type.uint32))
--b:set(buffer.type.double, 1.243416560929)
b:set(buffer.type.uint32, 1886545252)
print(b:get(buffer.type.uint32))
print(b:get(buffer.type.cstr))
q = buffer.fromaddress(b:address(), b:size())
print(q:get(buffer.type.cstr))
q:set(buffer.type.cstr, "Goodbye!")
print(q:get(buffer.type.cstr), b:get(buffer.type.cstr))
s = "A string!"
b = buffer.fromobject(s)
prepr(s)
print('...is a', buffer.objtype[b:get(buffer.type.int, 0)])
print('(buffer.sizeof.ptr = ', buffer.sizeof.ptr, ')')
print('(buffer.sizeof.int = ', buffer.sizeof.int, ')')
print('(buffer.sizeof.int*2 = ', buffer.sizeof.int*2, ')')
print('(buffer.sizeof.int*2 + buffer.sizeof.ptr = ', buffer.sizeof.int*2 + (buffer.sizeof.ptr), ')')
bs = b:get(buffer.type.ptr, buffer.sizeof.int*2 + (buffer.sizeof.ptr))
print('...string buffer:', bs)
print('...with value:', bs:get(buffer.type.cstr))
print('--- IO redirection')
oldstdout = io.stdout
io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
print('A line!')
print('An object:', {a=1, b=2, c="turkey"})
print('Something mysterious :o')
io.stdout:write('Writing directly to a file :D')
io.stdout:flush()
io.stdout = oldstdout
print('...restored stdout.')
f = io.open('stdout', io.MODE_READ)
s = f:read(io.ALL)
print('Buffered output was:')
prepr(s)
f = None
print('...second time.')
io.stdout = io.open('stdout', io.MODE_WRITE|io.MODE_TRUNCATE)
print('Hey there!')
print('l'+'ol'*32)
io.stdout:flush()
io.stdout = oldstdout
print('...restored.')
print('Output was:')
prepr(io.open('stdout', io.MODE_READ):read(io.ALL))
print('--- Substrings')
s = 'This is a test!'
prepr(s)
prepr(s:sub(1, -1))
prepr(s:sub(3, -3))
prepr(s:sub(3, 5))
prepr(s:sub(3, 11))
prepr(s:sub(-1000, -1000))
print('--- Splitting')
s = 'This is a test!'
prepr(s)
prepr(s:split(' '))
prepr(s:split('i'))
prepr(s:split('0'))
prepr(s:split('aeiou'))
l = s:split(' ')
for i in l do
prepr(i, type(i))
end
print('--- Continue/break')
l = range(10)
for i in l do
print(i)
if i >= 5 then break end
end
print('---')
for i in l do
if i%2 == 0 then continue end
print(i)
end
print('--- All done!')

11
programs/test_monty.sol

@ -0,0 +1,11 @@
execfile('monty.sol')
t = tokenizer.new(io.open('/tmp/monty', io.MODE_READ))
ttg = ttreegen.new(t)
p = parser.new(ttg)
c = converter.new(p)
stmt = c:run()
print('Resulting statement:')
ast.print(stmt)
print('---Running results begin here---')
stmt()
io.stdin:read(io.LINE)
Loading…
Cancel
Save