Skip to content

Commit

Permalink
removed typed-var (var<T>) support (use cast!)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Oct 5, 2013
1 parent fafe2e8 commit d43222a
Show file tree
Hide file tree
Showing 60 changed files with 104 additions and 97 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -27,7 +27,7 @@ The Sol compiler is written in Sol.
use{foo: 42, bar: "fortytwo"}

typedef Option = "A" or "B" or "C"
var<Option> x = "B"
var x = "B" : Option


-- Stuff the compiler catches:
Expand Down Expand Up @@ -64,7 +64,7 @@ Based on MetaLua. Cumbersome syntax:

Compard to Sol:

var<number> n = 42
var n = 42 : number

### [Lua analyzer](https://bitbucket.org/kevinclancy/love-studio/wiki/OptionalTypeSystem)

Expand Down
2 changes: 1 addition & 1 deletion build/class.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/class.sol on 2013 Oct 05 08:47:55 --]] function sol_class(klass_name, super_name)
--[[ DO NOT MODIFY - COMPILED FROM sol/class.sol on 2013 Oct 05 08:59:03 --]] function sol_class(klass_name, super_name)
assert(klass_name, "You must specify a class name") --[[SOL OUTPUT--]]
if super_name ~= nil then
Expand Down
2 changes: 1 addition & 1 deletion build/edit_distance.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/edit_distance.sol on 2013 Oct 05 08:47:55 --]] -- From http://nayruden.com/?p=115 - https://gist.github.com/Nayruden/427389
--[[ DO NOT MODIFY - COMPILED FROM sol/edit_distance.sol on 2013 Oct 05 08:59:03 --]] -- From http://nayruden.com/?p=115 - https://gist.github.com/Nayruden/427389
-- Translated to Sol by Emil Ernerfeldt in 2013
--[[
Function: EditDistance
Expand Down
2 changes: 1 addition & 1 deletion build/lexer.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/lexer.sol on 2013 Oct 05 08:47:55 --]] local U = require 'util' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/lexer.sol on 2013 Oct 05 08:59:03 --]] local U = require 'util' --[[SOL OUTPUT--]]
local D = require 'sol_debug' --[[SOL OUTPUT--]]
local set = U.set --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion build/lua_intrinsics.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/lua_intrinsics.sol on 2013 Oct 05 08:47:55 --]] local INTRINSICS = [[
--[[ DO NOT MODIFY - COMPILED FROM sol/lua_intrinsics.sol on 2013 Oct 05 08:59:04 --]] local INTRINSICS = [[
global unpack = extern : function(t: [any]) -> ...
global coroutine = {
Expand Down
2 changes: 1 addition & 1 deletion build/output.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/output.sol on 2013 Oct 05 08:47:55 --]] require 'parser' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/output.sol on 2013 Oct 05 08:59:04 --]] require 'parser' --[[SOL OUTPUT--]]
local D = require 'sol_debug' --[[SOL OUTPUT--]]
local U = require 'util' --[[SOL OUTPUT--]]
local printf_err = U.printf_err --[[SOL OUTPUT--]]
Expand Down
11 changes: 7 additions & 4 deletions build/parser.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/parser.sol on 2013 Oct 05 08:47:55 --]] --
--[[ DO NOT MODIFY - COMPILED FROM sol/parser.sol on 2013 Oct 05 08:59:04 --]] --
-- parse_sol.lua
-- parse_sol taken in a token stream (from the lexer)
-- and outputs an AST.
Expand Down Expand Up @@ -1404,11 +1404,14 @@ local is_mem_fun = (type == 'mem_fun') --[[SOL OUTPUT--]]
local where = where_am_i() --[[SOL OUTPUT--]]
local types = nil --[[SOL OUTPUT--]]
--[[
--parse var<type>
if scoping == 'var' then
types = parse_type_args(scope) --[[SOL OUTPUT--]]
types = parse_type_args(scope)
elseif parse_type_args(scope) then
return false, report_error("%s cannot have type list - did you want 'var' ?") --[[SOL OUTPUT--]]
end --[[SOL OUTPUT--]]
return false, report_error("%s cannot have type list - did you want 'var' ?")
end
--]]
if types and #types == 0 then
return false, report_error("Empty type list") --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion build/scope.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/scope.sol on 2013 Oct 05 08:47:55 --]] local T = require 'type' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/scope.sol on 2013 Oct 05 08:59:04 --]] local T = require 'type' --[[SOL OUTPUT--]]
local D = require 'sol_debug' --[[SOL OUTPUT--]]
local U = require 'util' --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion build/sol.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/sol.sol on 2013 Oct 05 08:47:55 --]] -- For running a .sol without outputting a .lua to disk
--[[ DO NOT MODIFY - COMPILED FROM sol/sol.sol on 2013 Oct 05 08:59:04 --]] -- For running a .sol without outputting a .lua to disk
-- TODO: compiler.sol continaing thing common to sol.sol and solc.sol
local lfs = require 'lfs' --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion build/sol_debug.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/sol_debug.sol on 2013 Oct 05 08:47:55 --]] local D = {} --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/sol_debug.sol on 2013 Oct 05 08:59:04 --]] local D = {} --[[SOL OUTPUT--]]
D.active = false --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion build/solc.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/solc.sol on 2013 Oct 05 08:47:55 --]] --[[
--[[ DO NOT MODIFY - COMPILED FROM sol/solc.sol on 2013 Oct 05 08:59:04 --]] --[[
Command line compiler.
Compiles .sol to .lua, or prints out an error
Expand Down
2 changes: 1 addition & 1 deletion build/type.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/type.sol on 2013 Oct 05 08:47:55 --]] --[[
--[[ DO NOT MODIFY - COMPILED FROM sol/type.sol on 2013 Oct 05 08:59:04 --]] --[[
A type can either be a particular value (number or string) or one of the following.
--]]
Expand Down
4 changes: 2 additions & 2 deletions build/type_check.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/type_check.sol on 2013 Oct 05 08:47:55 --]] local U = require 'util' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/type_check.sol on 2013 Oct 05 08:59:04 --]] local U = require 'util' --[[SOL OUTPUT--]]
local set = U.set --[[SOL OUTPUT--]]
local T = require 'type' --[[SOL OUTPUT--]]
local P = require 'parser' --[[SOL OUTPUT--]]
Expand Down Expand Up @@ -45,7 +45,7 @@ local function loose_lookup(table, id)
local edit_distance = require 'edit_distance' --[[SOL OUTPUT--]]
local closest_dist = math.huge --[[SOL OUTPUT--]]
local closest_dist = math.huge --[[SOL OUTPUT--]]
local closest_key = nil --[[SOL OUTPUT--]]
for k,_ in pairs(table) do
Expand Down
2 changes: 1 addition & 1 deletion build/util.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/util.sol on 2013 Oct 05 08:47:55 --]] --[[
--[[ DO NOT MODIFY - COMPILED FROM sol/util.sol on 2013 Oct 05 08:59:04 --]] --[[
Util.lua
Provides some common utilities shared throughout the project.
Expand Down
2 changes: 1 addition & 1 deletion demo.lua
Expand Up @@ -134,7 +134,7 @@ local ok
= 'yes'
--var<Tribool> bad = 'wrong'
--var bad = 'wrong' : Tribool
local function do_stuff(how)
Expand Down
14 changes: 7 additions & 7 deletions demo.sol
Expand Up @@ -116,9 +116,9 @@ local win, err_msg, too_many = win_or_fail()
-----------------------------------------------------
-- var vs local

local local_can_be_anything = require 'unfindable'
var var_must_be_deducible = some_lua_function()
var<int> var_can_be_explicit = require 'unfindable'
local local_can_be_anything = require 'unfindable'
var var_must_be_deducible = some_lua_function()
var var_can_be_explicit = (require 'unfindable') : int



Expand All @@ -131,7 +131,7 @@ do
list[2] = '1337'

typedef Int2str = {int => string}
var<Int2str> map = {}
var map = {} : Int2str
map[1] = 'one'
map[2] = 'two'
map['three'] = 3
Expand All @@ -144,8 +144,8 @@ end

typedef Tribool = 'yes' or 'no' or 'maybe'

var<Tribool> ok = 'yes'
var<Tribool> bad = 'wrong'
var ok = 'yes' : Tribool
var bad = 'wrong' : Tribool

local function do_stuff(how: "quickly" or "slowly")

Expand Down Expand Up @@ -195,7 +195,7 @@ typedef BarNode : Node = {

local function work_on_node(n: Node)
if n.tag == 'Foo' then
var<FooNode> f = n
var f = n : FooNode
end

var<[string]> a = n.wild
Expand Down
2 changes: 1 addition & 1 deletion headers/edit_distance.sol
@@ -1,3 +1,3 @@
-- Compiled from sol/edit_distance.sol on 2013 Oct 05 08:47:53
-- Compiled from sol/edit_distance.sol on 2013 Oct 05 08:59:02

return function(s: string or [int], t: string or [int], lim: int?) -> int
2 changes: 1 addition & 1 deletion headers/lexer.sol
@@ -1,4 +1,4 @@
-- Compiled from sol/lexer.sol on 2013 Oct 05 08:47:53
-- Compiled from sol/lexer.sol on 2013 Oct 05 08:59:02

return {
-- Types:
Expand Down
2 changes: 1 addition & 1 deletion headers/lua_intrinsics.sol
@@ -1,3 +1,3 @@
-- Compiled from sol/lua_intrinsics.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/lua_intrinsics.sol on 2013 Oct 05 08:59:02

return { add_intrinsics_to_global_scope: function() -> void; }
2 changes: 1 addition & 1 deletion headers/output.sol
@@ -1,3 +1,3 @@
-- Compiled from sol/output.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/output.sol on 2013 Oct 05 08:59:02

return function(ast, filename: string, strip_white_space: bool?) -> string
2 changes: 1 addition & 1 deletion headers/parser.sol
@@ -1,4 +1,4 @@
-- Compiled from sol/parser.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/parser.sol on 2013 Oct 05 08:59:02

return {
-- Types:
Expand Down
2 changes: 1 addition & 1 deletion headers/scope.sol
@@ -1,3 +1,3 @@
-- Compiled from sol/scope.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/scope.sol on 2013 Oct 05 08:59:02

return table
2 changes: 1 addition & 1 deletion headers/sol_debug.sol
@@ -1,4 +1,4 @@
-- Compiled from sol/sol_debug.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/sol_debug.sol on 2013 Oct 05 08:59:02

return {
activate: function() -> void;
Expand Down
2 changes: 1 addition & 1 deletion headers/type.sol
@@ -1,4 +1,4 @@
-- Compiled from sol/type.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/type.sol on 2013 Oct 05 08:59:02

return {
-- Types:
Expand Down
2 changes: 1 addition & 1 deletion headers/type_check.sol
@@ -1,3 +1,3 @@
-- Compiled from sol/type_check.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/type_check.sol on 2013 Oct 05 08:59:02

return function(ast, filename: string, on_require: OnRequireT?, settings) -> true or false, any
2 changes: 1 addition & 1 deletion headers/util.sol
@@ -1,4 +1,4 @@
-- Compiled from sol/util.sol on 2013 Oct 05 08:47:54
-- Compiled from sol/util.sol on 2013 Oct 05 08:59:02

return {
INDENTATION: "\9";
Expand Down
2 changes: 1 addition & 1 deletion install/class.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/class.sol on 2013 Oct 05 08:47:55 --]] function sol_class(klass_name, super_name)
--[[ DO NOT MODIFY - COMPILED FROM sol/class.sol on 2013 Oct 05 08:59:03 --]] function sol_class(klass_name, super_name)
assert(klass_name, "You must specify a class name") --[[SOL OUTPUT--]]
if super_name ~= nil then
Expand Down
2 changes: 1 addition & 1 deletion install/edit_distance.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/edit_distance.sol on 2013 Oct 05 08:47:55 --]] -- From http://nayruden.com/?p=115 - https://gist.github.com/Nayruden/427389
--[[ DO NOT MODIFY - COMPILED FROM sol/edit_distance.sol on 2013 Oct 05 08:59:03 --]] -- From http://nayruden.com/?p=115 - https://gist.github.com/Nayruden/427389
-- Translated to Sol by Emil Ernerfeldt in 2013
--[[
Function: EditDistance
Expand Down
2 changes: 1 addition & 1 deletion install/lexer.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/lexer.sol on 2013 Oct 05 08:47:55 --]] local U = require 'util' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/lexer.sol on 2013 Oct 05 08:59:03 --]] local U = require 'util' --[[SOL OUTPUT--]]
local D = require 'sol_debug' --[[SOL OUTPUT--]]
local set = U.set --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion install/lua_intrinsics.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/lua_intrinsics.sol on 2013 Oct 05 08:47:55 --]] local INTRINSICS = [[
--[[ DO NOT MODIFY - COMPILED FROM sol/lua_intrinsics.sol on 2013 Oct 05 08:59:04 --]] local INTRINSICS = [[
global unpack = extern : function(t: [any]) -> ...
global coroutine = {
Expand Down
2 changes: 1 addition & 1 deletion install/output.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/output.sol on 2013 Oct 05 08:47:55 --]] require 'parser' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/output.sol on 2013 Oct 05 08:59:04 --]] require 'parser' --[[SOL OUTPUT--]]
local D = require 'sol_debug' --[[SOL OUTPUT--]]
local U = require 'util' --[[SOL OUTPUT--]]
local printf_err = U.printf_err --[[SOL OUTPUT--]]
Expand Down
11 changes: 7 additions & 4 deletions install/parser.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/parser.sol on 2013 Oct 05 08:47:55 --]] --
--[[ DO NOT MODIFY - COMPILED FROM sol/parser.sol on 2013 Oct 05 08:59:04 --]] --
-- parse_sol.lua
-- parse_sol taken in a token stream (from the lexer)
-- and outputs an AST.
Expand Down Expand Up @@ -1404,11 +1404,14 @@ local is_mem_fun = (type == 'mem_fun') --[[SOL OUTPUT--]]
local where = where_am_i() --[[SOL OUTPUT--]]
local types = nil --[[SOL OUTPUT--]]
--[[
--parse var<type>
if scoping == 'var' then
types = parse_type_args(scope) --[[SOL OUTPUT--]]
types = parse_type_args(scope)
elseif parse_type_args(scope) then
return false, report_error("%s cannot have type list - did you want 'var' ?") --[[SOL OUTPUT--]]
end --[[SOL OUTPUT--]]
return false, report_error("%s cannot have type list - did you want 'var' ?")
end
--]]
if types and #types == 0 then
return false, report_error("Empty type list") --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion install/scope.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/scope.sol on 2013 Oct 05 08:47:55 --]] local T = require 'type' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/scope.sol on 2013 Oct 05 08:59:04 --]] local T = require 'type' --[[SOL OUTPUT--]]
local D = require 'sol_debug' --[[SOL OUTPUT--]]
local U = require 'util' --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion install/sol.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/sol.sol on 2013 Oct 05 08:47:55 --]] -- For running a .sol without outputting a .lua to disk
--[[ DO NOT MODIFY - COMPILED FROM sol/sol.sol on 2013 Oct 05 08:59:04 --]] -- For running a .sol without outputting a .lua to disk
-- TODO: compiler.sol continaing thing common to sol.sol and solc.sol
local lfs = require 'lfs' --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion install/sol_debug.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/sol_debug.sol on 2013 Oct 05 08:47:55 --]] local D = {} --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/sol_debug.sol on 2013 Oct 05 08:59:04 --]] local D = {} --[[SOL OUTPUT--]]
D.active = false --[[SOL OUTPUT--]]
Expand Down
2 changes: 1 addition & 1 deletion install/solc.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/solc.sol on 2013 Oct 05 08:47:55 --]] --[[
--[[ DO NOT MODIFY - COMPILED FROM sol/solc.sol on 2013 Oct 05 08:59:04 --]] --[[
Command line compiler.
Compiles .sol to .lua, or prints out an error
Expand Down
2 changes: 1 addition & 1 deletion install/type.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/type.sol on 2013 Oct 05 08:47:55 --]] --[[
--[[ DO NOT MODIFY - COMPILED FROM sol/type.sol on 2013 Oct 05 08:59:04 --]] --[[
A type can either be a particular value (number or string) or one of the following.
--]]
Expand Down
4 changes: 2 additions & 2 deletions install/type_check.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/type_check.sol on 2013 Oct 05 08:47:55 --]] local U = require 'util' --[[SOL OUTPUT--]]
--[[ DO NOT MODIFY - COMPILED FROM sol/type_check.sol on 2013 Oct 05 08:59:04 --]] local U = require 'util' --[[SOL OUTPUT--]]
local set = U.set --[[SOL OUTPUT--]]
local T = require 'type' --[[SOL OUTPUT--]]
local P = require 'parser' --[[SOL OUTPUT--]]
Expand Down Expand Up @@ -45,7 +45,7 @@ local function loose_lookup(table, id)
local edit_distance = require 'edit_distance' --[[SOL OUTPUT--]]
local closest_dist = math.huge --[[SOL OUTPUT--]]
local closest_dist = math.huge --[[SOL OUTPUT--]]
local closest_key = nil --[[SOL OUTPUT--]]
for k,_ in pairs(table) do
Expand Down
2 changes: 1 addition & 1 deletion install/util.lua
@@ -1,4 +1,4 @@
--[[ DO NOT MODIFY - COMPILED FROM sol/util.sol on 2013 Oct 05 08:47:55 --]] --[[
--[[ DO NOT MODIFY - COMPILED FROM sol/util.sol on 2013 Oct 05 08:59:04 --]] --[[
Util.lua
Provides some common utilities shared throughout the project.
Expand Down
2 changes: 1 addition & 1 deletion sol/edit_distance.sol
Expand Up @@ -63,7 +63,7 @@ local function edit_distance( s: string or [int], t: string or [int], lim: int?
t = { string.byte( t, 1, t_len ) }
end

var< function(...:int)->int > min = math.min -- Localize for performance
var min = math.min : function(...:int)->int -- Localize for performance
var num_columns = t_len + 1 -- We use this a lot

var d = {} : [int] -- (s_len+1) * (t_len+1) is going to be the size of this array
Expand Down
7 changes: 5 additions & 2 deletions sol/parser.sol
Expand Up @@ -504,7 +504,7 @@ function P.parse_sol(src: string, tok, filename: string?, settings, module_scope
end
end
elseif tok:consume_symbol('...', token_list) then
var<T.Type?> elem_type = T.Any
var elem_type = T.Any

if settings.function_types and tok:consume_symbol(':') then
elem_type = parse_type(func_scope)
Expand Down Expand Up @@ -1060,7 +1060,7 @@ function P.parse_sol(src: string, tok, filename: string?, settings, module_scope
if not tok:consume_symbol(')') then
while true do
if tok:consume_symbol('...') then
var<T.Type> var_arg_t = T.Any
var var_arg_t = T.Any

if tok:consume_symbol(':') then
var_arg_t = parse_type(scope)
Expand Down Expand Up @@ -1404,11 +1404,14 @@ function P.parse_sol(src: string, tok, filename: string?, settings, module_scope
var where = where_am_i()
var types = nil : T.Typelist?

--[[
--parse var<type>
if scoping == 'var' then
types = parse_type_args(scope)
elseif parse_type_args(scope) then
return false, report_error("%s cannot have type list - did you want 'var' ?")
end
--]]

if types and #types == 0 then
return false, report_error("Empty type list")
Expand Down

0 comments on commit d43222a

Please sign in to comment.