Skip to content

Instantly share code, notes, and snippets.

@exerro
exerro / graphics.lua
Last active August 29, 2015 14:21
Graphics.lua
local function linewrap( str, w )
if str:sub( 1, w + 1 ):find "\n" then
return str:match "(.-)\n(.+)"
end
if str:sub( 1, w + 1 ):find "%s" then
local pos = w - str:sub( 1, w + 1 ):reverse():find "%s" + 1
return str:sub( 1, pos), str:sub( pos + 2 ):gsub( "^%s+", "" )
end
return str:sub( 1, w ), str:sub( w + 1 )
--[[
newTextBody(string text)
- creates a new text object with the given starting text
- note: use :format() once created
:format(int i = 1, int j = #lines)
- updates the formatting on the range of lines given
:insert(int n, string line)
{
["editor:TabWidth"] = 4;
}
local function copy( a, b )
if love.filesystem.isDirectory( a ) then
for i, file in ipairs( love.filesystem.getDirectoryItems( a ) ) do
copy( a .. "/" .. file, b .. "/" .. file )
end
else
love.filesystem.write( b, love.filesystem.read( a ) )
end
end
local function isByte( n )
return type( n ) == "number" and n >= 0 and n < 256
end
types.define( "byte", { type = "custom", value = isByte } )
types.define( "optional byte", "byte | nil" )
types.define( "colour", "{ byte, byte, byte, optional byte }" )
print( types.check( { 1, 2 }, "colour" ) ) --> false
Source
int x = math::random()
if x > 5
print( x < 3 )
else
print( x > 7 )
types.parseMany [[
HasPosition: { "source" = string, "line" = number, "character" = number, "strline" = string }
LeftUnaryExpressionOperator: "++" | "--" | "-" | "~" | "#" | "!"
TokenType: "String" | "Integer" | "Number" | "Identifier" | "Symbol" | "Hexadecimal" | "Binary" | "Byte"
ConstantExpressionType: "StringConstant" | "NumberConstant" | "IntegerConstant" | "ByteConstant" | "HexadecimalConstant" | "BinaryConstant"
ThrowExpression: { "type" = "ThrowExpression", "value" = Expression } & HasPosition
Position: { "source" = string, "line" = number, "character" = number, "strline" = string }
HasPosition: { "position" = Position }
ConstantExpressionType: "CharacterConstant" | "StringConstant" | "FloatConstant" | "IntegerConstant" | "ByteConstant" | "HexadecimalConstant" | "BinaryConstant"
ConstantExpression: { "type" = ConstantExpressionType, "value" = string } & HasPosition
ThrowExpression: { "type" = "ThrowExpression", "value" = Expression } & HasPosition
FunctionExpression: { "type" = "FunctionExpression", "returns" = Type | nil, "parameters" = { number = { "name" = string, "class" = Type } } | {}, "body" = Block | nil } & HasPosition
BracketExpression: { "type" = "BracketExpression", "value" = Expression }
local function concat(t, start, finish)
local s = ""
start = start or 1
finish = finish or #t
while start <= finish do
s = s .. t[start]
start = start + 1
local px = BLANK_PIXEL
for i = 1, width * height do
self.pixels[i] = px
end