Advertisement
Guest User

Untitled

a guest
Jan 5th, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. -- TUA
  2. -- Tua is a programming language which adds optional static type checking to the Lua programming language. Programs can be written in Tua, typechecked by the Tua compiler,  then compiled into Lua and run anywhere Lua scripts are used. The type checking features in Tua are powerful, but optional, allowing programmers to mix dynamic and static programming styles as they wish. Tua aims to be backwards compatible with existing Lua code. So far i've implemented type checking for primitives, unions, tables, functions (and their arguments and return values), and type inference when declaring variables. Here are some examples of the current syntax:
  3.  
  4. -- Declaring dynamically typed local variables
  5. -- (same syntax and behaviour as ordinary lua):
  6. local a = 1
  7. local b = true
  8. local c = "hello"
  9. local d = {}
  10. local e = { hello = "world" }
  11. local function f( x, y )
  12.     return x + y
  13. end
  14.  
  15. -- Declaring statically typed local variables using explicit types:
  16. local a : number = 1               
  17. local b : boolean = true           
  18. local c : string = "hello"         
  19. local d : table = {}               
  20. local e : { string -> string } = { hello = "world" }
  21. local f : function( number, number ) -> number = function( x : number, y : number )
  22.     return x + y
  23. end
  24.  
  25. -- Declaring statically typed local variables using inferred types:
  26. auto a = 1             
  27. auto b = true
  28. auto c = "hello"           
  29. auto d = {}            
  30. auto e = { hello = "world" }
  31. auto function f( x : number, y : number )
  32.     return x + y
  33. end
  34.  
  35. -- When variables are statically typed, the Tua compiler will emit error messages when they're incorrectly used. Here are some examples of incorrect usage and the corresponding error messages:
  36. a = "hello"     -- Attempt to assign string to number
  37. b = nil         -- Attempt to assign nil to boolean
  38. c["hello"] = 42 -- Attempt to index string value
  39. d()             -- Attempt to call table value             
  40. e.hello = false -- Attempt to assign boolean to string|nil
  41. f( 1, "2" )     -- Attempt to pass string as argument 2 to function( number, number ) -> number
  42. auto s = f( 1, 2 ); s = "hello" -- Attempt to assign string to number
  43.  
  44. -- This project is a work in progress.
  45. -- If you have any feedback or suggestions to share with me, send them to my twitter: @DanTwoHundred, or dratcliffe@gmail.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement