-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.t
39 lines (32 loc) · 1.09 KB
/
main.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
-- This top-level code is plain Lua code.
function printhello()
-- This is a plain Lua function.
print("Hello, Lua!")
end
printhello()
print()
-- Terra is backwards compatible with C, we'll use C's io
-- library in our example.
local C = terralib.includec("stdio.h")
-- The keyword 'terra' introduces a new Terra function.
terra hello(argc : int, argv : &rawstring)
-- Here we call a C function from Terra.
C.printf("Hello, Terra!\n")
return 0
end
-- You can call Terra functions directly from Lua, they are
-- JIT compiled using LLVM to create machine code.
hello(0,nil)
print()
-- Terra functions are first-class values in Lua, and can be
-- introspected and meta-programmed using it.
print("Dissassembly of hello:")
hello:disas()
print()
-- You can save Terra code as executables, object files, or
-- shared libraries and link them into existing programs.
terralib.saveobj("helloterra",{ main = hello })
print("Running ./helloterra executable:")
os.execute("./helloterra")
-- For more examples and an overview of the Terra language,
-- see: https://terralang.org/getting-started.html