├── .cirrus.yml ├── .clang-format ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .gitlab-ci.yml ├── CHANGES.md ├── CMakeLists.txt ├── CODE_OF_CONDUCT.md ├── LICENSE.txt ├── README.md ├── STABILITY.md ├── TODO.txt ├── appveyor.yml ├── build └── .gitignore ├── cmake └── Modules │ ├── BuildDirs.cmake │ ├── ExtractLLVM.cmake │ ├── FindClang.cmake │ ├── GetLuaJIT.cmake │ └── VersionNumber.cmake ├── default.nix ├── docker ├── Dockerfile.ubuntu ├── Dockerfile.ubuntu-prebuilt-test ├── Dockerfile.ubuntu-source-test ├── Dockerfile.ubuntu-test ├── build.sh ├── compatibility_test.sh ├── install_cuda.sh └── install_script.sh ├── docs ├── CNAME ├── Gemfile ├── Gemfile.lock ├── Makefile ├── _config.yml ├── _gen.lua ├── _layouts │ └── post.html ├── _posts │ └── .gitignore ├── _terraforcpp.txt ├── about.md ├── api.md ├── community.md ├── demo.html ├── getting-started.md ├── index.md ├── javascripts │ ├── codemirror.js │ ├── lua.js │ ├── prism.js │ └── scale.fix.js ├── logo.png ├── pldi071-devito.pdf ├── pldi083-devito.pdf ├── publications.md ├── snapl-devito.pdf ├── stylesheets │ ├── codemirror.css │ ├── prism.css │ └── styles.css └── terraforcpp.html ├── format.sh ├── lib ├── parsing.t └── std.t ├── msvc ├── .gitignore └── ext │ ├── getopt.c │ ├── getopt.h │ ├── getopt_long.c │ ├── inttypes.h │ └── setjmp.h ├── nix └── cflags.patch ├── release ├── include │ └── terra │ │ └── terra.h └── share │ └── terra │ ├── LICENSE.txt │ └── README.md ├── src ├── CMakeLists.txt ├── MSVCSetupAPI.h ├── asdl.lua ├── bin2c.c ├── cudalib.lua ├── dummy.c ├── genheader.lua ├── geninternalizedfiles.lua ├── lctype.cpp ├── lctype.h ├── linenoise.cpp ├── linenoise.h ├── lj_strscan.c ├── lj_strscan.h ├── llex.cpp ├── llex.h ├── llvmheaders.h ├── llvmheaders_110.h ├── llvmheaders_120.h ├── llvmheaders_130.h ├── llvmheaders_140.h ├── llvmheaders_150.h ├── llvmheaders_160.h ├── llvmheaders_170.h ├── llvmheaders_180.h ├── lobject.cpp ├── lobject.h ├── lparser.cpp ├── lparser.h ├── lstring.cpp ├── lstring.h ├── lutil.h ├── lzio.cpp ├── lzio.h ├── main.cpp ├── strict.lua ├── tcompiler.cpp ├── tcompiler.h ├── tcompilerstate.h ├── tcuda.cpp ├── tcuda.h ├── tcwrapper.cpp ├── tcwrapper.h ├── tdebug.cpp ├── tdebug.h ├── terra.cpp ├── terralib.lua ├── terralist.lua ├── terrastate.h ├── tinline.cpp ├── tinline.h ├── tinternalizedfiles.cpp ├── tkind.cpp ├── tkind.h ├── tllvmutil.cpp ├── tllvmutil.h ├── tobj.h ├── treadnumber.c ├── treadnumber.h └── twindows.h ├── terra-scm-1.rockspec ├── tests ├── abouttocompile.t ├── addlanguage1.t ├── addressspace.t ├── aggregatearr.t ├── ainline.t ├── alignment.t ├── amdgpu_kernel.t ├── amdgpu_struct.t ├── and.t ├── anon.t ├── anon3.t ├── anonstruct.t ├── anonstruct2.t ├── antiquote1.t ├── antiquote2.t ├── antiquote3.t ├── antiquote4.t ├── antiquote5.t ├── arith.t ├── array.t ├── arraylit.t ├── arrayt.t ├── arrayt2.t ├── arrptr.t ├── asdlns.t ├── asm.t ├── atoi.t ├── atomicrmw.t ├── avx512.t ├── avxhadd.t ├── badname.t ├── benchmark_dgemm.t ├── benchmark_fannkuchredux.t ├── benchmark_nbody.t ├── benchmarks │ ├── bs.t │ ├── bs_eigen.cpp │ ├── dynfusion.t │ ├── fusion.t │ ├── makefile │ ├── raysphere.t │ ├── raysphere_eigen.cpp │ └── timing.h ├── bf.t ├── blankexp.t ├── blockescape.t ├── blocking.t ├── blocking2-fixed.t ├── blocking2.t ├── blocking3.t ├── boolcast.t ├── bounce.t ├── breaktest.t ├── bug.t ├── bug2.t ├── bug3.t ├── bug372.t ├── bug372_perf.t ├── bug372b.t ├── bug372c.t ├── bug372d.t ├── bug4.t ├── calc.t ├── call.t ├── callbackcache.t ├── canon.t ├── canon2.t ├── cast.t ├── cbool.t ├── cconv.t ├── cconv_array.t ├── cconv_more.t ├── cfgbug.t ├── chaindecl.t ├── clanginfo.t ├── class.t ├── class2.t ├── class3.t ├── class4.t ├── class5.t ├── class6.t ├── classifyfloatstructs.t ├── clean.t ├── cmpxchg.t ├── cnames.t ├── cnamespace.t ├── cnamespaces.t ├── compile_time_array.t ├── compile_time_array2.t ├── compile_time_array3.t ├── compilecallback.t ├── completec.t ├── conflict.t ├── constant.t ├── constant2.t ├── constantinits.t ├── constanttypes.t ├── constructor.t ├── cov ├── coverage.t ├── coverage2.t ├── coverage3.t ├── coverage4.t ├── crash1.t ├── crash2.t ├── cstringtest.t ├── cstruct.t ├── cstruct2.t ├── cudaagg.t ├── cudaaggregate.t ├── cudaatomic.t ├── cudaconst2.t ├── cudaglobal.t ├── cudahello.t ├── cudaoffline.t ├── cudaoo.t ├── cudaprintf.t ├── cudashared.t ├── cudatest.t ├── cudatex.t ├── cunion.t ├── cunion2.t ├── customline.t ├── customtable.t ├── cvar.t ├── declerrors.t ├── decltwice.t ├── decltwicef.t ├── def1.t ├── defaultoperator.t ├── defer.t ├── deferbreak.t ├── defercond.t ├── defercond2.t ├── defergoto.t ├── definewrong.t ├── dgemm.t ├── dgemm2.t ├── dgemm3.t ├── dgemmpaper.t ├── diffuse.t ├── disabled │ ├── crash.t │ ├── cudaldg.t │ ├── fails_recvar3.t │ ├── interface.t │ ├── interface2.t │ ├── leaktest.t │ ├── recvar.t │ ├── recvar2.t │ ├── varinit.t │ ├── varinit2.t │ └── varwithrecinit.t ├── dynlib.t ├── eager.t ├── embeddedcode1.t ├── empty.t ├── emptycalls.t ├── emptyname.t ├── emptystruct.t ├── emptytag.tt ├── enumc.t ├── evenodd.t ├── examplecompiler1.t ├── examplelanguage1.t ├── exampleparser1.t ├── exittest.t ├── explicitcast.t ├── exportdynamic.t ├── expvec.t ├── f2.t ├── fact.t ├── fail.lua ├── fails │ ├── abouttofreeze.t │ ├── arith.t │ ├── atomicrmw.t │ ├── atomicrmw2.t │ ├── atomicrmw3.t │ ├── atomicrmw4.t │ ├── attrload.t │ ├── attrload2.t │ ├── attrload3.t │ ├── attrstore.t │ ├── attrstore2.t │ ├── cast.t │ ├── comparestruct.t │ ├── constructor.t │ ├── defer.t │ ├── defer1.t │ ├── defergoto.t │ ├── defergoto2.t │ ├── dupvar.t │ ├── entrieserror.t │ ├── escapeerr.t │ ├── escapemacro.t │ ├── f0.t │ ├── f1.t │ ├── f10.t │ ├── f11.t │ ├── f12.t │ ├── f13.t │ ├── f14.t │ ├── f15.t │ ├── f16.t │ ├── f17.t │ ├── f18.t │ ├── f19.t │ ├── f2.t │ ├── f20.t │ ├── f21.t │ ├── f22.t │ ├── f23.t │ ├── f24.t │ ├── f25.t │ ├── f26.t │ ├── f27.t │ ├── f28.t │ ├── f29.t │ ├── f3.t │ ├── f30.t │ ├── f31.t │ ├── f32.t │ ├── f33.t │ ├── f34.t │ ├── f35.t │ ├── f36.t │ ├── f37.t │ ├── f38.t │ ├── f39.t │ ├── f4.t │ ├── f40.t │ ├── f41.t │ ├── f43.t │ ├── f44.t │ ├── f45.t │ ├── f46.t │ ├── f47.t │ ├── f48.t │ ├── f5.t │ ├── f7.t │ ├── f8.t │ ├── f9.t │ ├── failtrunc.t │ ├── fnindex.t │ ├── fnpointer.t │ ├── for.t │ ├── for2.t │ ├── forlist.t │ ├── forlist2.t │ ├── forlist3.t │ ├── getmethod.t │ ├── gettype.t │ ├── ifelse.t │ ├── ifelse2.t │ ├── incomplete.t │ ├── intrinsic1.t │ ├── intrinsic2.t │ ├── intrinsic3.t │ ├── intrinsic4.t │ ├── intrinsic5.t │ ├── let.t │ ├── lvaluetreelist.t │ ├── macroselect.t │ ├── metatype.t │ ├── methodantiquote.t │ ├── missingmetamethod.t │ ├── missingmethod.t │ ├── nesterror.t │ ├── nilreturn.t │ ├── nilsym.t │ ├── niltype2.t │ ├── opaque.t │ ├── opaque2.t │ ├── overload.t │ ├── overload3.t │ ├── overload4.t │ ├── quoteescape.t │ ├── quoteescape2.t │ ├── recfn.t │ ├── recpattern.t │ ├── recursivecompile.t │ ├── recursivecompile2.t │ ├── symboltest.t │ ├── symbolvar.t │ ├── symbolvar2.t │ ├── symbolvar3.t │ ├── symbolvar6.t │ ├── symbolvar7.t │ ├── symparam.t │ ├── symparam3.t │ ├── ucall.t │ ├── usercastwrong.t │ ├── veclit.t │ ├── veclit2.t │ ├── veclit3.t │ ├── vectorerror.t │ └── vectype.t ├── fakeasm.t ├── falsespec.t ├── fastcall.t ├── fastmath.t ├── fib.t ├── fib2.t ├── fmod.t ├── fnames.t ├── fncalltest.t ├── fnpointer.t ├── fnptr.t ├── fnptrc.t ├── fnptrstruct.t ├── foo.t ├── for.t ├── for2.t ├── forbreak.t ├── forlist.t ├── forlist2.t ├── forp.t ├── forsym.t ├── forwardtodef.t ├── foundintermediate.t ├── foundintermediate2.t ├── foundintermediate3.t ├── functionnoproto.t ├── gctest.t ├── gemm.t ├── getmethod.t ├── gettype.t ├── globals.t ├── globals_large.t ├── goto.t ├── goto2.t ├── gvarfault.t ├── hasbeenfrozen.t ├── hello.t ├── hello2.t ├── hexf.t ├── huge.t ├── ifelse.t ├── includec.t ├── includetwice.t ├── incomplete.t ├── incomplete2.t ├── incomplete3.t ├── incomplete4.t ├── incomplete5.t ├── incompletetypetest.t ├── incompletetypetest2.t ├── incompletetypetest3.t ├── indexing64.t ├── indexingbug.t ├── inline_c.t ├── intrinsic.t ├── isconstant.t ├── isvolatile.t ├── labelbug.t ├── latelink.t ├── lazycstring.t ├── lazylog.t ├── let1.t ├── let2.t ├── lib │ ├── addlanguage.t │ ├── barlang.t │ ├── def.t │ ├── embeddedcode.t │ ├── examplecompiler.t │ ├── examplelanguage.t │ ├── exampleparser.t │ ├── fakeimport.t │ ├── foolang.t │ ├── golike.t │ ├── javalike.t │ ├── javalikesimple.t │ ├── leak.lua │ ├── matrixtestharness.t │ ├── objc.t │ ├── pratttest.t │ ├── prof.t │ ├── soa.t │ ├── sumlanguage.t │ ├── sumlanguage2.t │ └── testlang.t ├── linklibrary.t ├── linkllvm.t ├── linkllvm2.t ├── list.t ├── localenv.t ├── localenv2.t ├── logical.t ├── luaapi.t ├── luabridge.t ├── luabridge2.t ├── luabridgefn.t ├── luabridgerec.t ├── luabridgeunion.t ├── luaterramethod.t ├── lvaluepointer.t ├── lvaluequote.t ├── lvaluetreelist.t ├── macro.t ├── macro2.t ├── macro3.t ├── macrokey.t ├── macrolet.t ├── macroselect.t ├── macrotest.t ├── malloc.t ├── mathlib.t ├── metatype.t ├── method.t ├── methodantiquote.t ├── methodmissing.t ├── methodrvalue.t ├── methodsugar.t ├── missingfields.t ├── mixed.t ├── multiconstructor.t ├── multimacro.t ├── multiterra.t ├── mytest.h ├── names.t ├── nantest.t ├── nestedcalls.t ├── nestextract.t ├── nestnoerror.t ├── new.t ├── newerrortest.t ├── nillocal.t ├── niltype.t ├── nojit.t ├── nolengthop.t ├── nonprototypec.t ├── nontemporal.t ├── noopt.t ├── noreturn.t ├── numliteral.t ├── objc.t ├── objc2.jpg ├── objc2.t ├── objtest.t ├── offsetcalc.t ├── opaquealloc.t ├── option_e.t ├── or.t ├── ordercomplete.t ├── output.t ├── overload.t ├── overload2.t ├── overload3.t ├── overloadcall.t ├── overloadmethod.t ├── overloadmethod2.t ├── overloadmethod3.t ├── overloadproduct.t ├── overloadrecv.t ├── painfulrecstruct.t ├── paren.t ├── parsecrash.t ├── parsefail.t ├── parsefail2.t ├── pattern.t ├── perfregression.lua ├── point.t ├── pointerarith.t ├── pointerlike.t ├── pow.t ├── ppltalk.t ├── ppnil.t ├── pratttest1.t ├── prec.t ├── prec2.t ├── pretty.t ├── prettyprintsym.t ├── printd.t ├── printfarray.t ├── printfloat.t ├── proxystruct.t ├── pt.t ├── pthreads.t ├── quote.t ├── quote10.t ├── quote2.t ├── quote3.t ├── quote4.t ├── quote5.t ├── quote6.t ├── quote7.t ├── quote8.t ├── quote9.t ├── quoteblock.t ├── quoteenv.t ├── quoteselect.t ├── rd.t ├── receivercasts.t ├── recfn.t ├── recoverfromerror.t ├── recstruct.t ├── recstruct2.t ├── reference │ ├── Makefile │ ├── benchmark_fannkuchredux.c │ ├── benchmark_nbody.c │ ├── matmul.cpp │ └── matmuls.cpp ├── rename.t ├── renaming.t ├── requiretwice.t ├── run ├── rvaluerecv.t ├── scc.t ├── scope.t ├── selectoverload.t ├── setname.t ├── setter.t ├── sgemm-old.t ├── sgemm.t ├── sgemm3.t ├── sgemmkernel.t ├── shallowfreeze.t ├── sharedlib.t ├── shift.t ├── signext.t ├── simple.t ├── simpleadd.t ├── simpleapply.t ├── simpleglobal.t ├── simplerec.t ├── simplestruct.t ├── simplevec.t ├── sintable.t ├── sizetests.t ├── special.t ├── speed.t ├── splitprimary.t ├── splitquote.t ├── ssimple.t ├── staticmethod.t ├── stattest.t ├── stdio.t ├── stencil.t ├── strerror.t ├── string.t ├── stringconst.t ├── struct.t ├── structarg.t ├── structcast.t ├── structconstructor.t ├── structrvalue.t ├── structsyntax.t ├── stuff.t ├── sugar.t ├── sumlanguage1.t ├── sumlanguage2.t ├── switch.t ├── symbolmangling.t ├── symbolvar.t ├── symbolvar2.t ├── symbolvar3.t ├── symbolvar4.t ├── symbolvar5.t ├── symbolvar6.t ├── symbolvar7.t ├── symparam.t ├── symparam2.t ├── symparam3.t ├── tag.tt ├── takedefinition.t ├── template.t ├── terracast.t ├── terralua.t ├── terraluamethod.t ├── terranew.t ├── test.lua ├── testdebug.t ├── testimport.t ├── testlang1.t ├── testlang2.t ├── testlog.t ├── testminmax.t ├── testrequire.t ├── teststd.t ├── testvector.t ├── torturechain.t ├── toterraexpression.t ├── twolang.t ├── typeexp.t ├── union.t ├── unm.t ├── unpacktuple.t ├── unsafesym.t ├── unstrict.t ├── usercast.t ├── ustore.t ├── vararg.t ├── varargcstring.t ├── varargluacall.t ├── varrec.broken ├── vars.t ├── vars2.t ├── vec.t ├── vecarith.t ├── veclit.t ├── vecobj.t ├── vecptr.t ├── vecsize.t ├── version.t ├── vtablerec.t ├── weirdheader.t ├── zeroargs.t ├── zeroreturn.t └── zeroreturn2.t └── travis.sh /.cirrus.yml: -------------------------------------------------------------------------------- 1 | freebsd_task: 2 | name: FreeBSD 3 | freebsd_instance: 4 | matrix: 5 | image_family: freebsd-14-0 6 | env: 7 | matrix: 8 | LLVM_VERSION: 11 9 | LLVM_VERSION: 12 10 | LLVM_VERSION: 13 11 | # LLVM_VERSION: 14 12 | LLVM_VERSION: 15 13 | LLVM_VERSION: 16 14 | LLVM_VERSION: 17 15 | LLVM_VERSION: 18 16 | install_script: pkg install -y bash coreutils cmake gmake llvm$LLVM_VERSION 17 | script: | 18 | export CC=cc 19 | bash travis.sh 20 | 21 | arm_task: 22 | name: Linux AArch64 23 | arm_container: 24 | image: ubuntu:18.04 25 | env: 26 | llvm: 11.1.0 27 | cuda: 0 28 | variant: prebuilt 29 | test: 1 30 | threads: 2 31 | script: | 32 | ./docker/install_script.sh 33 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | Language: Cpp 3 | BasedOnStyle: Google 4 | IndentWidth: 4 5 | ColumnLimit: 90 6 | AccessModifierOffset: -4 7 | ConstructorInitializerIndentWidth: 8 8 | ContinuationIndentWidth: 8 9 | SortIncludes: false # breaks the build when enabled 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /cmake-build-debug 3 | /build 4 | /docs/_site 5 | /docs/_vendor 6 | /install 7 | /release/bin 8 | /release/lib 9 | /release/include/terra/lauxlib.h 10 | /release/include/terra/lua*.h 11 | /terra 12 | /tests/afile.txt 13 | /tests/benchmark_fannkuchredux 14 | /tests/benchmark_nbody 15 | /tests/hello 16 | /tests/output 17 | /tests/output2 18 | /tests/renamed 19 | /tests/speed 20 | /tests/dynlib 21 | /tests/dynlib.exe 22 | /tests/not_bc 23 | /tests/class2 24 | /tests/inline_c.exe 25 | /tests/objc 26 | /tests/objc2 27 | /tests/stdio.exe 28 | 29 | 30 | *.bc 31 | *.ll 32 | *.o 33 | *.so 34 | 35 | *~ 36 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | # This CI configuration is intended to be run on Ascent. See: 2 | # https://code.ornl.gov/ecpcitest/csc335/terra/-/pipelines 3 | 4 | variables: 5 | # workaround for filesystem issues 6 | CUSTOM_CI_BUILDS_DIR: "/gpfs/wolf/proj-shared/csc335/ci/${CI_JOB_ID}" 7 | 8 | build: 9 | tags: 10 | - nobatch 11 | script: 12 | - module load gcc cuda cmake 13 | - wget -nv https://github.com/terralang/llvm-build/releases/download/llvm-14.0.0/clang+llvm-14.0.0-powerpc64le-linux-gnu.tar.xz 14 | - tar xf clang+llvm-14.0.0-powerpc64le-linux-gnu.tar.xz 15 | - export CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH:$PWD/clang+llvm-14.0.0-powerpc64le-linux-gnu 16 | - cd build 17 | - cmake .. -DTERRA_ENABLE_CUDA=1 18 | - make -j8 19 | - ctest -j8 20 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | release/share/terra/LICENSE.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | release/share/terra/README.md -------------------------------------------------------------------------------- /build/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terralang/terra/c62d8e69bd0857968501832401ea16ace981555c/build/.gitignore -------------------------------------------------------------------------------- /cmake/Modules/BuildDirs.cmake: -------------------------------------------------------------------------------- 1 | file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/bin") 2 | file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/lib") 3 | file(MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/include/terra") 4 | -------------------------------------------------------------------------------- /cmake/Modules/VersionNumber.cmake: -------------------------------------------------------------------------------- 1 | if(GIT_FOUND) 2 | execute_process( 3 | COMMAND "${GIT_EXECUTABLE}" describe --tags 4 | WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" 5 | RESULT_VARIABLE HAS_TERRA_VERSION 6 | OUTPUT_VARIABLE TERRA_VERSION 7 | ERROR_QUIET 8 | ) 9 | endif() 10 | 11 | if(HAS_TERRA_VERSION EQUAL 0) 12 | string(STRIP "${TERRA_VERSION}" TERRA_VERSION) 13 | string(REGEX REPLACE "^(release-)" "" 14 | TERRA_VERSION "${TERRA_VERSION}" 15 | ) 16 | else() 17 | set(TERRA_VERSION unknown) 18 | endif() 19 | 20 | set(TERRA_VERSION_DEFINITIONS "TERRA_VERSION_STRING=\"${TERRA_VERSION}\"") 21 | -------------------------------------------------------------------------------- /docker/Dockerfile.ubuntu: -------------------------------------------------------------------------------- 1 | ARG release=18.04 2 | 3 | FROM ubuntu:$release 4 | 5 | ARG llvm=6.0 6 | ARG lua= 7 | ARG static= 8 | ARG slib= 9 | ARG cuda=0 10 | ARG variant=package 11 | ARG test=1 12 | ARG threads=4 13 | 14 | ENV DEBIAN_FRONTEND noninteractive 15 | ENV CI 1 # skip CUDA tests 16 | 17 | COPY . /terra 18 | 19 | RUN cd /terra && ./docker/install_script.sh 20 | -------------------------------------------------------------------------------- /docker/Dockerfile.ubuntu-prebuilt-test: -------------------------------------------------------------------------------- 1 | Dockerfile.ubuntu-source-test -------------------------------------------------------------------------------- /docker/Dockerfile.ubuntu-source-test: -------------------------------------------------------------------------------- 1 | ARG release=18.04 2 | 3 | FROM ubuntu:$release 4 | 5 | ENV DEBIAN_FRONTEND noninteractive 6 | ENV CI 1 # skip CUDA tests 7 | 8 | COPY . /terra_install 9 | 10 | RUN cat /etc/lsb-release && \ 11 | apt-get update -qq && \ 12 | apt-get install -qq build-essential && \ 13 | cd /terra_install/share/terra/tests && \ 14 | /terra_install/bin/terra ./run 15 | -------------------------------------------------------------------------------- /docker/Dockerfile.ubuntu-test: -------------------------------------------------------------------------------- 1 | ARG release=18.04 2 | 3 | FROM ubuntu:$release 4 | 5 | ENV DEBIAN_FRONTEND noninteractive 6 | ENV CI 1 # skip CUDA tests 7 | 8 | COPY . /terra_install 9 | 10 | RUN cat /etc/lsb-release && \ 11 | apt-get update -qq && \ 12 | apt-get install -qq build-essential libedit-dev libncurses5-dev zlib1g-dev libpfm4-dev && \ 13 | cd /terra_install/share/terra/tests && \ 14 | /terra_install/bin/terra ./run 15 | -------------------------------------------------------------------------------- /docker/compatibility_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | distro="$1" 6 | build_version="$2" 7 | test_versions="$3" 8 | arch="$4" 9 | llvm="$5" 10 | lua="$6" 11 | static=1 12 | slib=1 13 | cuda=1 14 | variant="$7" 15 | test=1 16 | threads="$8" 17 | 18 | ./docker/build.sh "${distro}-${build_version}" "$arch" "$llvm" "$lua" "$static" "$slib" "$cuda" "$variant" "$test" "$threads" 19 | 20 | cd terra_install 21 | 22 | for version in $test_versions; do 23 | docker build --build-arg release=$version -t terralang/terra:$distro-$version-test -f ../docker/Dockerfile.$distro${variant:+-}$variant-test . 24 | docker rmi terralang/terra:$distro-$version-test 25 | done 26 | -------------------------------------------------------------------------------- /docker/install_cuda.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | set -x 5 | 6 | sudo_command="$1" 7 | 8 | $sudo_command apt-get update -qq 9 | $sudo_command apt-get install -qq software-properties-common 10 | wget -nv https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin 11 | $sudo_command mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600 12 | $sudo_command apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub 13 | $sudo_command add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /" 14 | $sudo_command apt-get update -qq 15 | $sudo_command apt-get install -qq cuda-compiler-11.6 16 | -------------------------------------------------------------------------------- /docs/CNAME: -------------------------------------------------------------------------------- 1 | terralang.org 2 | 3 | -------------------------------------------------------------------------------- /docs/Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | gem "github-pages", "~> 227" 6 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | 2 | terraforcpp.html: _terraforcpp.txt _gen.lua 3 | ~/terra-jit/terra _gen.lua -------------------------------------------------------------------------------- /docs/_config.yml: -------------------------------------------------------------------------------- 1 | markdown: kramdown 2 | kramdown: 3 | syntax_highlighter_opts: 4 | disable: true 5 | exclude: 6 | - Gemfile 7 | - Gemfile.lock 8 | -------------------------------------------------------------------------------- /docs/_posts/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terralang/terra/c62d8e69bd0857968501832401ea16ace981555c/docs/_posts/.gitignore -------------------------------------------------------------------------------- /docs/about.md: -------------------------------------------------------------------------------- 1 | --- 2 | layout: post 3 | title: About Us 4 | --- 5 | About Us 6 | ======== 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/community.md: -------------------------------------------------------------------------------- 1 | --- 2 | layout: post 3 | title: Community 4 | --- 5 | 6 | [Zulip](https://terralang.zulipchat.com) is used for real time chat and is a great place to ask questions and meet other users. 7 | 8 | The [mailing list](https://mailman.stanford.edu/mailman/listinfo/terralang) is for general discussion and announcements about Terra. 9 | 10 | For bug reports or feature requests, please [create a GitHub issue](https://github.com/terralang/terra/issues). 11 | -------------------------------------------------------------------------------- /docs/javascripts/scale.fix.js: -------------------------------------------------------------------------------- 1 | var metas = document.getElementsByTagName('meta'); 2 | var i; 3 | if (navigator.userAgent.match(/iPhone/i)) { 4 | for (i=0; i 2 | 3 | #define sigsetjmp(jb,ssmf) setjmp(jb) 4 | #define siglongjmp(jb,v) longjmp(jb,v) 5 | #define sigjmp_buf jmp_buf -------------------------------------------------------------------------------- /src/dummy.c: -------------------------------------------------------------------------------- 1 | /* dummy file compiled to extract head path information from clang */ 2 | 3 | int main() { return 0; } -------------------------------------------------------------------------------- /src/tcompiler.h: -------------------------------------------------------------------------------- 1 | #ifndef _tcompiler_h 2 | #define _tcompiler_h 3 | 4 | struct terra_State; 5 | struct terra_CompilerState; 6 | struct TerraTarget; 7 | int terra_compilerinit(struct terra_State* T); 8 | int terra_compilerfree(struct terra_CompilerState* T); 9 | void freetarget(TerraTarget* TT); 10 | 11 | #endif -------------------------------------------------------------------------------- /src/tcuda.h: -------------------------------------------------------------------------------- 1 | #ifndef tcuda_h 2 | #define tcuda_h 3 | 4 | struct terra_State; 5 | int terra_cudainit(struct terra_State* T); 6 | int terra_cudafree(struct terra_State* T); 7 | 8 | #endif -------------------------------------------------------------------------------- /src/tcwrapper.h: -------------------------------------------------------------------------------- 1 | #ifndef t_cwrapper_h 2 | #define t_cwrapper_h 3 | #include "terrastate.h" 4 | 5 | void terra_cwrapperinit(terra_State* T); 6 | 7 | #endif -------------------------------------------------------------------------------- /src/tdebug.h: -------------------------------------------------------------------------------- 1 | #ifndef _tdebug_h 2 | #define _tdebug_h 3 | 4 | struct terra_State; 5 | namespace llvm { 6 | class JITMemoryManager; 7 | } 8 | 9 | int terra_debuginit(struct terra_State* T); 10 | 11 | #endif -------------------------------------------------------------------------------- /src/tinline.h: -------------------------------------------------------------------------------- 1 | #ifndef tinline_h 2 | #define tinline_h 3 | 4 | #include "llvmheaders.h" 5 | 6 | class ManualInliner { 7 | llvm::CallGraphSCCPass *SI; 8 | llvm::CallGraph *CG; 9 | PassManager PM; 10 | 11 | public: 12 | ManualInliner(llvm::TargetMachine *tm, llvm::Module *m); 13 | void run(std::vector::iterator fbegin, 14 | std::vector::iterator fend); 15 | void eraseFunction(llvm::Function *f); 16 | }; 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/tinternalizedfiles.cpp: -------------------------------------------------------------------------------- 1 | // auto-generate files that defines the data for the internalized headers 2 | #include "stdint.h" 3 | #include "internalizedfiles.h" 4 | #include "terra.h" 5 | 6 | void terra_registerinternalizedfiles(lua_State* L, int terratable) { 7 | lua_getfield(L, terratable, "registerinternalizedfiles"); 8 | lua_pushlightuserdata(L, &headerfile_names[0]); 9 | lua_pushlightuserdata(L, &headerfile_contents[0]); 10 | lua_pushlightuserdata(L, &headerfile_sizes[0]); 11 | lua_call(L, 3, 0); 12 | } -------------------------------------------------------------------------------- /src/tkind.cpp: -------------------------------------------------------------------------------- 1 | /* See Copyright Notice in ../LICENSE.txt */ 2 | 3 | #include "terrastate.h" 4 | #include "tkind.h" 5 | #include 6 | extern "C" { 7 | #include "lua.h" 8 | } 9 | static const char* kindtostr[] = { 10 | #define T_KIND_STRING(a, str) str, 11 | T_KIND_LIST(T_KIND_STRING) NULL}; 12 | const char* tkindtostr(T_Kind k) { 13 | assert(k < T_NUM_KINDS); 14 | return kindtostr[k]; 15 | } 16 | 17 | void terra_kindsinit(terra_State* T) { 18 | lua_State* L = T->L; 19 | lua_getfield(L, LUA_GLOBALSINDEX, "terra"); 20 | lua_newtable(L); 21 | for (int i = 0; i < T_NUM_KINDS; i++) { 22 | lua_pushnumber(L, i); 23 | lua_setfield(L, -2, tkindtostr((T_Kind)i)); 24 | } 25 | lua_setfield(L, -2, "kinds"); 26 | lua_pop(L, 1); // terra object 27 | } -------------------------------------------------------------------------------- /src/twindows.h: -------------------------------------------------------------------------------- 1 | #ifndef twindows_h 2 | #define twindows_h 3 | 4 | #pragma pack(push) 5 | #pragma pack(8) 6 | #define WINVER 0x0600 //_WIN32_WINNT_VISTA 7 | #define _WIN32_WINNT 0x0600 8 | #define NTDDI_VERSION 0x06000100 // NTDDI_VISTASP1 9 | #define WIN32_LEAN_AND_MEAN 10 | #ifndef NOMINMAX // Some compilers enable this by default 11 | #define NOMINMAX 12 | #endif 13 | #define NODRAWTEXT 14 | #define NOBITMAP 15 | #define NOMCX 16 | #define NOSERVICE 17 | #define NOHELP 18 | #include 19 | #pragma pack(pop) 20 | 21 | #endif -------------------------------------------------------------------------------- /terra-scm-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "terra" 2 | version = "scm-1" 3 | 4 | source = { 5 | url = "git://github.com/terralang/terra.git", 6 | branch = "master", 7 | } 8 | 9 | description = { 10 | summary = "A low-level counterpart to Lua.", 11 | homepage = "", 12 | license = "MIT", 13 | } 14 | 15 | build = { 16 | type = "command", 17 | build_command = [[ 18 | make clean 19 | libdir=$(LUA_LIBDIR) 20 | LUAJIT_PREFIX=${libdir%/lib} 21 | make LUAJIT_PREFIX=$LUAJIT_PREFIX CXX=clang++ CC=clang LLVM_CONFIG=$(LUA_BINDIR)/llvm-config 22 | ]], 23 | install_command = [[ 24 | make PREFIX=$(PREFIX) install 25 | ]], 26 | } -------------------------------------------------------------------------------- /tests/abouttocompile.t: -------------------------------------------------------------------------------- 1 | struct A { 2 | a : int; 3 | b : int; 4 | } 5 | 6 | function A.metamethods.__getentries(self) 7 | print("GET ENTRIES") 8 | for i,e in ipairs(self.entries) do 9 | e.field = "foo"..e.field 10 | end 11 | return self.entries 12 | end 13 | 14 | terra foo() 15 | var a : A 16 | a.fooa = 3 17 | a.foob = 4 18 | return a.fooa + a.foob 19 | end 20 | 21 | 22 | assert(foo() == 7) -------------------------------------------------------------------------------- /tests/addlanguage1.t: -------------------------------------------------------------------------------- 1 | do 2 | import "lib/addlanguage" 3 | local a = 4 4 | local b = add a,3,4+5,(function() terra one() return a end return one() end)() end 5 | local inception = add add 3,4,a end,4 end 6 | local test = require("test") 7 | 8 | 9 | test.eq(b,20) 10 | test.eq(inception,15) 11 | end 12 | 13 | do 14 | import "lib/addlanguage" 15 | local c = add 4,5 end 16 | assert(9 == c) 17 | end -------------------------------------------------------------------------------- /tests/addressspace.t: -------------------------------------------------------------------------------- 1 | -- Tests of pointers with address spaces. 2 | 3 | -- The exact meaning of this depends on the target, but at least basic 4 | -- code compilation should work. 5 | 6 | local function ptr1(ty) 7 | -- A pointer in address space 1. 8 | return terralib.types.pointer(ty, 1) 9 | end 10 | 11 | terra test(x : &int, y : ptr1(int)) 12 | -- Should be able to do math on pointers with non-zero address spaces: 13 | var a = [ptr1(int8)](y) 14 | var b = a + 8 15 | var c = [ptr1(int)](b) 16 | var d = c - y 17 | y = c 18 | 19 | -- Casts should work: 20 | y = [ptr1(int)](x) 21 | x = [&int](y) 22 | 23 | return d 24 | end 25 | test:compile() 26 | print(test) 27 | -------------------------------------------------------------------------------- /tests/aggregatearr.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | a = terralib.new(int[4]) 4 | 5 | terra foo() 6 | a[3] = 4 7 | end 8 | 9 | foo() 10 | assert(4 == a[3]) 11 | 12 | 13 | terra bar() 14 | a = array(5,6,7,8) 15 | end 16 | 17 | bar() 18 | assert(a[3] == 8) 19 | -------------------------------------------------------------------------------- /tests/ainline.t: -------------------------------------------------------------------------------- 1 | 2 | C = terralib.includec("stdio.h") 3 | terra foo() 4 | C.printf("hello, world\n") 5 | C.printf("hello, world\n") 6 | C.printf("hello, world\n") 7 | C.printf("hello, world\n") 8 | C.printf("hello, world\n") 9 | C.printf("hello, world\n") 10 | C.printf("hello, world\n") 11 | C.printf("hello, world\n") 12 | C.printf("hello, world\n") 13 | C.printf("hello, world\n") 14 | end 15 | 16 | foo:setinlined(true) 17 | 18 | terra bar() 19 | foo() 20 | return 4 21 | end 22 | 23 | bar:compile() 24 | bar:disas() 25 | -------------------------------------------------------------------------------- /tests/alignment.t: -------------------------------------------------------------------------------- 1 | local alignment = 16 2 | local aligned = terralib.aligned 3 | terra foobar(a : &float) 4 | terralib.attrstore(a,terralib.attrload(a+3,{ align = alignment }), { align = alignment }) 5 | end 6 | print(foobar) 7 | foobar:disas() -------------------------------------------------------------------------------- /tests/amdgpu_struct.t: -------------------------------------------------------------------------------- 1 | -- This tests a bug that occurred when using C structs imported from a 2 | -- header file. The structs were associated with a certain target, and 3 | -- if used from a different target could cause issues. 4 | 5 | local c = terralib.includecstring [[ 6 | typedef struct t { 7 | int x; 8 | int y; 9 | } t; 10 | ]] 11 | 12 | terra f() 13 | var s : c.t 14 | s.x = 123 15 | s.y = 456 16 | return s.x + s.y 17 | end 18 | 19 | -- Works on CPU. 20 | print(f()) 21 | assert(f() == 579) 22 | 23 | local arch = 'gfx908' 24 | local amd_target = terralib.newtarget { 25 | Triple = 'amdgcn-amd-amdhsa', 26 | CPU = arch, 27 | FloatABIHard = true, 28 | } 29 | 30 | -- Make sure it also works on AMD GPU. 31 | print(terralib.saveobj(nil, "llvmir", {f=f}, nil, amd_target)) 32 | -------------------------------------------------------------------------------- /tests/and.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | terra foo(a : double, b : double, c : double) : bool 4 | return a < b and b < c 5 | end 6 | 7 | test.eq(foo(1,2,3),true) 8 | test.eq(foo(1,2,1),false) 9 | test.eq(foo(2,1,2),false) -------------------------------------------------------------------------------- /tests/anon.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | local Num = int 4 | 5 | local fib = 6 | terra(a : Num) : Num 7 | var i,c,p = 0,1,1 8 | while i < a do 9 | c,p = c + p,c 10 | i = i + 1 11 | end 12 | return c 13 | end 14 | 15 | function fib2(a) 16 | local i,c,p = 0,1,1 17 | while i < a do 18 | c,p = c + p,c 19 | i = i + 1 20 | end 21 | return c 22 | end 23 | for i = 0,10 do 24 | print(fib(i)) 25 | test.eq(fib(i),fib2(i)) 26 | end -------------------------------------------------------------------------------- /tests/anon3.t: -------------------------------------------------------------------------------- 1 | C,T = terralib.includecstring [[ 2 | struct D { 3 | struct { int a; struct { int c; } c; } b; 4 | } c; 5 | typedef struct {int c;} B; 6 | typedef struct { 7 | struct { int a; } a; 8 | struct { int b; } b; 9 | B c; 10 | } A; 11 | ]] 12 | 13 | terra foo(a : &C.A) 14 | var c = a.a 15 | var b = a.b.b 16 | return c.a + b + a.c.c; 17 | end 18 | foo:compile() -------------------------------------------------------------------------------- /tests/anonstruct.t: -------------------------------------------------------------------------------- 1 | 2 | local C = tuple(int,tuple(int,int)) 3 | terra anon() 4 | var c : tuple(int,tuple(int,int)) = { 1, {2,3} } 5 | var d : C = c 6 | d._0 = 2 7 | return d._0 + c._1._0 + c._1._1 8 | end 9 | 10 | test = require("test") 11 | 12 | test.eq(anon(),7) -------------------------------------------------------------------------------- /tests/anonstruct2.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : &A } 3 | 4 | terra foo() 5 | var a : A 6 | end 7 | foo() -------------------------------------------------------------------------------- /tests/antiquote1.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | function omgfunc() 4 | return 2 5 | end 6 | 7 | terra foo() 8 | return 1 + [{omgfunc()}] + [omgfunc()] 9 | end 10 | 11 | local test = require("test") 12 | test.eq(foo(),5) -------------------------------------------------------------------------------- /tests/antiquote2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | function omgfunc() 4 | return 4 5 | end 6 | 7 | local what = `[ {2,3} ] 8 | terra foo() 9 | return [{3,`[ {2,3} ]}] 10 | end 11 | 12 | local test = require("test") 13 | test.meq({3,2,3},foo()) 14 | -------------------------------------------------------------------------------- /tests/antiquote3.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | function omgfunc() 4 | return 2 5 | end 6 | 7 | a = global(0) 8 | 9 | terra foo() 10 | [quote a = a + 1 end]; 11 | [{quote a = a + 1 end,quote a = a + 1 end}] 12 | return a 13 | end 14 | 15 | local test = require("test") 16 | test.eq(foo(),3) -------------------------------------------------------------------------------- /tests/antiquote4.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo() 4 | var a = 4 5 | return [a] 6 | end 7 | 8 | foo:printpretty() 9 | 10 | local test = require("test") 11 | test.eq(foo(),4) -------------------------------------------------------------------------------- /tests/antiquote5.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | function makeloop(N,body) 4 | return quote 5 | for i = 0, N do 6 | body 7 | end 8 | end 9 | end 10 | 11 | 12 | terra stuff() 13 | var a = 0; 14 | [makeloop(10,quote 15 | a = a + 1 16 | end)] 17 | return a 18 | end 19 | 20 | 21 | print(stuff()) -------------------------------------------------------------------------------- /tests/arraylit.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | var a = array(1,2,3) 4 | return a[0] + a[1] + a[2] 5 | end 6 | 7 | terra foo2() 8 | var a = array(1,2.5,3) 9 | return a[1] 10 | end 11 | terra what() 12 | return 3,4.5 13 | end 14 | local expand = macro(function(a) return {`a._0,`a._1} end) 15 | terra foo3() 16 | var w = what() 17 | var a = array(1,2.5,expand(w)) 18 | return a[3] 19 | end 20 | terra foo4() 21 | var a = array("what","magic","is","this") 22 | return a[1][1] 23 | end 24 | 25 | local test = require("test") 26 | test.eq(foo(),6) 27 | test.eq(foo2(),2.5) 28 | test.eq(foo3(),4.5) 29 | test.eq(foo4(),97) -------------------------------------------------------------------------------- /tests/arrptr.t: -------------------------------------------------------------------------------- 1 | a = &int[4] 2 | a = (&int)[4] 3 | local test = require("test") 4 | test.eq(false,a:ispointer()) 5 | test.eq(true,a:isarray()) -------------------------------------------------------------------------------- /tests/asdlns.t: -------------------------------------------------------------------------------- 1 | 2 | local asdl = require("asdl") 3 | local C = asdl.NewContext() 4 | 5 | C:Define [[ 6 | module A { 7 | B = C(number n, E e, B b, N.What nwhat) 8 | | D 9 | } 10 | E = (number a) 11 | Z = (A.B ab, A.D ad) 12 | module N { 13 | What = () 14 | } 15 | ]] 16 | 17 | print(C.A.C(1,C.E(4),C.A.D,C.N.What())) -------------------------------------------------------------------------------- /tests/asm.t: -------------------------------------------------------------------------------- 1 | local a = ... 2 | if not a then 3 | --force MCJIT 4 | os.execute("../terra -m asm.t true") 5 | return 6 | end 7 | 8 | C = terralib.includec("stdio.h") 9 | 10 | struct Vendor { 11 | maxV : int; 12 | b : int; 13 | c : int; 14 | d : int; 15 | } 16 | 17 | terra foo() 18 | var r = terralib.asm(Vendor,"cpuid","={eax},={ebx},={ecx},={edx},{eax},~{dirflag},~{fpsr},~{flags}",false,0) 19 | r.c,r.d = r.d,r.c 20 | C.printf("%.12s\n", &r.b) 21 | end 22 | foo() 23 | 24 | 25 | terra addone(a : int) 26 | return terralib.asm(int,"addl $$1,$1","=r,0",true,a) 27 | end 28 | assert(addone(3) == 4) -------------------------------------------------------------------------------- /tests/atoi.t: -------------------------------------------------------------------------------- 1 | c = terralib.includec("stdlib.h") 2 | 3 | terra what() 4 | return c.atoi("52") 5 | end 6 | 7 | print(what()) 8 | -------------------------------------------------------------------------------- /tests/avx512.t: -------------------------------------------------------------------------------- 1 | local vec = vector(int8, 64) 2 | terra compute(x: vec, y: vec) 3 | -- vec has a size of 8 * 64 = 512 bits so this operation should compile 4 | -- to a single AVX512 instruction 5 | return x + y 6 | end 7 | compute:compile() 8 | compute:disas() 9 | -------------------------------------------------------------------------------- /tests/badname.t: -------------------------------------------------------------------------------- 1 | --tostring on a type should never error, so we assign it its original name if it does 2 | struct A {} 3 | A.metamethods.__typename = error 4 | print(A) -------------------------------------------------------------------------------- /tests/benchmarks/makefile: -------------------------------------------------------------------------------- 1 | 2 | INCLUDES += -I/Users/research/Documents/eigen 3 | INCLUDES += -I/Users/zdevito/Downloads/eigen-eigen-5097c01bcdc4 4 | default: bs_eigen raysphere_eigen 5 | 6 | clean: 7 | rm bs_eigen 8 | 9 | all: bs_eigen raysphere_eigen 10 | 11 | bs_eigen: bs_eigen.cpp 12 | clang++ -O3 $(INCLUDES) bs_eigen.cpp -o bs_eigen 13 | 14 | raysphere_eigen: raysphere_eigen.cpp 15 | clang++ -O3 $(INCLUDES) raysphere_eigen.cpp -o raysphere_eigen -------------------------------------------------------------------------------- /tests/benchmarks/timing.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | double current_time() { 5 | struct timeval v; 6 | gettimeofday(&v, NULL); 7 | return v.tv_sec + v.tv_usec / 1000000.0; 8 | } 9 | -------------------------------------------------------------------------------- /tests/blankexp.t: -------------------------------------------------------------------------------- 1 | local a = quote 2 | while false do end 3 | end 4 | terra foo() 5 | return a 6 | end 7 | foo() -------------------------------------------------------------------------------- /tests/blockescape.t: -------------------------------------------------------------------------------- 1 | terra foo() 2 | var c = 3 3 | escape 4 | local a = 1 5 | for i = 1,10 do 6 | emit quote c = c + a end 7 | end 8 | end 9 | return c 10 | end 11 | 12 | assert(foo() == 13) 13 | 14 | terra foo2() 15 | return escape emit(1) end 16 | end 17 | 18 | assert(1 == foo2()) 19 | 20 | a = terralib.newlist() 21 | a:insert quote 22 | foo() 23 | end -------------------------------------------------------------------------------- /tests/boolcast.t: -------------------------------------------------------------------------------- 1 | terra f1(x : int64) 2 | return [bool](x) 3 | end 4 | 5 | terra f2(x : int64) 6 | return not [bool](x) 7 | end 8 | 9 | assert(not f1(0)) 10 | assert(f2(0)) 11 | for _, x in ipairs({-2,-1,1,2,3,4,16,256,65536,4294967296}) do 12 | assert(f1(x)) 13 | assert(not f2(x)) 14 | end 15 | -------------------------------------------------------------------------------- /tests/bounce.t: -------------------------------------------------------------------------------- 1 | local io = terralib.includec("stdio.h") 2 | 3 | struct Count { value : int } 4 | function luafn(a) 5 | print("lua:",a.value) 6 | a.value = a.value + 1 7 | terrafn(a) 8 | end 9 | luafn = terralib.cast( &Count -> {},luafn) 10 | terra terrafn(a : &Count) 11 | io.printf("terra: %d\n",a.value) 12 | if a.value < 100 then 13 | luafn(a) 14 | end 15 | return a.value 16 | end 17 | 18 | terra begin() 19 | var c = Count {0} 20 | return terrafn(&c) 21 | end 22 | 23 | local test = require("test") 24 | test.eq(begin(),100) -------------------------------------------------------------------------------- /tests/bug.t: -------------------------------------------------------------------------------- 1 | local f = assert(io.popen("uname", 'r')) 2 | local s = assert(f:read('*a')) 3 | f:close() 4 | 5 | if s~="Darwin\n" then 6 | print("Warning, not running test b/c this isn't a mac") 7 | else 8 | 9 | 10 | local OC = require("lib/objc") 11 | local OCR = terralib.includec("objc/runtime.h", {"-fblocks"}) 12 | 13 | terra main() 14 | var nsobject = OC.NSObject 15 | OCR.objc_allocateClassPair([&OCR.objc_class](nsobject.data),nil,0) 16 | end 17 | 18 | main:compile() 19 | 20 | end -------------------------------------------------------------------------------- /tests/bug2.t: -------------------------------------------------------------------------------- 1 | ffi = require("ffi") 2 | cstdio = terralib.includec("stdio.h") 3 | 4 | local vec4 = &vector(float,4) 5 | 6 | local align = terralib.aligned 7 | terra lol( w : &float, out : &float) 8 | var a = terralib.attrload(vec4(w),{align = 4}) 9 | terralib.attrstore(vec4(out), a, {align = 4}) 10 | end 11 | 12 | dat = ffi.new("float[?]",32) 13 | for i=0,31 do dat[i]=i end 14 | datO = ffi.new("float[?]",32) 15 | 16 | lol:compile() 17 | lol:disas() 18 | 19 | lol(dat, datO) 20 | 21 | -------------------------------------------------------------------------------- /tests/bug3.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local S = terralib.types.newstruct("mystruct") 4 | 5 | struct A { 6 | v : int 7 | } 8 | 9 | S.entries:insert( { type = A, field = "what" } ) 10 | terra foo() 11 | var v : S 12 | return v.what.v 13 | end 14 | 15 | foo:compile() 16 | -------------------------------------------------------------------------------- /tests/bug372.t: -------------------------------------------------------------------------------- 1 | -- Tests fix for bug #372. 2 | 3 | local C = terralib.includecstring [[ 4 | #include "stdio.h" 5 | #include "string.h" 6 | ]] 7 | 8 | struct st 9 | { 10 | length : uint; 11 | values : double; 12 | } 13 | 14 | struct Config 15 | { 16 | union { 17 | FileDir : int8[16], 18 | s : st, 19 | } 20 | } 21 | 22 | terra f() 23 | var config : Config 24 | C.memcpy(&(config.FileDir[0]), "String111222333", 16) 25 | C.printf("orig: %s\n", config.FileDir) 26 | var copy = config 27 | C.printf("copy: %s\n", copy.FileDir) 28 | return C.strcmp(config.FileDir, copy.FileDir) 29 | end 30 | 31 | local test = require("test") 32 | test.eq(f(),0) 33 | -------------------------------------------------------------------------------- /tests/bug372b.t: -------------------------------------------------------------------------------- 1 | -- Tests fix for bug #372. 2 | 3 | local C = terralib.includecstring [[ 4 | #include "stdio.h" 5 | #include "string.h" 6 | 7 | struct Config { 8 | union { 9 | char FileDir[16]; 10 | struct { 11 | unsigned int length; 12 | double values; 13 | } s; 14 | } u; 15 | }; 16 | ]] 17 | 18 | terra f() 19 | var config : C.Config 20 | C.memcpy(&(config.u.FileDir[0]), "String111222333", 16) 21 | C.printf("orig: %s\n", config.u.FileDir) 22 | var copy = config 23 | C.printf("copy: %s\n", copy.u.FileDir) 24 | return C.strcmp(config.u.FileDir, copy.u.FileDir) 25 | end 26 | 27 | local test = require("test") 28 | test.eq(f(),0) 29 | -------------------------------------------------------------------------------- /tests/bug4.t: -------------------------------------------------------------------------------- 1 | c = terralib.includecstring [[ 2 | #include 3 | #include 4 | ]] 5 | struct exception { slug : int8[60]; code : int; msg : int8[960]; } 6 | EXC_INFO = terralib.new(exception) 7 | terra bar() c.memcpy(EXC_INFO.slug + 0, 'foobar', 7); c.printf('%s\n', EXC_INFO.slug + 0); end 8 | bar() 9 | 10 | terra zoo() EXC_INFO.slug[0] = 65; EXC_INFO.slug[1] = 0; c.printf('%s\n', EXC_INFO.slug + 0); end 11 | zoo() 12 | 13 | terra zoo2() EXC_INFO.slug[0] = 65; EXC_INFO.slug[1] = 0; return EXC_INFO.slug[0] end 14 | assert(zoo2() == 65) -------------------------------------------------------------------------------- /tests/callbackcache.t: -------------------------------------------------------------------------------- 1 | 2 | printtwo = terralib.cast({int,int} -> {},print) 3 | printone = terralib.cast({int} -> {},print) 4 | for i = 1,10 do 5 | local terra doprint() 6 | printtwo(1,2) 7 | printone(3) 8 | end 9 | doprint() 10 | end -------------------------------------------------------------------------------- /tests/canon.t: -------------------------------------------------------------------------------- 1 | 2 | struct B { 3 | a : A 4 | } 5 | struct A { 6 | b : &B 7 | } 8 | 9 | 10 | 11 | 12 | struct C { 13 | i : int 14 | } 15 | local U = struct { c : C } 16 | local UP = &U 17 | 18 | 19 | local FP = UP -> int 20 | local FP2 = UP -> int 21 | 22 | local FI = int -> int 23 | local FI2 = int -> int 24 | terra anon() 25 | var b : B 26 | b.a.b = &b 27 | return 4 28 | end 29 | 30 | terra anon2() 31 | var u = U { C {3} } 32 | 33 | var fp : FP, fi : FI 34 | var fi2 : FI2 = fi 35 | var fp2 : FP2 = fp 36 | var up : UP = &u 37 | return up.c.i 38 | end 39 | 40 | 41 | 42 | test = require("test") 43 | 44 | test.eq(anon(),4) 45 | test.eq(anon2(),3) -------------------------------------------------------------------------------- /tests/cast.t: -------------------------------------------------------------------------------- 1 | terra bar(a : int) : int 2 | return a + 1 3 | end 4 | terra foo(a : int) : int 5 | 6 | return a 7 | end 8 | 9 | local test = require("test") 10 | test.eq(foo(2),2) 11 | test.eq(bar(2),3) 12 | -------------------------------------------------------------------------------- /tests/cbool.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | C = terralib.includecstring [[ 4 | _Bool And(_Bool a, _Bool b) { return a && b; } 5 | ]] 6 | 7 | 8 | terra foobar(a : bool, b : bool) 9 | return C.And(a,b) 10 | end 11 | 12 | assert(foobar(false,true) == false) 13 | assert(foobar(true,true) == true) -------------------------------------------------------------------------------- /tests/cfgbug.t: -------------------------------------------------------------------------------- 1 | local struct Type { 2 | field: int 3 | } 4 | 5 | local terra foo(obj: Type) end 6 | foo:setinlined(false) 7 | 8 | local terra bar() 9 | var obj: Type 10 | defer foo(obj) 11 | return 12 | end 13 | 14 | bar() -------------------------------------------------------------------------------- /tests/chaindecl.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local terra terrafoo(a : &Bar) 4 | baz(a:foo()) 5 | end 6 | local struct Bar {} 7 | local terra baz(a : int) 8 | end 9 | terra Bar:foo() return 1 end 10 | local a = 4 11 | 12 | assert(a == 4) 13 | terrafoo:disas() -------------------------------------------------------------------------------- /tests/clanginfo.t: -------------------------------------------------------------------------------- 1 | terralib.includec("stdio.h",{"-v"}) 2 | -------------------------------------------------------------------------------- /tests/class3.t: -------------------------------------------------------------------------------- 1 | local Class = require("lib/javalike") 2 | local C = Class.C 3 | 4 | local Prints = Class.Interface("Prints",{ print = {} -> {} }) 5 | 6 | struct Leaf(Class(nil,Prints)) { 7 | data : int 8 | } 9 | terra Leaf:print() : {} 10 | C.printf("%d\n",self.data) 11 | end 12 | 13 | 14 | struct Node(Class(Leaf)) { 15 | next : &Leaf 16 | } 17 | 18 | terra Node:print() : {} 19 | C.printf("%d\n",self.data) 20 | self.next:print() 21 | end 22 | 23 | Prints:Define() 24 | 25 | terra test() 26 | var a,b = Leaf.alloc(), Node.alloc() 27 | a.data,b.data,b.next = 1,2,a 28 | var p : &Prints.type = b 29 | p:print() 30 | end 31 | 32 | test() 33 | -------------------------------------------------------------------------------- /tests/class4.t: -------------------------------------------------------------------------------- 1 | J = require("lib/javalike") 2 | 3 | 4 | Drawable = J.Interface("Drawable", { draw = {} -> {} }) 5 | 6 | struct Shape(J()) { 7 | foo : int 8 | } 9 | 10 | 11 | struct Square(J(Shape,Drawable)) { 12 | length : int 13 | } 14 | 15 | terra Square:draw() : {} end 16 | 17 | Drawable:Define() 18 | 19 | terra bar() 20 | var a : &Square = Square.alloc() 21 | a:draw() 22 | end 23 | 24 | bar() 25 | -------------------------------------------------------------------------------- /tests/clean.t: -------------------------------------------------------------------------------- 1 | local c = terralib.includec("stdio.h") 2 | 3 | iamclean = macro(function(arg) 4 | return quote 5 | var a = 3 6 | return a,arg 7 | end 8 | end) 9 | 10 | terra doit() 11 | var a = 4 12 | iamclean(a) 13 | end 14 | 15 | local a = doit() 16 | local test = require("test") 17 | test.meq({3,4}, a) -------------------------------------------------------------------------------- /tests/cnames.t: -------------------------------------------------------------------------------- 1 | struct Foo { 2 | c : float; 3 | } 4 | Foo.displayname = "struct.Foo" 5 | terra useFoo() 6 | var a : Foo 7 | a.c = 4.5 8 | return a.c 9 | end 10 | assert(4.5 == useFoo()) 11 | 12 | C = terralib.includecstring [[ 13 | typedef struct { int a; int b; } Foo; 14 | ]] 15 | 16 | terra stuff() 17 | var a : Foo 18 | var b : C.Foo 19 | b.a = 1 20 | b.b = 2 21 | a.c = 4.5 22 | return b.a + b.b + a.c 23 | end 24 | 25 | assert(7.5 == stuff()) 26 | -------------------------------------------------------------------------------- /tests/cnamespace.t: -------------------------------------------------------------------------------- 1 | G,T = terralib.includecstring [[ 2 | typedef struct { 3 | double c; 4 | } A; 5 | struct A { 6 | int b; 7 | }; 8 | ]] 9 | 10 | G2,T2 = terralib.includecstring [[ 11 | struct A; 12 | typedef struct C A; 13 | ]] 14 | 15 | assert(T2.A == T.A) 16 | assert(G2.A ~= T2.A and G2.A ~= G.A) 17 | terra foo() 18 | var a : G.A 19 | var b : T.A 20 | a.c = 4.5 21 | b.b = 4.5 22 | return a.c + b.b 23 | end 24 | assert(8.5 == foo()) -------------------------------------------------------------------------------- /tests/cnamespaces.t: -------------------------------------------------------------------------------- 1 | C,T = terralib.includecstring [[ 2 | 3 | struct Foo { 4 | int a; 5 | }; 6 | typedef int Foo; 7 | typedef struct Foo * Bar; 8 | typedef Foo * Bar2; 9 | Bar bar(struct Foo * a, Bar2 b) { return (Bar)0; } 10 | ]] 11 | 12 | terra what() 13 | var f = T.Foo { 3 } 14 | var a : C.Bar = &f 15 | return a.a 16 | end 17 | 18 | assert(3 == what()) 19 | 20 | C,T = terralib.includecstring [[ 21 | typedef struct { int a; } Foo; 22 | typedef Foo * FooPtr; 23 | int returna(FooPtr a) { return a->a; } 24 | ]] 25 | 26 | terra what2() 27 | var a : C.Foo = C.Foo { 3 } 28 | var ap : C.FooPtr = &a 29 | return C.returna(ap) 30 | end 31 | 32 | assert(3 == what2()) 33 | -------------------------------------------------------------------------------- /tests/compilecallback.t: -------------------------------------------------------------------------------- 1 | 2 | local a = 0 3 | terra bar :: {} -> int 4 | 5 | local foo = macro(function(arg) 6 | assert(({} -> int) == &bar:gettype()) 7 | a = 3 8 | return 3 9 | end) 10 | 11 | terra bar() 12 | return foo() 13 | end 14 | 15 | assert(bar() == 3) 16 | assert(a == 3) -------------------------------------------------------------------------------- /tests/completec.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | struct A { 3 | int a; 4 | }; 5 | static struct A foo; 6 | struct A * getfoo() { 7 | foo.a = 4; 8 | return &foo; 9 | } 10 | ]] 11 | 12 | assert(C.getfoo().a == 4) 13 | 14 | -------------------------------------------------------------------------------- /tests/conflict.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | end 4 | 5 | foo:compile() 6 | terralib.dumpmodule() 7 | 8 | terralib.includecstring [[ 9 | int foo() { 10 | return 4; 11 | } 12 | ]] -------------------------------------------------------------------------------- /tests/constant.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local foo = terralib.constant(terralib.new(int[4],{1,2,3,4})) 4 | 5 | struct A { 6 | a : int; 7 | b : float 8 | } 9 | 10 | local mystr = terralib.new(A,{3,4.5}) 11 | local const = constant(mystr) 12 | 13 | 14 | terra bar() 15 | return foo[3] + mystr.a 16 | end 17 | 18 | terra bar2() 19 | return foo[1] + mystr.b 20 | end 21 | 22 | function wrapper(a) 23 | return a + 1 24 | end 25 | 26 | local p1 = terralib.constant(int -> int, wrapper) 27 | 28 | terra doit() 29 | return p1(3) 30 | end 31 | 32 | local test = require("test") 33 | test.eq(bar(),7) 34 | test.eq(bar2(),6.5) 35 | test.eq(doit(),4) 36 | -------------------------------------------------------------------------------- /tests/constant2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local a = terralib.new(int,3) 4 | local b = terralib.new(int8,4) 5 | local c = terralib.new(int64,5) 6 | local d = terralib.new(float,3.25) 7 | local e = terralib.new(double,4.25) 8 | 9 | f = global(4) 10 | terra foo() 11 | return &f 12 | end 13 | 14 | local pf = foo() 15 | 16 | terra bar() 17 | return a + b + c + d + e + @pf 18 | end 19 | 20 | local test = require("test") 21 | test.eq(bar(),23.5) -------------------------------------------------------------------------------- /tests/constructor.t: -------------------------------------------------------------------------------- 1 | 2 | local b = 1 3 | local dd = "d" 4 | local c = label() 5 | 6 | terra foo() 7 | var a = { _0 = [b], [c] = 2, [dd] = 3, r = 4} 8 | return a._0 + a.[c] + a.d + a.r 9 | end 10 | 11 | local test = require("test") 12 | test.eq(foo(),10) -------------------------------------------------------------------------------- /tests/cov: -------------------------------------------------------------------------------- 1 | #!../terra 2 | 3 | local terralib = io.open("../src/terralib.lua","r") 4 | local lineinfo = loadfile("coverageinfo.lua")() 5 | 6 | local i = 1 7 | for l in terralib:lines() do 8 | local ntimes = lineinfo[i] 9 | 10 | if ntimes then 11 | io.write(string.format("%5d: ",ntimes)) 12 | else 13 | if string.match(l,"^ *%-%-") or 14 | string.match(l,"^ *$") or 15 | string.match(l,"^ *end *$") or 16 | string.match(l,"^ *else *$") or 17 | string.match(l,"^ *local function") then 18 | io.write(" : ") 19 | else 20 | io.write("#####: ") 21 | end 22 | end 23 | io.write(l) 24 | io.write("\n") 25 | i = i + 1 26 | end 27 | -------------------------------------------------------------------------------- /tests/crash1.t: -------------------------------------------------------------------------------- 1 | a = &int -------------------------------------------------------------------------------- /tests/crash2.t: -------------------------------------------------------------------------------- 1 | a = global( struct { a : int } ) -------------------------------------------------------------------------------- /tests/cstringtest.t: -------------------------------------------------------------------------------- 1 | ffi = require 'ffi' 2 | 3 | 4 | ffi.cdef [[ 5 | typedef struct FFIDefined { 6 | int a; 7 | } FFIDefined; 8 | ]] 9 | 10 | C = terralib.includecstring [[ 11 | typedef struct FFIDefined { 12 | int a; 13 | } FFIDefined; 14 | ]] 15 | 16 | r = terralib.new(C.FFIDefined,{}) -------------------------------------------------------------------------------- /tests/cstruct.t: -------------------------------------------------------------------------------- 1 | local C = terralib.includecstring [[ 2 | 3 | struct teststruct { 4 | int idata; 5 | float fdata; 6 | }; 7 | 8 | void makeitlive(struct teststruct * s) {} 9 | 10 | ]] 11 | 12 | 13 | terra foo() 14 | var a : C.teststruct 15 | a.idata = 3 16 | a.fdata = 3.5 17 | return a.idata + a.fdata 18 | end 19 | 20 | assert(foo() == 6.5) -------------------------------------------------------------------------------- /tests/cstruct2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | C = terralib.includecstring [[ 4 | typedef struct { int x; int y; } Point; 5 | Point mkpoint() { Point p; p.x = p.y = 3; return p; } 6 | ]] 7 | 8 | assert(C.mkpoint().x == 3) -------------------------------------------------------------------------------- /tests/cudaconst2.t: -------------------------------------------------------------------------------- 1 | if not terralib.cudacompile then 2 | print("CUDA not enabled, not performing test...") 3 | return 4 | end 5 | if os.getenv("CI") then 6 | print("Running in CI environment without a GPU, not performing test...") 7 | return 8 | end 9 | 10 | local const = cudalib.constantmemory(float, 1) 11 | 12 | local terra kernel(data: &float) 13 | end 14 | 15 | local M = terralib.cudacompile({ 16 | kernel = kernel, 17 | const = const 18 | }) 19 | -------------------------------------------------------------------------------- /tests/cudaoo.t: -------------------------------------------------------------------------------- 1 | if not terralib.cudacompile then 2 | print("CUDA not enabled, not performing test...") 3 | return 4 | end 5 | if os.getenv("CI") then 6 | print("Running in CI environment without a GPU, not performing test...") 7 | return 8 | end 9 | C = terralib.includec("cuda_runtime.h") 10 | 11 | cudalib.linkruntime() 12 | 13 | terra foo() 14 | var stuff : &opaque 15 | C.cudaMalloc(&stuff,sizeof(int)) 16 | return stuff 17 | end 18 | 19 | local a = foo() 20 | 21 | terra blank() end 22 | terralib.cudacompile { blank = blank } 23 | 24 | assert(0 == C.cudaMemset(a,0,4)) 25 | -------------------------------------------------------------------------------- /tests/cunion.t: -------------------------------------------------------------------------------- 1 | local C = terralib.includecstring [[ 2 | 3 | union testunion { 4 | int idata; 5 | float fdata; 6 | }; 7 | 8 | typedef 9 | union { 10 | int a; 11 | float b; 12 | } S; 13 | 14 | void setUnion(union testunion * u, S * s){ 15 | u->idata = 3; 16 | } 17 | int getUnionInt(union testunion * u){ 18 | return u->idata; 19 | } 20 | 21 | ]] 22 | 23 | terra foo() : int 24 | var u : C.testunion 25 | C.setUnion(&u,nil) 26 | var s : C.S 27 | return C.getUnionInt(&u) 28 | end 29 | 30 | 31 | terralib.printraw(C.S) 32 | 33 | local test = require("test") 34 | test.eq(foo(),3) -------------------------------------------------------------------------------- /tests/cunion2.t: -------------------------------------------------------------------------------- 1 | local C = terralib.includecstring [[ 2 | 3 | union testunion { 4 | int idata; 5 | float fdata; 6 | }; 7 | 8 | void setUnionI(union testunion * u){ 9 | u->idata = 3; 10 | } 11 | void setUnionF(union testunion * u){ 12 | u->fdata = 4.f; 13 | } 14 | 15 | ]] 16 | 17 | terra foo() : int 18 | var u : C.testunion 19 | C.setUnionI(&u) 20 | var f = u.idata 21 | C.setUnionF(&u) 22 | var f2 = u.fdata 23 | return f + f2 24 | end 25 | 26 | terra foo2() 27 | var a : C.testunion 28 | a.fdata = -3.0 29 | a.idata = a.idata and not (1 << 31) 30 | return a.fdata 31 | end 32 | 33 | local test = require("test") 34 | test.eq(foo(),7) 35 | test.eq(foo2(),3) -------------------------------------------------------------------------------- /tests/customline.t: -------------------------------------------------------------------------------- 1 | terra foo(c : int, b : int) 2 | terralib.debuginfo("../src/terralib.lua",300) 3 | var a = b + c 4 | terralib.debuginfo("../src/terralib.lua",301) 5 | return a + a 6 | end 7 | 8 | foo(3,4) 9 | 10 | --terralib.dumpmodule() -------------------------------------------------------------------------------- /tests/customtable.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | } 5 | 6 | A.metamethods.__luametatable = { __index = function(self,idx) return 4 end } 7 | A.methods.foo = function() return 5 end 8 | a = terralib.new(A) 9 | 10 | assert(a.foo == 4) -------------------------------------------------------------------------------- /tests/cvar.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | int foo = 4; 3 | const int foo2 = 5; 4 | ]] 5 | terra t() 6 | C.foo = C.foo + 1; 7 | return C.foo + C.foo2 8 | end 9 | assert(t() == 10) -------------------------------------------------------------------------------- /tests/declerrors.t: -------------------------------------------------------------------------------- 1 | function failit(fn,err) 2 | local success,msg = pcall(fn) 3 | if success then 4 | error("failed to fail.",2) 5 | elseif not string.match(msg,err or "Errors reported during") then 6 | error("failed wrong: "..msg,2) 7 | end 8 | end 9 | 10 | 11 | failit(function() 12 | local A = {} 13 | terra A:foo() end 14 | end,"expected a struct") 15 | 16 | 17 | failit(function() 18 | terra A.b() end 19 | end,"failed attempting to index field") 20 | 21 | failit(function() 22 | terra A.b.c() end 23 | end,"failed attempting to index field") 24 | 25 | 26 | failit(function() 27 | local A = 4 28 | struct A.b {} 29 | end,"failed attempting to index field") -------------------------------------------------------------------------------- /tests/decltwice.t: -------------------------------------------------------------------------------- 1 | 2 | terra mything :: int -> int 3 | 4 | terra mybar() 5 | return mything(4) 6 | end 7 | 8 | terra mything(a : int) 9 | return a 10 | end 11 | 12 | do end -- cause an ordering problem 13 | 14 | terra mything :: int -> int 15 | 16 | terra mybar2() 17 | return mything(4) 18 | end 19 | 20 | 21 | terra mything(a : int) return a + 1 end 22 | 23 | assert(mybar() + 1 == mybar2()) -------------------------------------------------------------------------------- /tests/decltwicef.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | local a = {} 4 | local b = a 5 | 6 | terra a.mything :: int -> int 7 | 8 | terra a.mybar() 9 | return mything(4) 10 | end 11 | 12 | terra a.mything(a : int) 13 | return a 14 | end 15 | 16 | --do end -- consider a duplicate definition error? 17 | 18 | terra b.mything :: int -> int 19 | 20 | terra b.mybar2() 21 | return mything(4) 22 | end 23 | 24 | 25 | terra b.mything(a : int) return a + 1 end 26 | 27 | assert(mybar() + 1 == mybar2()) -------------------------------------------------------------------------------- /tests/def1.t: -------------------------------------------------------------------------------- 1 | import "lib/def" 2 | 3 | local c = 3 4 | local a = 4 5 | local b = def(a) a + c 6 | local d = b(10) 7 | 8 | def e(a) a + c 9 | local def f(a) a + c 10 | 11 | print(d, e(10), f(10)) 12 | 13 | local test = require("test") 14 | test.eq(d,13) 15 | test.eq(e(10),13) 16 | test.eq(f(10),13) 17 | -------------------------------------------------------------------------------- /tests/defaultoperator.t: -------------------------------------------------------------------------------- 1 | 2 | local a = "+" 3 | local b = "__sub" 4 | local ops = {4,5} 5 | terra foobar() 6 | return operator(b,operator(a,3,operator("+",ops))) 7 | end 8 | 9 | assert(foobar() == -12) 10 | -------------------------------------------------------------------------------- /tests/deferbreak.t: -------------------------------------------------------------------------------- 1 | C = terralib.includec("stdio.h") 2 | 3 | cur = global(int) 4 | errint = terralib.cast(int -> {},error) 5 | terra d(a : int) 6 | if cur ~= a then 7 | C.printf("found %d while expecting %d\n",a,cur) 8 | errint(cur) 9 | end 10 | cur = cur + 1 11 | C.printf("d%d\n",a) 12 | end 13 | d:setinlined(false) 14 | 15 | terra side(a : int, b : {}) 16 | d(a) 17 | end 18 | 19 | terra doit() 20 | d(0) 21 | for i = 0,10 do 22 | defer d(2*i+2) 23 | if true then 24 | defer d(2*i + 1) 25 | if i == 8 then 26 | break 27 | end 28 | end 29 | end 30 | d(19) 31 | end 32 | cur:set(0) 33 | doit() 34 | doit:printpretty() 35 | 36 | -------------------------------------------------------------------------------- /tests/defercond2.t: -------------------------------------------------------------------------------- 1 | c = global(int,0) 2 | 3 | terra up() 4 | c = c + 1 5 | end 6 | function failit(match,fn) 7 | local success,msg = pcall(fn) 8 | if success then 9 | error("failed to fail.",2) 10 | elseif not string.match(msg,match) then 11 | error("failed wrong: "..msg,2) 12 | end 13 | end 14 | local df = "defer statements are not allowed in conditional expressions" 15 | failit(df,function() 16 | local terra foo() 17 | if [quote defer up() in true end] then end 18 | end 19 | foo:printpretty() 20 | end) -------------------------------------------------------------------------------- /tests/defergoto.t: -------------------------------------------------------------------------------- 1 | C = terralib.includec("stdio.h") 2 | cur = global(int) 3 | errint = terralib.cast(int -> {},error) 4 | 5 | terra d(a : int) 6 | if cur ~= a then 7 | C.printf("found %d while expecting %d\n",a,cur) 8 | errint(cur) 9 | end 10 | cur = cur + 1 11 | C.printf("d%d\n",a) 12 | end 13 | d:setinlined(false) 14 | 15 | terra foo() 16 | d(0) 17 | defer d(14) 18 | var a = 0 19 | ::begin:: 20 | defer d(terralib.select(a == 10,13,a + 1)) 21 | if a >= 10 then 22 | defer d(11) 23 | goto theend 24 | end 25 | a = a + 1 26 | goto begin 27 | ::theend:: 28 | defer d(12) 29 | return a 30 | end 31 | 32 | foo:printpretty() 33 | cur:set(0) 34 | 35 | test = require("test") 36 | test.eq(foo(),10) -------------------------------------------------------------------------------- /tests/definewrong.t: -------------------------------------------------------------------------------- 1 | local r,s = xpcall(function() 2 | local terra a :: int -> int 3 | do end 4 | terra a() end 5 | end,debug.traceback) 6 | 7 | assert(not r and s:match "attempting to define terra function declaration with type") -------------------------------------------------------------------------------- /tests/disabled/crash.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | terra bar(a : &int) 5 | @a = 4 6 | print("hi") 7 | end 8 | terra foo(a : &int) 9 | bar(a) 10 | print("hi") 11 | end 12 | 13 | 14 | bar:setinlined(false) 15 | foo:setinlined(false) 16 | foo(nil) -------------------------------------------------------------------------------- /tests/disabled/fails_recvar3.t: -------------------------------------------------------------------------------- 1 | var a = bar() 2 | terra bar() 3 | return foo() 4 | end 5 | terra foo() : int 6 | return a 7 | end 8 | foo() -------------------------------------------------------------------------------- /tests/disabled/interface.t: -------------------------------------------------------------------------------- 1 | 2 | IO = terralib.includec("stdio.h") 3 | local Class = require("lib/javalike") 4 | 5 | struct A { 6 | a : int 7 | } 8 | 9 | terra A:foo(a : int) : int 10 | return self.a + a 11 | end 12 | 13 | local m = A.methods.foo:getdefinitions()[1] 14 | 15 | HasFoo = Class.interface({ foo = int -> int }) 16 | 17 | Class.implements(A,HasFoo) 18 | 19 | terra hasfoo(a : &HasFoo) 20 | return a:foo(3) 21 | end 22 | 23 | terra testit() 24 | var a = A.alloc() 25 | a.a = 4 26 | return hasfoo(a) + a:foo(5) 27 | end 28 | 29 | print(m:getpointer()) 30 | testit:compile() 31 | print("DONE") 32 | 33 | assert(testit() == 16) -------------------------------------------------------------------------------- /tests/disabled/interface2.t: -------------------------------------------------------------------------------- 1 | 2 | IO = terralib.includec("stdio.h") 3 | local Class = require("lib/javalike") 4 | 5 | HasFoo = Class.interface({ foo = int -> int }) 6 | 7 | struct A { 8 | a : int 9 | } 10 | 11 | terra A:foo(a : int) : int 12 | return 1 13 | end 14 | Class.implements(A,HasFoo) 15 | 16 | struct B {} 17 | Class.extends(B,A) 18 | 19 | 20 | struct C {} 21 | Class.extends(C,B) 22 | 23 | terra C:foo(a : int) : int 24 | return 3 25 | end 26 | 27 | terra hasfoo(a : &HasFoo) 28 | return a:foo(3) 29 | end 30 | 31 | terra testit() 32 | var a = A.alloc() 33 | var b = B.alloc() 34 | var c = C.alloc() 35 | return hasfoo(a) + hasfoo(b) + hasfoo(c) 36 | end 37 | 38 | assert(testit() == 5) -------------------------------------------------------------------------------- /tests/disabled/leaktest.t: -------------------------------------------------------------------------------- 1 | -- Disabled due to non-deterministic failures: https://github.com/terralang/terra/issues/404 2 | 3 | local leak = require "lib.leak" 4 | 5 | function make() 6 | local terra a(x:int) 7 | return x+1 8 | end 9 | 10 | local terra b(x:int) 11 | return a(x) 12 | end 13 | 14 | return b(12),leak.track(b) 15 | end 16 | nan = 0/0 17 | res,t=make() 18 | assert(res == 13) 19 | local gcd, path = leak.find(t) 20 | if gcd then 21 | print(path) 22 | assert(false) 23 | end 24 | 25 | l = leak.track(leak) 26 | foo = 0/0 27 | local f,p = leak.find(l) 28 | assert(f) 29 | assert(p == "$locals.leak") 30 | -------------------------------------------------------------------------------- /tests/disabled/recvar.t: -------------------------------------------------------------------------------- 1 | var a = b 2 | var b = a 3 | 4 | terra foo() 5 | return a 6 | end 7 | foo() -------------------------------------------------------------------------------- /tests/disabled/recvar2.t: -------------------------------------------------------------------------------- 1 | var a = a 2 | 3 | terra foo() 4 | return a 5 | end 6 | foo() -------------------------------------------------------------------------------- /tests/disabled/varinit.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra a(x : int) : int 4 | if x == 0 then 5 | b() 6 | end 7 | return 4 8 | end 9 | 10 | terra b() : int 11 | return a(3) 12 | end 13 | 14 | var global = a(1) 15 | 16 | terra c() : int 17 | var v = a(1) + global 18 | if v == 0 then 19 | return d() 20 | end 21 | return v 22 | end 23 | 24 | terra d() : int 25 | c() 26 | end 27 | 28 | print(c()) 29 | -------------------------------------------------------------------------------- /tests/disabled/varinit2.t: -------------------------------------------------------------------------------- 1 | var b = a + 1 2 | var a = 3 3 | 4 | terra foo() 5 | return b 6 | end 7 | 8 | local test = require("test") 9 | test.eq(foo(),4) -------------------------------------------------------------------------------- /tests/disabled/varwithrecinit.t: -------------------------------------------------------------------------------- 1 | var a = bar(7) 2 | 3 | terra bar(x : int) : int 4 | if x > 0 then 5 | return bar(x - 1) 6 | end 7 | return 0 8 | end 9 | 10 | terra usea() 11 | return a 12 | end 13 | 14 | usea() -------------------------------------------------------------------------------- /tests/eager.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local fn = terra() return 0 end 4 | 5 | for i = 1,10 do 6 | fn = terra() return fn() + 1 end 7 | end 8 | 9 | local test = require("test") 10 | test.eq(fn(),10) -------------------------------------------------------------------------------- /tests/embeddedcode1.t: -------------------------------------------------------------------------------- 1 | import "lib/embeddedcode" 2 | 3 | local c = 3 4 | local a = 4 5 | local b = defexp(a) a + c 6 | local d = b(10) 7 | assert(d == 13) 8 | 9 | 10 | local e = def(x) 11 | return x + c + a 12 | end 13 | local f = e(4) 14 | assert(f == 11) 15 | 16 | local g = deft(x : int) return x + c + a end 17 | g:disas() 18 | assert(g(4) == 11) 19 | 20 | local h = deftexp(x : int) x + c + a 21 | h:disas() 22 | assert(h(5) == 12) 23 | -------------------------------------------------------------------------------- /tests/empty.t: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terralang/terra/c62d8e69bd0857968501832401ea16ace981555c/tests/empty.t -------------------------------------------------------------------------------- /tests/emptycalls.t: -------------------------------------------------------------------------------- 1 | terra f(x : {}) 2 | return x 3 | end 4 | 5 | 6 | print(f({})) 7 | 8 | terra f2(x : {}) 9 | return x,x 10 | end 11 | 12 | print(f2({})._0) 13 | 14 | terra f3(x : {}, a : int) 15 | return a + 1 16 | end 17 | 18 | assert(f3({},3) == 4) 19 | 20 | terra f4(x : {}) 21 | return x,4 22 | end 23 | 24 | a = f4({}) 25 | assert(a._1 == 4) 26 | 27 | terra f5() 28 | return f({}) 29 | end 30 | 31 | f5() -------------------------------------------------------------------------------- /tests/emptyname.t: -------------------------------------------------------------------------------- 1 | struct A {} 2 | A.metamethods.__typename = function() return "" end 3 | struct B {} 4 | B.metamethods.__typename = function() return "" end 5 | print(A:cstring(),B:cstring()) -------------------------------------------------------------------------------- /tests/emptystruct.t: -------------------------------------------------------------------------------- 1 | terra f(x : {}) 2 | return x 3 | end 4 | 5 | print(f({})) 6 | 7 | terra f2(x : {}) 8 | return x,x 9 | end 10 | 11 | print(f2({})) 12 | 13 | terra f3(x : {}, a : int) 14 | return a + 1 15 | end 16 | 17 | assert(f3({},3) == 4) 18 | 19 | terra f4(x : {}) 20 | return x,4 21 | end 22 | 23 | a = f4({}) 24 | assert(a._1 == 4) 25 | 26 | terra f5() 27 | return f({}) 28 | end 29 | 30 | f5() -------------------------------------------------------------------------------- /tests/emptytag.tt: -------------------------------------------------------------------------------- 1 | #!../terra -------------------------------------------------------------------------------- /tests/enumc.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | enum Foo { 3 | A_VALUE = -1, 4 | B_VALUE = 4, 5 | C_VALUE = 2 << 1, 6 | D_VALUE = (1 << 31) - 1, 7 | E_VALUE = (1 << 44) 8 | }; 9 | ]] 10 | assert(C.A_VALUE == -1) 11 | assert(C.B_VALUE == 4) 12 | assert(C.C_VALUE == 4) 13 | assert(C.D_VALUE == 2147483647) 14 | assert(C.E_VALUE == -2147483648) -------------------------------------------------------------------------------- /tests/evenodd.t: -------------------------------------------------------------------------------- 1 | terra iseven(a : uint) : bool 2 | if a == 0 then 3 | return true 4 | else 5 | return isodd(a - 1) 6 | end 7 | end 8 | terra isodd(a : uint) : bool 9 | if a == 0 then 10 | return false 11 | else 12 | return iseven(a - 1) 13 | end 14 | end 15 | 16 | local test = require("test") 17 | test.eq(iseven(3),false) 18 | test.eq(iseven(2),true) 19 | test.eq(isodd(3),true) 20 | test.eq(isodd(2),false) 21 | -------------------------------------------------------------------------------- /tests/examplelanguage1.t: -------------------------------------------------------------------------------- 1 | import "lib/examplelanguage" 2 | 3 | local a,b = 1,2 4 | c = 3 5 | local d = sum a,b,c done 6 | 7 | print(d) -------------------------------------------------------------------------------- /tests/exampleparser1.t: -------------------------------------------------------------------------------- 1 | import "lib.exampleparser" 2 | 3 | P exp -1 + 1 * (2 - 3) 4 | P exp not true or false and 3 5 | P exp a.b.c[d]()(a,b,c) 6 | P exp (a + 3)(4) 7 | P exp {a,3,4, d = 4, [1+3] = 3} 8 | P exp [ 3 + 4 ] 9 | P statement var a,b,c:int = 3 10 | P statement do 11 | if a then b else c end 12 | if a then b end 13 | if a then b elseif c then d end 14 | while 3 + 3 do end 15 | repeat 16 | until a + b 17 | for a,b,c:int = 1,3 do end 18 | for a,b,c:int in 1,3 do end 19 | a = b 20 | a 21 | a,c = d,e 22 | end -------------------------------------------------------------------------------- /tests/exittest.t: -------------------------------------------------------------------------------- 1 | 2 | C = { exit = terralib.externfunction("exit", int -> {}) } 3 | 4 | 5 | terra moo() 6 | C.exit(0) 7 | end 8 | 9 | moo() -------------------------------------------------------------------------------- /tests/explicitcast.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | var a : int64 = 256+3 4 | var c = intptr(&a) 5 | var d = [&int64](c) 6 | var fi = int(true) 7 | var what = @[&int8](&a) 8 | return @d,fi,what 9 | end 10 | local test = require("test") 11 | test.meq({256+3,1,3},foo()) -------------------------------------------------------------------------------- /tests/exportdynamic.t: -------------------------------------------------------------------------------- 1 | terralib.includepath = terralib.includepath .. ";" .. terralib.terrahome .. "/include/terra" 2 | C = terralib.includecstring [[ 3 | #include "lua.h" 4 | #include "lauxlib.h" 5 | ]] 6 | 7 | local s = C.luaL_newstate() 8 | assert(C.lua_gettop(s) == 0) 9 | 10 | -------------------------------------------------------------------------------- /tests/f2.t: -------------------------------------------------------------------------------- 1 | local C = terralib.includecstring [[ 2 | typedef struct { 3 | int a; 4 | int b; 5 | } Foo; 6 | ]] 7 | for i,e in ipairs(C.Foo.entries) do 8 | print(e.field,e.type) 9 | end 10 | 11 | terra useFoo() 12 | var f : C.Foo 13 | f.a = 1 14 | f.b = 2 15 | end 16 | 17 | useFoo() -------------------------------------------------------------------------------- /tests/fact.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | local Num = int 4 | local terra fact(a : Num) : Num 5 | var c,i = 1,1 6 | while i <= a do 7 | c = c * i 8 | i = i + 1 9 | end 10 | return c 11 | end 12 | 13 | 14 | test.eq(fact(1),1) 15 | test.eq(fact(2),2) 16 | test.eq(fact(3),6) -------------------------------------------------------------------------------- /tests/fail.lua: -------------------------------------------------------------------------------- 1 | -- add require("fail") at the beginning of a file 2 | -- makes sure the remainder of the file produces an error 3 | package.loaded["fail"] = true 4 | function failit(fn) 5 | local success,msg = xpcall(fn,debug.traceback) 6 | if success then 7 | error("failed to fail.",2) 8 | elseif not (msg:match "Errors reported during" or msg:match "Error occured while") then 9 | error("failed wrong: "..msg,2) 10 | end 11 | print(msg) 12 | end 13 | local srcfile = debug.getinfo(3).source:sub(2) 14 | failit(assert(terralib.loadfile(srcfile))) 15 | return false -------------------------------------------------------------------------------- /tests/fails/abouttofreeze.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | struct A { 3 | a : int; 4 | b : int; 5 | } 6 | 7 | function A.metamethods.__finalizelayout(self) 8 | self:freeze() 9 | end 10 | 11 | terra foo() 12 | var a : A 13 | a.fooa = 3 14 | a.foob = 4 15 | return a.fooa + a.foob 16 | end 17 | 18 | 19 | assert(foo() == 7) 20 | -------------------------------------------------------------------------------- /tests/fails/arith.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra test5() 3 | var d = 1.f ^ 2.f 4 | var e = 3.75 ^ 2.0 5 | return (d == 1.f) and (e == 1.75) 6 | end 7 | 8 | 9 | local test = require("test") 10 | 11 | test.eq(test5(),0) 12 | -------------------------------------------------------------------------------- /tests/fails/atomicrmw.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | return terralib.atomicrmw(&a,4) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/atomicrmw2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | return terralib.atomicrmw("add", &a, 4, {}) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/atomicrmw3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | return terralib.atomicrmw("add", &a, true, {ordering = "monotonic"}) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/atomicrmw4.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = true 6 | return terralib.atomicrmw("add", &a, true, {ordering = "monotonic"}) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/attrload.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | return terralib.attrload(&a,4) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/attrload2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | return terralib.attrload(a,{}) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/attrload3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra f(x : &int) 5 | terralib.attrload(x, {align = "asdf"}) 6 | end 7 | f:compile() 8 | -------------------------------------------------------------------------------- /tests/fails/attrstore.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | terralib.attrstore(&a,4,4) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/attrstore2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | terralib.attrstore(a,4,{}) 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/cast.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var b = 3 6 | var a = [double] b 7 | end 8 | 9 | foo:compile() 10 | -------------------------------------------------------------------------------- /tests/fails/comparestruct.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | struct S {} 3 | terra foo() 4 | var a : S, b : S 5 | return a == b 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/constructor.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | local b = 1 4 | local dd = "d" 5 | local c = label() 6 | 7 | terra foo() 8 | var a = { [b], [c] = 2, [dd] = 3, r = 4} 9 | return a._0 + a.[c] + a.d + a.r 10 | end 11 | 12 | local test = require("test") 13 | test.eq(foo(),10) 14 | -------------------------------------------------------------------------------- /tests/fails/defer.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo() 3 | defer 3 4 | end 5 | foo() 6 | -------------------------------------------------------------------------------- /tests/fails/defer1.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | bar = macro(function() return 2 end) 3 | terra foo() 4 | defer bar() 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/defergoto.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra d(a : int) 3 | end 4 | d:setinlined(false) 5 | 6 | terra foo() 7 | ::what2:: 8 | if true then 9 | defer d(1) 10 | ::what:: 11 | end 12 | if true then 13 | goto what 14 | end 15 | end 16 | foo:compile() 17 | -------------------------------------------------------------------------------- /tests/fails/defergoto2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra d(a : int) 3 | end 4 | d:setinlined(false) 5 | 6 | terra foo() 7 | if true then 8 | defer d(1) 9 | ::what:: 10 | end 11 | defer d(3) 12 | if true then 13 | goto what 14 | end 15 | end 16 | foo:compile() 17 | -------------------------------------------------------------------------------- /tests/fails/dupvar.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a = 3 6 | var a = 4 7 | return a 8 | end 9 | 10 | print(foo()) 11 | -------------------------------------------------------------------------------- /tests/fails/entrieserror.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | struct A {} 3 | function A.metamethods.__getentries(self) 4 | return 0 5 | end 6 | 7 | terra foo() 8 | var a : A 9 | end 10 | 11 | foo() 12 | -------------------------------------------------------------------------------- /tests/fails/escapeerr.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | function bar() 5 | return foo + 1 6 | end 7 | 8 | struct A { 9 | a : foo + 1 10 | } 11 | 12 | struct B { 13 | a : bar() 14 | } 15 | 16 | terra foobar() 17 | var a : A 18 | var b : B 19 | end 20 | 21 | foobar:compile() 22 | -------------------------------------------------------------------------------- /tests/fails/escapemacro.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | thea = nil 4 | function store(a) 5 | thea = a 6 | return a 7 | end 8 | function load() 9 | return thea 10 | end 11 | store = macro(store) 12 | load = macro(load) 13 | 14 | terra nonsense() 15 | for i = 0,10 do 16 | var a = i 17 | store(a) 18 | end 19 | return load() 20 | end 21 | 22 | print(nonsense()) 23 | -------------------------------------------------------------------------------- /tests/fails/f0.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo() 3 | return a 4 | end 5 | foo() 6 | -------------------------------------------------------------------------------- /tests/fails/f1.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a : 1 + "a" 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f10.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a = [ int[2] ]({0, a = 3}).a 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f11.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a : struct { a : int, b : int } = { a = 1, b = 2, c = 3} 5 | return a.a 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f12.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a = { a = 1, a = 2, c = 3} 5 | return a.a 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f13.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a : struct { a : int, b : int } = {1, a = 2} 5 | return a.a 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f14.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a : struct { a : int, b : int } = {b = 1, a = 1,3} 5 | return a.a 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f15.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra bar(a : int) return a end 4 | terra foo() 5 | var a : &int = 7 6 | var d = bar(a) 7 | end 8 | foo() 9 | -------------------------------------------------------------------------------- /tests/fails/f16.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo(c : int) 4 | var a : int8 = int8(&c) 5 | end 6 | foo(1) 7 | -------------------------------------------------------------------------------- /tests/fails/f17.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return true + 1 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f18.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return nil + 1 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f19.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return -false 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo() 3 | foo() 4 | end 5 | foo() 6 | -------------------------------------------------------------------------------- /tests/fails/f20.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return niltype + "a" 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f21.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return nil + "a" 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f22.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | (3) = 4 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f23.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | @(3) = 4 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f24.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return 3 >> 4.4 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f25.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a,b,c = 3,4 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f26.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a,b,c = 3,4,6,7 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f27.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return niltype(8) 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f28.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a = 8 5 | return a(8) 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f29.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a = 8 5 | return a(8) 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo() 3 | var a : tuple(int) 4 | return a._1 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f30.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra bar() end 4 | 5 | terra foo() 6 | return bar() + 3 7 | end 8 | foo() 9 | -------------------------------------------------------------------------------- /tests/fails/f31.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a : struct {} 5 | return a.b + 4 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f32.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a : int 5 | return a.b + 4 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f33.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra bar() 4 | end 5 | 6 | bar:compile() 7 | local what = bar.rawjitptr 8 | 9 | terra foo() 10 | var a = what 11 | end 12 | foo() 13 | -------------------------------------------------------------------------------- /tests/fails/f34.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | aa = 4 3 | aa = nil 4 | terra foo() 5 | return aa 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f35.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local function aa() end 3 | 4 | terra foo() 5 | return aa.a 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f36.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local aa = { b = 4} 3 | terra foo() 4 | return aa.a 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f37.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return 4 .. 4 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f38.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a : int[2] 5 | return a[3.3] 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f39.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return (4)[4] 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f4.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | struct A { a : int, a : int } 3 | terra foo() 4 | var a : A 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f40.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return { [3] = 4 } 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f41.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local a = {} 3 | terra foo() 4 | return a + 20 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f43.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | while 1 do end 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f44.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | ::a:: 5 | ::a:: 6 | end 7 | foo() 8 | -------------------------------------------------------------------------------- /tests/fails/f45.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | break 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f46.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a,b,c 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f47.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | goto a 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/f48.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | if true then 5 | return 1,3 6 | else 7 | return 1,2,3 8 | end 9 | end 10 | foo() 11 | -------------------------------------------------------------------------------- /tests/fails/f5.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo(a : int, a : int) 3 | 4 | end 5 | foo(2,4) 6 | -------------------------------------------------------------------------------- /tests/fails/f7.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terralib.fulltrace = true 3 | struct B { a : A[4] } 4 | struct A { b : B } 5 | 6 | terra foo() 7 | var a : A 8 | end 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/f8.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() : {int,1} 4 | end 5 | foo() 6 | -------------------------------------------------------------------------------- /tests/fails/f9.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() : 3 4 | end 5 | foo() 6 | -------------------------------------------------------------------------------- /tests/fails/failtrunc.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() : {} 4 | return (foo()),4 5 | end 6 | foo() 7 | -------------------------------------------------------------------------------- /tests/fails/fnindex.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | end 6 | 7 | terra bar() 8 | return foo + 1 9 | end 10 | 11 | bar() 12 | -------------------------------------------------------------------------------- /tests/fails/fnpointer.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | return 4LL 5 | end 6 | 7 | terra bar() 8 | return 5LL 9 | end 10 | 11 | terra bar3(fn: {} -> int64) 12 | return fn() 13 | end 14 | 15 | terra baz(a : int64) 16 | var afn = foo 17 | if a > 2 then 18 | afn = bar 19 | end 20 | return bar3(@afn) 21 | end 22 | 23 | local test = require("test") 24 | test.eq(baz(1),4) 25 | test.eq(baz(3),5) 26 | -------------------------------------------------------------------------------- /tests/fails/for.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo(a : uint64, s : uint64) 4 | var r = 0 5 | for i = 0.0,a,s do 6 | r = r + i 7 | end 8 | return r 9 | end 10 | 11 | 12 | 13 | local test = require("test") 14 | test.eq(foo(10,1),45) 15 | test.eq(foo(10,2),20) 16 | -------------------------------------------------------------------------------- /tests/fails/for2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo(a : uint64, s : uint64) 3 | var r = 0 4 | for i = "a",a,s do 5 | r = r + i 6 | end 7 | return r 8 | end 9 | 10 | 11 | 12 | local test = require("test") 13 | test.eq(foo(10,1),45) 14 | test.eq(foo(10,2),20) 15 | -------------------------------------------------------------------------------- /tests/fails/forlist.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | for i in 3 do 6 | end 7 | end 8 | 9 | foo() 10 | -------------------------------------------------------------------------------- /tests/fails/forlist2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | struct Range {} 4 | Range.metamethods.__for = function(iter,body) 5 | return body() 6 | end 7 | 8 | terra foo() 9 | for i in Range {} do 10 | end 11 | end 12 | 13 | foo() 14 | -------------------------------------------------------------------------------- /tests/fails/forlist3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | struct Range {} 4 | Range.metamethods.__for = function(iter,body) 5 | return body 6 | end 7 | 8 | terra foo() 9 | for i in Range {} do 10 | end 11 | end 12 | 13 | foo() 14 | -------------------------------------------------------------------------------- /tests/fails/getmethod.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | struct A {} 5 | 6 | function A.metamethods.__getmethod() 7 | error("nope!") 8 | end 9 | 10 | terra foo() 11 | var a : A 12 | return a:bar() 13 | end 14 | 15 | foo() 16 | -------------------------------------------------------------------------------- /tests/fails/gettype.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | 5 | function makestuff(a) 6 | local T = a:gettype() 7 | local struct ST { 8 | a : T 9 | } 10 | return `ST { a } 11 | end 12 | 13 | makestuff = macro(makestuff) 14 | 15 | terra useit() 16 | return makestuff(true).a,makestuff(3.0).a,makestuff(makestuff).a 17 | end 18 | 19 | local a,b = useit() 20 | assert(a == true) 21 | assert(b == 3) 22 | -------------------------------------------------------------------------------- /tests/fails/ifelse.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo(a : int) 4 | return terralib.select(a > 0, vector(1,1),"a") 5 | end 6 | 7 | print(foo(1),foo(-1)) 8 | -------------------------------------------------------------------------------- /tests/fails/ifelse2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo(a : int) 4 | return terralib.select(vector(true,false), vector(3,4,5),-1)[0] 5 | end 6 | 7 | print(foo(1),foo(-1)) 8 | -------------------------------------------------------------------------------- /tests/fails/incomplete.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | struct A 3 | 4 | terra foo2(a : &A) 5 | return a.b 6 | end 7 | 8 | foo2:compile() 9 | -------------------------------------------------------------------------------- /tests/fails/intrinsic1.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local sqrt = terralib.intrinsic("llvm.sqrt.f32",float -> float) 3 | local sqrt2 = terralib.intrinsic("llvm.sqrt.v4f32",vector(float,4) -> vector(float,4)) 4 | 5 | local sqrt3 = terralib.intrinsic(function(types) 6 | if #types == 1 and types[1]:isvector() and types[1].type == float then 7 | local N = types[1].N 8 | return ("llvm.sqrt.v%df32"):format(N), vector(float,N) -> vector(float,N) 9 | elseif #types == 1 and types[1] == float then 10 | return int,float -> float 11 | end 12 | end) 13 | 14 | terra foo() 15 | var v = vector(1.f,2.f,3.f,4.f) 16 | var c = sqrt2(vector(1,2,3,4)) 17 | return sqrt(4) + c[3] + sqrt3(v)[3] + sqrt3(4.f) 18 | end 19 | 20 | local test = require("test") 21 | 22 | test.eq(foo(),8) 23 | -------------------------------------------------------------------------------- /tests/fails/intrinsic3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local sqrt = terralib.intrinsic("llvm.sqrt.f32",float -> float) 3 | local sqrt2 = terralib.intrinsic("llvm.sqrt.v4f32",vector(float,4) -> vector(float,4)) 4 | 5 | local sqrt3 = terralib.intrinsic(function(types) 6 | if #types == 1 and types[1]:isvector() and types[1].type == float then 7 | local N = types[1].N 8 | return ("llvm.sqrt.v%df32"):format(N), vector(float,N) -> vector(float,N) 9 | elseif #types == 1 and types[1] == float then 10 | return "llvm.sqrt.f32",float 11 | end 12 | end) 13 | 14 | terra foo() 15 | var v = vector(1.f,2.f,3.f,4.f) 16 | var c = sqrt2(vector(1,2,3,4)) 17 | return sqrt(4) + c[3] + sqrt3(v)[3] + sqrt3(4.f) 18 | end 19 | 20 | local test = require("test") 21 | 22 | test.eq(foo(),8) 23 | -------------------------------------------------------------------------------- /tests/fails/intrinsic4.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local sqrt = terralib.intrinsic("llvm.sqrt.f32",float -> float) 3 | local sqrt2 = terralib.intrinsic("llvm.sqrt.v4f32",vector(float,4) -> vector(float,4)) 4 | 5 | local sqrt3 = terralib.intrinsic(function(types) 6 | if #types == 1 and types[1]:isvector() and types[1].type == float then 7 | local N = types[1].N 8 | return ("llvm.sqrt.v%df32"):format(N), vector(float,N) -> vector(float,N) 9 | elseif #types == 1 and types[1] == float then 10 | return "llvm.sqrt.f32",float -> {} 11 | end 12 | end) 13 | 14 | terra foo() 15 | var v = vector(1.f,2.f,3.f,4.f) 16 | var c = sqrt2(vector(1,2,3,4)) 17 | return sqrt(4) + c[3] + sqrt3(v)[3] + sqrt3(4.f) 18 | end 19 | 20 | local test = require("test") 21 | 22 | test.eq(foo(),8) 23 | -------------------------------------------------------------------------------- /tests/fails/intrinsic5.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local sqrt = terralib.intrinsic("llvm.sqrt.f32",float -> float) 3 | local sqrt2 = terralib.intrinsic("llvm.sqrt.v4f32",vector(float,4) -> vector(float,4)) 4 | 5 | local sqrt3 = terralib.intrinsic(function(types) 6 | if #types == 1 and types[1]:isvector() and types[1].type == float then 7 | local N = types[1].N 8 | return ("llvm.sqrt.v%df32"):format(N), vector(float,N) -> vector(float,N) 9 | elseif #types == 1 and types[1] == float then 10 | return "llvm.sqrt.f32",float -> float 11 | end 12 | end) 13 | 14 | terra foo() 15 | var v = vector(1.f,2.f,3.f,4.f) 16 | var c = sqrt2(vector(1,2,3,4),5) 17 | return sqrt(4) + c[3] + sqrt3(v)[3] + sqrt3(4.f) 18 | end 19 | 20 | local test = require("test") 21 | 22 | test.eq(foo(),8) 23 | -------------------------------------------------------------------------------- /tests/fails/let.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local c = symbol(int) 3 | local b = 4 | quote 5 | var [c] = 3 6 | in 7 | c,c+1 8 | end 9 | 10 | terra f3() 11 | var what = (b) 12 | return ([c]) 13 | end 14 | assert(f3() == 3) 15 | -------------------------------------------------------------------------------- /tests/fails/lvaluetreelist.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | 5 | terra notlvalues() 6 | var a = 1 7 | a,a,[quote in a,4 end] = 5,6,7,8 8 | end 9 | 10 | notlvalues:compile() 11 | -------------------------------------------------------------------------------- /tests/fails/macroselect.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | struct A { a : int, b : int } 4 | 5 | local c = 1 6 | terra foo() 7 | var a = A {1,2} 8 | return a.[c] 9 | end 10 | 11 | local test = require("test") 12 | test.eq(foo(),2) 13 | -------------------------------------------------------------------------------- /tests/fails/metatype.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local function foo(T) 3 | error("what") 4 | end 5 | struct A(foo) { 6 | a : int; 7 | b : int; 8 | } 9 | 10 | -------------------------------------------------------------------------------- /tests/fails/methodantiquote.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | --the zero line 3 | M = {} 4 | struct M.B {a : int, b : int} 5 | 6 | terra M.B:foo(a : int) 7 | return self.a + a 8 | end 9 | 10 | 11 | local avar = 2 12 | 13 | terra bar() 14 | var b : M.B = { 1,2 } 15 | return b:[avar](3) 16 | end 17 | 18 | test = require("test") 19 | test.eq(bar(),4) 20 | -------------------------------------------------------------------------------- /tests/fails/missingmetamethod.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | struct A { 4 | } 5 | 6 | terra foo() 7 | var a : A 8 | a() 9 | end 10 | 11 | foo() 12 | -------------------------------------------------------------------------------- /tests/fails/missingmethod.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | struct A { 4 | } 5 | 6 | terra foo() 7 | var a : A 8 | a:bar() 9 | end 10 | 11 | foo() 12 | -------------------------------------------------------------------------------- /tests/fails/nesterror.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foobar() 5 | return (1):foo() 6 | end 7 | 8 | terra noerror() 9 | foobar() 10 | return (1):foo() 11 | end 12 | 13 | 14 | noerror() 15 | -------------------------------------------------------------------------------- /tests/fails/nilreturn.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo() : bar 3 | end 4 | -------------------------------------------------------------------------------- /tests/fails/nilsym.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo() 3 | var [what] = 4 4 | end 5 | -------------------------------------------------------------------------------- /tests/fails/niltype2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra bar() 5 | var a : A 6 | end 7 | 8 | bar:compile() 9 | -------------------------------------------------------------------------------- /tests/fails/opaque.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo() 5 | var a : &opaque = nil 6 | return a + 1 7 | end 8 | foo() 9 | -------------------------------------------------------------------------------- /tests/fails/opaque2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra what(a : opaque) 5 | end 6 | 7 | 8 | what:compile() 9 | -------------------------------------------------------------------------------- /tests/fails/overload.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo(a : int) 4 | return 1 5 | end 6 | 7 | terra foo(a : &int8) 8 | return 2 9 | end 10 | 11 | terra doit() 12 | foo(array(3)) 13 | end 14 | 15 | print(doit()) 16 | -------------------------------------------------------------------------------- /tests/fails/overload3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | struct A { a : int } 4 | struct B { a : int, b : int} 5 | 6 | terra foo(a : A) 7 | return 1 8 | end 9 | 10 | terra foo(a : B) 11 | return 2 12 | end 13 | 14 | terra doit() 15 | return foo({1}) + foo({1,2}) 16 | end 17 | 18 | local test = require("test") 19 | test.eq(doit(),4) 20 | -------------------------------------------------------------------------------- /tests/fails/overload4.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo(a : int) 5 | end 6 | terra foo(a : &int8) 7 | end 8 | 9 | 10 | terra doit() 11 | var a = foo(3) 12 | end 13 | 14 | doit() 15 | -------------------------------------------------------------------------------- /tests/fails/quoteescape.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | q = nil 5 | 6 | foo = macro(function(a) 7 | q = a 8 | return `0 9 | end) 10 | 11 | 12 | terra bar() 13 | var a = 8 14 | return foo(a) 15 | end 16 | 17 | terra baz() 18 | return q 19 | end 20 | 21 | print(bar()) 22 | print(baz()) 23 | -------------------------------------------------------------------------------- /tests/fails/quoteescape2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | q = nil 5 | foo = macro(function(a) 6 | q = a 7 | return `0 8 | end) 9 | 10 | 11 | terra bar() 12 | var b : int 13 | do 14 | var a = 8 15 | b = foo(a) 16 | end 17 | return q + foo(b) 18 | end 19 | 20 | terra baz() 21 | return q 22 | end 23 | 24 | bar:printpretty() 25 | print(bar()) 26 | -------------------------------------------------------------------------------- /tests/fails/recfn.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local terra foo() : {} 3 | bar() 4 | end 5 | nop = 1 6 | local 7 | terra bar() : {} 8 | foo() 9 | end 10 | 11 | 12 | foo:printpretty() 13 | -------------------------------------------------------------------------------- /tests/fails/recpattern.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | foo = global(2) 4 | terra a() : int 5 | b() 6 | c() 7 | return 1 8 | end 9 | 10 | terra b() : int 11 | a() 12 | return 2 13 | end 14 | 15 | terra c() : {} 16 | return start() + foo 17 | end 18 | 19 | terra start() : int 20 | a() 21 | return 1 22 | end 23 | 24 | start() 25 | -------------------------------------------------------------------------------- /tests/fails/recursivecompile.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra bar() : {} 5 | foo() 6 | end 7 | terra foo() : {} 8 | bar(); 9 | [ bar 10 | :compile() ] 11 | end 12 | 13 | print(foo()) 14 | -------------------------------------------------------------------------------- /tests/fails/recursivecompile2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra bar() : {} 5 | foo() 6 | end 7 | 8 | terra baz() 9 | bar() 10 | end 11 | terra foo() : {} 12 | bar(); 13 | [ baz 14 | :compile() ] 15 | end 16 | 17 | print(foo()) 18 | -------------------------------------------------------------------------------- /tests/fails/symboltest.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | local e = label() 5 | A = terralib.types.newstruct("A") 6 | A.entries:insert({ field = "a", type = int}) 7 | A.entries:insert({ field = e, type = int }) 8 | 9 | 10 | local b = label() 11 | A.methods[b] = terra(self : &A) return 3 end 12 | 13 | local f = label() 14 | 15 | terra foo() 16 | var a : A 17 | a.[e] = 3 18 | a.[f] = 4 19 | return a:[b]() + a.[e] 20 | end 21 | 22 | local test = require("test") 23 | test.eq(foo(),6) 24 | -------------------------------------------------------------------------------- /tests/fails/symbolvar.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | local a = "a" 5 | 6 | terra foo() 7 | var [a] = 3 8 | return [a] 9 | end 10 | 11 | local test = require("test") 12 | test.eq(3,foo()) 13 | -------------------------------------------------------------------------------- /tests/fails/symbolvar2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | local a = symbol(int) 5 | 6 | local q = quote 7 | [a] = [a] + 1 8 | end 9 | 10 | terra foo() 11 | q 12 | return [a] 13 | end 14 | 15 | local test = require("test") 16 | test.eq(3,foo()) 17 | -------------------------------------------------------------------------------- /tests/fails/symbolvar3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | local a = symbol(int) 5 | 6 | local q = quote 7 | var [a] = 2 8 | [a] = [a] + 1 9 | end 10 | 11 | terra foo() 12 | do 13 | q 14 | end 15 | return [a] 16 | end 17 | 18 | local test = require("test") 19 | test.eq(3,foo()) 20 | -------------------------------------------------------------------------------- /tests/fails/symbolvar6.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local a = symbol(double) 3 | local b = {3} 4 | 5 | local c = symbol(int) 6 | local d = {} 7 | 8 | 9 | terra foo() 10 | var [a],[b] = 1.25,3 11 | var [c],[d] = 3.25 12 | return [a] + [c] 13 | end 14 | 15 | local test = require("test") 16 | 17 | test.eq(foo(),4.25) 18 | -------------------------------------------------------------------------------- /tests/fails/symbolvar7.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | local a = symbol(double) 3 | local b = { symbol(int), symbol(int) } 4 | 5 | local c = symbol(double) 6 | local d = { symbol(double), symbol(double) } 7 | 8 | terra foo() 9 | var [a],[b] = 1.25,1.25,1.25 10 | var [c],[d] = 3.25,3.25 11 | return [a] + [b[1]] + [b[2]] + [c] + [d] 12 | end 13 | 14 | local test = require("test") 15 | 16 | test.eq(foo(),9.50) 17 | -------------------------------------------------------------------------------- /tests/fails/symparam.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | local a = label() 4 | terra foo([a], b : int) 5 | return [a] + b 6 | end 7 | 8 | local test = require("test") 9 | 10 | test.eq(foo(1,2),3) 11 | -------------------------------------------------------------------------------- /tests/fails/symparam3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | local a = symbol(int) 4 | local c = {} 5 | terra foo([a], b : int, [c]) 6 | return [a] + b 7 | end 8 | local d = {symbol(int),label()} 9 | terra foo2([a], b : int, [d]) 10 | return [a] + b + [d[1]] + [d[2]] 11 | end 12 | 13 | local test = require("test") 14 | 15 | test.eq(foo2(1,2,3,4),10) 16 | test.eq(foo(1,2),3) 17 | -------------------------------------------------------------------------------- /tests/fails/ucall.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | struct Foo {} 3 | function what(a) 4 | end 5 | 6 | terra uc() 7 | what(Foo {}) 8 | end 9 | 10 | uc() -------------------------------------------------------------------------------- /tests/fails/usercastwrong.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | struct A { 5 | a : int 6 | } 7 | 8 | function A.metamethods.__cast(from,to,exp) 9 | return "a" 10 | end 11 | 12 | terra foobar() 13 | var b : int = A { 3 } 14 | return b 15 | end 16 | 17 | foobar() 18 | -------------------------------------------------------------------------------- /tests/fails/veclit.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | terra foo() 4 | var a = 2* vector(1,"what",3) 5 | return a[0] + a[1] + a[2] 6 | end 7 | 8 | local test = require("test") 9 | test.eq(foo(),12) 10 | -------------------------------------------------------------------------------- /tests/fails/veclit2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo2() 5 | var a = vector("what","what","what") 6 | return a[1] 7 | end 8 | 9 | local test = require("test") 10 | test.eq(foo2(),12) 11 | -------------------------------------------------------------------------------- /tests/fails/veclit3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | terra foo2() 5 | var a = vector() 6 | return a[1] 7 | end 8 | 9 | local test = require("test") 10 | test.eq(foo2(),12) 11 | -------------------------------------------------------------------------------- /tests/fails/vectorerror.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | 3 | 4 | 5 | terra foobar() 6 | var a : vector(int,morenonsense) 7 | end 8 | 9 | 10 | foobar() 11 | -------------------------------------------------------------------------------- /tests/fails/vectype.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra foo() 3 | var a : vector(&int,3) 4 | end 5 | 6 | foo:compile() 7 | -------------------------------------------------------------------------------- /tests/falsespec.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra derp() 4 | var a = [false] 5 | return a 6 | end 7 | 8 | assert(derp() == false) -------------------------------------------------------------------------------- /tests/fastcall.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | return 1 4 | end 5 | 6 | 7 | assert(1 == foo()) 8 | assert(1 == foo()) 9 | 10 | 11 | terra foo2() 12 | return 1,2 13 | end 14 | local a,b = terralib.unpackstruct(foo2()) 15 | assert(a == 1 and b == 2) 16 | local a,b = unpackstruct(foo2()) 17 | assert(a == 1 and b == 2) 18 | 19 | terra foo(a : int) 20 | end 21 | -------------------------------------------------------------------------------- /tests/fib.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | local Num = int 4 | 5 | local base = {} 6 | terra base.fib(a : Num) : Num 7 | var i,c,p = 0,1,1 8 | while i < a do 9 | c,p = c + p,c 10 | i = i + 1 11 | end 12 | return c 13 | end 14 | 15 | function fib2(a) 16 | local i,c,p = 0,1,1 17 | while i < a do 18 | c,p = c + p,c 19 | i = i + 1 20 | end 21 | return c 22 | end 23 | for i = 0,10 do 24 | print(base.fib(i)) 25 | test.eq(base.fib(i),fib2(i)) 26 | end -------------------------------------------------------------------------------- /tests/fib2.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | local Num = int 4 | 5 | terra fib(a : Num) : Num 6 | if a == 0 then 7 | return 1 8 | elseif a == 1 then 9 | return 1 10 | else 11 | return fib(a - 1) + fib(a - 2) 12 | end 13 | end 14 | 15 | function fib2(a) 16 | if a == 0 then 17 | return 1 18 | elseif a == 1 then 19 | return 1 20 | else 21 | return fib2(a - 1) + fib2(a - 2) 22 | end 23 | end 24 | for i = 0,10 do 25 | print(fib(i)) 26 | test.eq(fib(i),fib2(i)) 27 | end 28 | -------------------------------------------------------------------------------- /tests/fmod.t: -------------------------------------------------------------------------------- 1 | local fmodf = terralib.externfunction("fmodf",{float,float} -> float) 2 | local terra boom (m : float) : float 3 | return fmodf(m,3.14) --m % 3.14159 4 | end 5 | 6 | print(boom(0)) -------------------------------------------------------------------------------- /tests/fnames.t: -------------------------------------------------------------------------------- 1 | foo = terra(a : int) end 2 | foo:setname("bar") 3 | 4 | assert(foo:getname() == "bar") 5 | foo:disas() -------------------------------------------------------------------------------- /tests/fncalltest.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | terra foo() 5 | 6 | end 7 | 8 | 9 | terra bar() 10 | foo() 11 | var what = foo 12 | --what() 13 | end 14 | 15 | bar:compile() 16 | 17 | -------------------------------------------------------------------------------- /tests/fnpointer.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | return 4LL 4 | end 5 | 6 | terra bar() 7 | return 5LL 8 | end 9 | 10 | terra bar3(fn: {} -> int64) 11 | return fn() 12 | end 13 | 14 | terra baz(a : int64) 15 | var afn = foo 16 | if a > 2 then 17 | afn = bar 18 | end 19 | return bar3(afn) 20 | end 21 | 22 | local test = require("test") 23 | test.eq(baz(1),4) 24 | test.eq(baz(3),5) -------------------------------------------------------------------------------- /tests/fnptr.t: -------------------------------------------------------------------------------- 1 | terra baz() return 1 end 2 | bp = baz:getpointer() 3 | terra foo() 4 | return bp() 5 | end 6 | 7 | assert(foo() == 1) -------------------------------------------------------------------------------- /tests/fnptrc.t: -------------------------------------------------------------------------------- 1 | 2 | C = terralib.includecstring [[ 3 | struct Foo { 4 | int (*bar)(int); 5 | }; 6 | int runptr(int (*bar)(int), int a) { 7 | return bar(a); 8 | } 9 | ]] 10 | 11 | terra add1(a : int) return a + 1 end 12 | 13 | terra what0() 14 | return C.runptr(add1,1) 15 | end 16 | 17 | assert(what0() == 2) 18 | 19 | terra what() 20 | var c : C.Foo 21 | c.bar = add1 22 | return c.bar(1) 23 | end 24 | 25 | assert(what() == 2) -------------------------------------------------------------------------------- /tests/fnptrstruct.t: -------------------------------------------------------------------------------- 1 | local C = terralib.includecstring [[ 2 | struct Foo { int (*what)(int); }; 3 | ]] 4 | 5 | C.Foo:printpretty() 6 | 7 | terra bar(a : int) return a + 1 end 8 | 9 | terra foo(a : &C.Foo, b : int -> int) 10 | a.what = b 11 | end 12 | 13 | foo:disas() -------------------------------------------------------------------------------- /tests/foo.t: -------------------------------------------------------------------------------- 1 | a = global( struct { real : float, imag : float }) -------------------------------------------------------------------------------- /tests/for.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo(a : int, s : int) 3 | var r = 0 4 | for i = 0,a,s do 5 | r = r + i 6 | end 7 | return r 8 | end 9 | 10 | terra foo2(a : int) 11 | var r = 0 12 | for i = 0,a do 13 | r = r + i 14 | end 15 | return r 16 | end 17 | 18 | terra foo3(a : int) 19 | var r = 0 20 | for i = a,0,-1 do 21 | r = r + i 22 | end 23 | return r 24 | end 25 | 26 | local test = require("test") 27 | test.eq(foo(10,1),45) 28 | test.eq(foo(10,2),20) 29 | test.eq(foo(0,1),0) 30 | test.eq(foo2(10),45) 31 | test.eq(foo3(10),55) -------------------------------------------------------------------------------- /tests/for2.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo(a : uint64, s : uint64) 3 | var r = 0 4 | for i : uint64 = 0,a,s do 5 | r = r + i 6 | end 7 | return r 8 | end 9 | 10 | terra foo2(a : int) 11 | var r = 0 12 | for i : int = 0.f,a do 13 | r = r + i 14 | end 15 | return r 16 | end 17 | terra foo3(a : int) 18 | var r = 0 19 | for i = a,0,-1 do 20 | r = r + i 21 | end 22 | return r 23 | end 24 | 25 | 26 | local test = require("test") 27 | test.eq(foo(10,1),45) 28 | test.eq(foo(10,2),20) 29 | test.eq(foo(0,1),0) 30 | test.eq(foo2(10),45) 31 | test.eq(foo3(10),55) 32 | -------------------------------------------------------------------------------- /tests/forbreak.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo(a : int, s : int) 3 | var r = 0 4 | for i = 0,a,s do 5 | if i == 8 then 6 | break 7 | end 8 | r = r + i 9 | 10 | end 11 | return r 12 | end 13 | 14 | local test = require("test") 15 | test.eq(foo(10,1),28) 16 | test.eq(foo(10,2),12) 17 | test.eq(foo(0,1),0) -------------------------------------------------------------------------------- /tests/forlist.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct Range { 4 | a : int; 5 | b : int; 6 | } 7 | Range.metamethods.__for = function(iter,body) 8 | return quote 9 | var it = iter 10 | for i = it.a,it.b do 11 | [body(i)] 12 | end 13 | end 14 | end 15 | 16 | terra foo() 17 | var a = 0 18 | for i in Range {0,10} do 19 | a = a + i 20 | end 21 | return a 22 | end 23 | 24 | assert(foo() == 10*9/2) -------------------------------------------------------------------------------- /tests/forlist2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct Array { 4 | data : int[3]; 5 | } 6 | Array.metamethods.__for = function(iter,bodycallback) 7 | return quote 8 | var it = &iter 9 | for i = 0,3 do 10 | [bodycallback(`&it.data[i])] 11 | end 12 | end 13 | end 14 | 15 | terra foo() 16 | var a = Array{ array(1,2,3) } 17 | for i in a do 18 | @i = @i + 1 19 | end 20 | return a.data[0] + a.data[1] + a.data[2] 21 | end 22 | 23 | terra foo2() 24 | var a = Array{ array(1,2,3) } 25 | var s = 0 26 | for i : &int in a do 27 | s = s + @i 28 | end 29 | return s 30 | end 31 | 32 | assert(foo() == 9) 33 | 34 | assert(foo2() == 6) -------------------------------------------------------------------------------- /tests/forp.t: -------------------------------------------------------------------------------- 1 | struct Range { 2 | a : int; 3 | b : int; 4 | } 5 | Range.metamethods.__for = function(iter,body) 6 | return quote 7 | var it = iter 8 | for i = it.a,it.b do 9 | [ body(i) ] 10 | end 11 | end 12 | end 13 | 14 | terra foo() 15 | var v = Range { 0, 3 } 16 | var vp = &v 17 | var i = 0 18 | for e in vp do i = i + e end 19 | return i 20 | end 21 | assert(3 == foo()) -------------------------------------------------------------------------------- /tests/forsym.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo(a : int, s : int) 3 | var r = 0 4 | for i = 0,a,s do 5 | r = r + i 6 | end 7 | return r 8 | end 9 | 10 | local i = symbol(int) 11 | 12 | terra foo2(a : int) 13 | var r = 0 14 | for [i] = 0,a do 15 | r = r + [i] 16 | end 17 | return r 18 | end 19 | 20 | local test = require("test") 21 | test.eq(foo(10,1),45) 22 | test.eq(foo(10,2),20) 23 | test.eq(foo(0,1),0) 24 | test.eq(foo2(10),45) -------------------------------------------------------------------------------- /tests/forwardtodef.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo() 4 | return 1 5 | end 6 | foo:setinlined(true) 7 | foo:disas() -------------------------------------------------------------------------------- /tests/foundintermediate.t: -------------------------------------------------------------------------------- 1 | terra bar() 2 | escape 3 | local terra what() return foo(3) end 4 | end 5 | return 3 6 | end 7 | terra foo(a : int) : int 8 | return bar() + 1 + a 9 | end 10 | 11 | assert(3 + 1 + 2 == foo(2)) -------------------------------------------------------------------------------- /tests/foundintermediate2.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra bar() 3 | escape 4 | local terra what() return foo(3) end 5 | end 6 | return 3 7 | end 8 | terra foo(a : int) 9 | return 1 + a 10 | end 11 | -------------------------------------------------------------------------------- /tests/foundintermediate3.t: -------------------------------------------------------------------------------- 1 | if not require("fail") then return end 2 | terra bar() 3 | escape 4 | print(foo:gettype()) 5 | end 6 | return 3 7 | end 8 | terra foo(a : int) 9 | return 1 + a 10 | end 11 | -------------------------------------------------------------------------------- /tests/functionnoproto.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | typedef int (*PROC)(); 3 | PROC what() { return 0; } 4 | ]] 5 | 6 | assert(C.what() == nil) -------------------------------------------------------------------------------- /tests/gctest.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | local terra foo() 6 | end 7 | 8 | local terra bar() 9 | end 10 | 11 | foo:compile() 12 | bar:compile() 13 | foo = nil 14 | 15 | collectgarbage() 16 | collectgarbage() 17 | 18 | print("HERE") -------------------------------------------------------------------------------- /tests/getmethod.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A {} 4 | 5 | 6 | A.metamethods.__getmethod = function(self,methodname) 7 | local c = methodname:sub(1,1):byte() 8 | return terra(a : &A) 9 | return c 10 | end 11 | end 12 | 13 | 14 | terra foo() 15 | var a : A 16 | return a:a() + a:b() 17 | end 18 | 19 | assert(foo() == ("a"):byte() + ("b"):byte()) -------------------------------------------------------------------------------- /tests/gettype.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | function makestuff(a) 5 | local T = a:gettype() 6 | local struct ST { 7 | a : T 8 | } 9 | return `ST { a } 10 | end 11 | 12 | makestuff = macro(makestuff) 13 | 14 | terra useit() 15 | return makestuff(true).a,makestuff(3.0).a 16 | end 17 | local test = require("test") 18 | test.meq({true,3},useit()) -------------------------------------------------------------------------------- /tests/globals.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : int } 3 | local a = terralib.global(A) 4 | a:set({3}) 5 | local b = terralib.global(true) 6 | local c = terralib.global(int[3]) 7 | local d = terralib.global(3.5) 8 | 9 | terra foobar() 10 | c[0] = 3 11 | return a.a + d 12 | end 13 | 14 | e = global(int) 15 | terra doihaveaname() 16 | e = 10 17 | return e 18 | end 19 | 20 | assert(doihaveaname() == 10) 21 | assert(6.5 == foobar()) 22 | print("HERE") 23 | assert(true == b:get()) 24 | assert(3 == c:get()[0]) 25 | -------------------------------------------------------------------------------- /tests/goto.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo() 4 | var a = 0 5 | ::begin:: 6 | if a >= 10 then 7 | goto theend 8 | end 9 | a = a + 1 10 | goto begin 11 | ::theend:: 12 | return a 13 | end 14 | 15 | test = require("test") 16 | test.eq(foo(),10) -------------------------------------------------------------------------------- /tests/goto2.t: -------------------------------------------------------------------------------- 1 | 2 | local begin = label() 3 | local theend = label() 4 | 5 | terra foo() 6 | var a = 0 7 | ::[begin]:: 8 | if a >= 10 then 9 | goto [theend] 10 | end 11 | a = a + 1 12 | goto [begin] 13 | ::[theend]:: 14 | return a 15 | end 16 | 17 | test = require("test") 18 | test.eq(foo(),10) -------------------------------------------------------------------------------- /tests/gvarfault.t: -------------------------------------------------------------------------------- 1 | if require("ffi").os == "Windows" then 2 | print("Disabled on windows (uses unistd.h)") 3 | return 4 | end 5 | local C = terralib.includecstring( 6 | [[ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | ]] 13 | ) 14 | 15 | local gstr = global(&int8, "some text") 16 | 17 | terra fun1() 18 | C.printf("gstr: %s \n", gstr) 19 | return 0 20 | end 21 | 22 | fun1:printpretty() 23 | 24 | fun1() -------------------------------------------------------------------------------- /tests/hasbeenfrozen.t: -------------------------------------------------------------------------------- 1 | struct A { 2 | a : int; 3 | b : int; 4 | c : &B 5 | } 6 | struct B { 7 | a : &A 8 | } 9 | 10 | local a = global(A) 11 | 12 | terra foo :: {} -> int 13 | function A.metamethods.__staticinitialize(self) 14 | print("A") 15 | assert(A:iscomplete()) 16 | a:get().a = 4 17 | end 18 | 19 | function B.metamethods.__staticinitialize(self) 20 | print("B") 21 | assert(B:iscomplete()) 22 | a:get().b = 3 23 | a:get().a = a:get().a + 1 24 | end 25 | 26 | terra foo() 27 | var b : B 28 | return a.a + a.b 29 | end 30 | 31 | assert(foo() == 8) -------------------------------------------------------------------------------- /tests/hello.t: -------------------------------------------------------------------------------- 1 | local c = terralib.includec("stdio.h") 2 | 3 | terra hello() 4 | c.printf("hello, world\n") 5 | end 6 | 7 | terralib.saveobj("hello",{main = hello}) 8 | hello() 9 | -------------------------------------------------------------------------------- /tests/hello2.t: -------------------------------------------------------------------------------- 1 | 2 | local c = terralib.includec("stdio.h") 3 | 4 | terralib.printraw(getmetatable(c).errors) 5 | 6 | terra main() 7 | c.printf("hello, world\n") 8 | end 9 | 10 | main() -------------------------------------------------------------------------------- /tests/hexf.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | assert(0xf == 15) 4 | 5 | terra foo() 6 | return 0xf 7 | end 8 | 9 | assert(foo() == 15) -------------------------------------------------------------------------------- /tests/huge.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra returnhuge() 4 | return math.huge 5 | end 6 | 7 | assert(returnhuge() == math.huge) -------------------------------------------------------------------------------- /tests/ifelse.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo(a : int) 3 | return terralib.select(a > 0, 1,-1) 4 | end 5 | 6 | 7 | terra foo2(a : int) 8 | var c = vector(a > 0, a > 1, a > 2) 9 | var d = terralib.select(c,vector(1,2,3),vector(4,5,6)) 10 | return d[0], d[1], d[2] 11 | end 12 | local test = require("test") 13 | 14 | test.eq(foo(1),1) 15 | test.eq(foo(-1),-1) 16 | 17 | test.meq({1,5,6},foo2(1)) -------------------------------------------------------------------------------- /tests/includetwice.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local C1 = terralib.includecstring [[ 4 | typedef struct { int a; } A; 5 | int geta(A* a) { return a->a; } 6 | ]] 7 | local C2 = terralib.includecstring [[ 8 | typedef struct { int a; } A; 9 | void seta(A* a, int v) { a->a = v; } 10 | ]] 11 | 12 | assert(C1.A == C2.A) 13 | 14 | 15 | terra usethem() 16 | var a = C1.A { 1 } 17 | C2.seta(&a,C1.geta(&a) + 3) 18 | return a.a 19 | end 20 | 21 | assert(usethem() == 4) -------------------------------------------------------------------------------- /tests/incomplete.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A 4 | 5 | terra foo(a : &A) 6 | return a 7 | end 8 | 9 | assert(nil == foo(nil)) 10 | assert(false == A:iscomplete()) 11 | 12 | 13 | struct A { 14 | b : int 15 | } 16 | 17 | terra foo2(a : &A) 18 | a.b = 6 19 | return @a 20 | end 21 | 22 | foo2:compile() 23 | assert(true == A:iscomplete()) 24 | 25 | local ptr = terralib.new(A) 26 | 27 | ptr.b = 4 28 | 29 | local r = foo2(ptr) 30 | 31 | assert(r.b == 6) 32 | assert(ptr.b == 6) -------------------------------------------------------------------------------- /tests/incomplete2.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { 3 | b : &B 4 | } 5 | struct B 6 | 7 | 8 | terra foo() 9 | var a : A 10 | a.b = nil 11 | return a 12 | end 13 | 14 | local a = foo() 15 | 16 | 17 | struct B { 18 | a : int 19 | } 20 | 21 | terra foo2(a : &A) 22 | a.b = a.b + 1 23 | end 24 | 25 | foo2(a) 26 | -------------------------------------------------------------------------------- /tests/incomplete3.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { 3 | b : B -> C 4 | } 5 | struct B 6 | struct C 7 | 8 | 9 | terra foo() 10 | var a : A 11 | a.b = nil 12 | return a 13 | end 14 | 15 | local a = foo() 16 | 17 | 18 | struct B { 19 | a : int 20 | } 21 | struct C { 22 | b : int 23 | } 24 | 25 | terra foo2(a : &A) 26 | var ptrb : &B 27 | var c = a.b(@ptrb) 28 | end 29 | 30 | foo2:compile() -------------------------------------------------------------------------------- /tests/incomplete4.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A 4 | 5 | terra foo(a : &A) 6 | return a 7 | end 8 | 9 | assert(nil == foo(nil)) 10 | assert(false == A:iscomplete()) 11 | 12 | 13 | struct A { 14 | b : int 15 | } 16 | 17 | terra foo2(a : &A) 18 | return a.b 19 | end 20 | 21 | foo2:compile() 22 | assert(true == A:iscomplete()) 23 | -------------------------------------------------------------------------------- /tests/incompletetypetest.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | 8 | terra what() 9 | var a : &A = nil 10 | return &a[4] 11 | end 12 | 13 | what:compile() -------------------------------------------------------------------------------- /tests/incompletetypetest2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | 8 | terra what() 9 | var a : &A = nil 10 | return a + 4 11 | end 12 | 13 | what:compile() -------------------------------------------------------------------------------- /tests/incompletetypetest3.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | 8 | terra what() 9 | var a : &A = nil 10 | return a - a 11 | end 12 | 13 | what:compile() -------------------------------------------------------------------------------- /tests/indexing64.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo() 4 | var a : int[5] 5 | a[2] = 2 6 | var b = [int64](2) 7 | return a[b] 8 | end 9 | 10 | assert(foo() == 2) -------------------------------------------------------------------------------- /tests/indexingbug.t: -------------------------------------------------------------------------------- 1 | arr = terralib.new(int[256]) 2 | 3 | arr[252] = 12 4 | 5 | terra foo(arr : &int) 6 | var idx = [uint8](252) 7 | return arr[idx] 8 | end 9 | 10 | terra foo2(arr : &int) 11 | var idx = [uint8](252) 12 | return @(arr + idx) 13 | end 14 | 15 | terra foo3(arr : &int) 16 | var idx = [int8](-3) 17 | arr = arr + 255 18 | return @(arr + idx) 19 | end 20 | 21 | 22 | terra foo4(arr : &int) 23 | var idx = [int8](-3) 24 | arr = arr + 255 25 | return arr[idx] 26 | end 27 | 28 | assert(foo2(arr) == 12) 29 | assert(foo(arr) == 12) 30 | assert(foo3(arr) == 12) 31 | assert(foo4(arr) == 12) -------------------------------------------------------------------------------- /tests/inline_c.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring[[inline int testfunc(int a, int b) { return a + b; }]] 2 | 3 | terra foobar() : int 4 | var a : int = 5 5 | return C.testfunc(a, 9) 6 | end 7 | assert(foobar() == 14) 8 | 9 | terralib.saveobj("inline_c.exe", {main = foobar}) -------------------------------------------------------------------------------- /tests/intrinsic.t: -------------------------------------------------------------------------------- 1 | local sqrt = terralib.intrinsic("llvm.sqrt.f32",float -> float) 2 | local sqrt2 = terralib.intrinsic("llvm.sqrt.v4f32",vector(float,4) -> vector(float,4)) 3 | 4 | local sqrt3 = terralib.intrinsic(function(types) 5 | if #types == 1 and types[1]:isvector() and types[1].type == float then 6 | local N = types[1].N 7 | return ("llvm.sqrt.v%df32"):format(N), vector(float,N) -> vector(float,N) 8 | elseif #types == 1 and types[1] == float then 9 | return "llvm.sqrt.f32",float -> float 10 | end 11 | end) 12 | 13 | terra foo() 14 | var v = vector(1.f,2.f,3.f,4.f) 15 | var c = sqrt2(vector(1,2,3,4)) 16 | return sqrt(4) + c[3] + sqrt3(v)[3] + sqrt3(4.f) 17 | end 18 | 19 | local test = require("test") 20 | 21 | test.eq(foo(),8) -------------------------------------------------------------------------------- /tests/isconstant.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | C = terralib.isconstant 4 | 5 | assert(C`1) 6 | 7 | assert(not C`1+1) 8 | a = 4 9 | c = terralib.constant(`4+6) 10 | print(c) 11 | assert(C`a) 12 | assert(C(c)) 13 | w = terralib.constant("what") 14 | assert(C`"what") 15 | assert(C(w)) 16 | assert(not C(nil)) 17 | assert(not C(4)) -------------------------------------------------------------------------------- /tests/isvolatile.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foobar(a : &vector(float,4),b : vector(int,4)) 4 | terralib.attrstore(a,b,{ isvolatile = true }) 5 | end 6 | 7 | foobar:disas() 8 | -------------------------------------------------------------------------------- /tests/labelbug.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | ::alabel:: ;; 4 | return 0 5 | end -------------------------------------------------------------------------------- /tests/latelink.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | typedef struct { 3 | int a; 4 | } S; 5 | typedef struct { 6 | S a; 7 | } A; 8 | ]] 9 | C2 = terralib.includecstring [[ 10 | typedef struct { 11 | int a; 12 | } S; 13 | typedef struct { 14 | S a; 15 | } B; 16 | ]] 17 | 18 | terra second() 19 | var a : C2.B 20 | var b : C.A 21 | 22 | a.a = b.a 23 | end 24 | second:disas() -------------------------------------------------------------------------------- /tests/lazycstring.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { 3 | a : int 4 | } 5 | 6 | 7 | print(A:cstring()) 8 | 9 | terra foo() 10 | return A { 1 } 11 | end 12 | 13 | struct B { 14 | a : int 15 | } 16 | 17 | terra foo2() 18 | return B { 1 } 19 | end 20 | 21 | assert(foo().a == foo2().a) -------------------------------------------------------------------------------- /tests/let1.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo() 4 | return (1),(2) 5 | end 6 | 7 | terra bar() 8 | var c = [quote var a = 3 in a end] 9 | var d = 5 10 | c = c + 10 11 | return [quote var a = 3 in [quote var b = 4 in a + b + c + d end] end] 12 | end 13 | local test = require("test") 14 | test.meq({1,2},foo()) 15 | assert(25 == bar()) 16 | -------------------------------------------------------------------------------- /tests/lib/barlang.t: -------------------------------------------------------------------------------- 1 | return { 2 | keywords = {"foo"}; 3 | entrypoints = {"bar"}; 4 | expression = function(self,lex) 5 | lex:expect("bar") 6 | lex:expect("foo") 7 | return function() return 2 end 8 | end 9 | } -------------------------------------------------------------------------------- /tests/lib/fakeimport.t: -------------------------------------------------------------------------------- 1 | error("what") -------------------------------------------------------------------------------- /tests/lib/foolang.t: -------------------------------------------------------------------------------- 1 | return { 2 | keywords = {"bar"}; 3 | entrypoints = {"foo"}; 4 | expression = function(self,lex) 5 | lex:expect("foo") 6 | lex:expect("bar") 7 | return function() return 1 end 8 | end 9 | } -------------------------------------------------------------------------------- /tests/linklibrary.t: -------------------------------------------------------------------------------- 1 | local ffi = require 'ffi' 2 | if ffi.os == "Windows" then 3 | os.exit() 4 | end 5 | 6 | -- Test that terralib.linklibrary doesn't reject a library that just happens to end in bc 7 | 8 | C = terralib.includec("stdio.h") 9 | 10 | terra f() 11 | C.printf("it works!\n") 12 | end 13 | 14 | terralib.saveobj("./not_bc", "sharedlibrary", {g = f}) -- Not actually a .bc file! 15 | terralib.linklibrary("./not_bc") 16 | L = terralib.includecstring [[ 17 | void g(void); 18 | ]] 19 | 20 | L.g() 21 | -------------------------------------------------------------------------------- /tests/linkllvm.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra add(a : int, b : int) 4 | return a + b 5 | end 6 | 7 | 8 | terralib.saveobj("add.bc","bitcode",{ add = add }) 9 | 10 | local addlib = terralib.linkllvm("add.bc") 11 | add2 = addlib:extern("add", {int,int} -> int) 12 | 13 | assert(add2(3,4) == 7) -------------------------------------------------------------------------------- /tests/linkllvm2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra add(a : int, b : int) 4 | return a + b 5 | end 6 | 7 | local r = terralib.saveobj(nil,"bitcode",{ add = add }) 8 | local addlib = terralib.linkllvmstring(r) 9 | add2 = addlib:extern("add", {int,int} -> int) 10 | 11 | assert(add2(3,4) == 7) -------------------------------------------------------------------------------- /tests/localenv2.t: -------------------------------------------------------------------------------- 1 | 2 | local dd = 8 3 | terra nested3() 4 | return [ `dd ] 5 | end 6 | local test = require("test") 7 | test.eq(nested3(),8) -------------------------------------------------------------------------------- /tests/logical.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local T = true 4 | local F = false 5 | 6 | terra check() 7 | var a = true 8 | return not true == false 9 | end 10 | 11 | assert(check() == true) 12 | 13 | terra check2() 14 | return not T == F 15 | end 16 | 17 | assert(check2() == true) 18 | 19 | terra check3() 20 | return not T == not not T 21 | end 22 | 23 | assert(check3() == false) 24 | 25 | terra check4() 26 | return not not T == T and not not F == F and true == T and false == F 27 | end 28 | 29 | assert(check4() == true) 30 | 31 | terra foo() return not false end 32 | 33 | assert(foo() == true) -------------------------------------------------------------------------------- /tests/luaapi.t: -------------------------------------------------------------------------------- 1 | terralib.includepath = terralib.includepath .. ";" .. terralib.terrahome .. "/include/terra" 2 | C = terralib.includecstring [[ 3 | #include "lua.h" 4 | #include "lauxlib.h" 5 | #include "stdio.h" 6 | ]] 7 | 8 | terra foo(L : &C.lua_State) : int 9 | C.printf("stack has %d arguments\n",C.lua_gettop(L)) 10 | C.lua_getfield(L,1,"a") 11 | return 1 12 | end 13 | local foob = terralib.bindtoluaapi(foo:getpointer()) 14 | 15 | assert(type(foob) == "function") 16 | 17 | a = {} 18 | assert(a == foob(_G,2,3)) 19 | -------------------------------------------------------------------------------- /tests/luabridge.t: -------------------------------------------------------------------------------- 1 | struct A { a : int, b : double } 2 | 3 | terra foo(a : A) 4 | return a.a + a.b 5 | end 6 | 7 | 8 | terra foo2(a : A) 9 | return a.a + a.b,a.a 10 | end 11 | 12 | local test = require("test") 13 | 14 | test.eq( foo( {a = 1,b = 2.3} ), 3.3 ) 15 | test.eq( foo( {1,2.3} ), 3.3 ) 16 | test.eq( foo( {b = 1, a = 2.3} ),3 ) 17 | 18 | test.meq({3.3,1},foo2( { a = 1, b = 2.3} )) -------------------------------------------------------------------------------- /tests/luabridgefn.t: -------------------------------------------------------------------------------- 1 | 2 | terra plus1(a : int) 3 | return a + int(1) 4 | end 5 | 6 | terra afn() : int -> int 7 | return plus1 8 | end 9 | 10 | terra doit(a : int, b : int -> int) 11 | return b(a) 12 | end 13 | 14 | local test = require("test") 15 | 16 | local foobar = afn() 17 | test.eq(foobar(3),4) 18 | test.eq(doit(4,foobar),5) 19 | local function whatwhat(a) 20 | print("I GOT A ", a) 21 | return a + 3 22 | end 23 | test.eq(doit(5,whatwhat),8) 24 | -------------------------------------------------------------------------------- /tests/luabridgerec.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : &A, b : int } 3 | 4 | terra foo(a : A) 5 | return a.b 6 | end 7 | 8 | local test = require("test") 9 | 10 | test.eq(foo({b = 5}),5) 11 | -------------------------------------------------------------------------------- /tests/luabridgeunion.t: -------------------------------------------------------------------------------- 1 | struct A { union {a : int, b : double}, c : int } 2 | 3 | terra foo(a : A) 4 | return a.a + a.c 5 | end 6 | terra foo2(a : A) 7 | return a.b + a.c 8 | end 9 | 10 | 11 | local test = require("test") 12 | 13 | test.eq( foo({a = 4, c = 6}), 10) 14 | test.eq( foo2({b = 4.4, c = 6}), 10.4) -------------------------------------------------------------------------------- /tests/lvaluepointer.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo() 4 | var a : int = 5 5 | var pa = &a 6 | @pa = 4 7 | @[&float](pa) = 5 8 | end 9 | 10 | foo() -------------------------------------------------------------------------------- /tests/lvaluequote.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | checkl = macro(function(a) 4 | assert(a:islvalue()) 5 | return a 6 | end) 7 | 8 | checkr = macro(function(a) 9 | assert(not a:islvalue()) 10 | return a 11 | end) 12 | 13 | 14 | terra testit() 15 | 16 | var a = checkr(4) 17 | var b = checkr(checkl(a) + 3) 18 | var c = checkl([quote var d = 5 in d end]) 19 | return a + b + c 20 | end 21 | 22 | assert(4+7+5 == testit()) 23 | -------------------------------------------------------------------------------- /tests/lvaluetreelist.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foobar() 4 | var a = 4 5 | @&[quote in a end] = 5 6 | return a 7 | end 8 | 9 | assert(foobar() == 5) -------------------------------------------------------------------------------- /tests/macro2.t: -------------------------------------------------------------------------------- 1 | 2 | local bar2 = macro(function(typ) 3 | return typ 4 | 5 | end) 6 | 7 | terra foo() : int 8 | var a = 3 9 | bar2(a) = bar2(a) + 5 10 | return a 11 | end 12 | 13 | local test = require("test") 14 | test.eq(8,foo()) 15 | -------------------------------------------------------------------------------- /tests/macro3.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | foo = macro(function() 4 | local terra bar() 5 | return 3 6 | end 7 | return bar 8 | end) 9 | 10 | terra baz() 11 | return foo()() 12 | end 13 | 14 | assert(baz() == 3) -------------------------------------------------------------------------------- /tests/macrokey.t: -------------------------------------------------------------------------------- 1 | 2 | what = function() return "abcd" end 3 | 4 | terra foo() 5 | var a = { [what()] = 4 } 6 | return a.abcd 7 | end 8 | local test = require("test") 9 | test.eq(foo(),4) -------------------------------------------------------------------------------- /tests/macrolet.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | twice = macro(function(a) 4 | return quote 5 | var v = a 6 | in 7 | v + v 8 | end 9 | end) 10 | 11 | 12 | terra foobar() 13 | var what = 1 14 | return twice([quote what = what + 1 in what end]) + what 15 | end 16 | 17 | assert(6 == foobar()) -------------------------------------------------------------------------------- /tests/macroselect.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : int, b : int } 3 | 4 | local c = "b" 5 | terra foo() 6 | var a = A {1,2} 7 | return a.[c] 8 | end 9 | 10 | local test = require("test") 11 | test.eq(foo(),2) -------------------------------------------------------------------------------- /tests/macrotest.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | #define foo 1 3 | #define foo2 -3.4 4 | #undef foo 5 | #define foo 3 6 | ]] 7 | 8 | assert(C.foo == 3) 9 | assert(C.foo2 == -3.4) -------------------------------------------------------------------------------- /tests/malloc.t: -------------------------------------------------------------------------------- 1 | local c = terralib.includecstring [[ 2 | #include 3 | #include 4 | ]] 5 | 6 | local N = 10 7 | terra foo() 8 | c.printf("size = %d\n",int(sizeof(int))) 9 | var list = [&int](c.malloc(sizeof(int)*N)) 10 | for i = 0,N do 11 | list[i] = i + 1 12 | end 13 | var result = 0 14 | for i = 0,N do 15 | c.printf("%d = %d\n",int(i),list[i]) 16 | result = result + list[i] 17 | end 18 | c.free([&uint8](list)) 19 | return result 20 | end 21 | 22 | local test = require("test") 23 | test.eq(foo(),N*(1 + N)/2) 24 | -------------------------------------------------------------------------------- /tests/mathlib.t: -------------------------------------------------------------------------------- 1 | local c = terralib.includec("math.h") 2 | 3 | terra mysqrt(a : float) 4 | return c.sqrtf(a) 5 | end 6 | 7 | local test = require("test") 8 | test.eq(mysqrt(4),2) 9 | test.eq(mysqrt(9),3) -------------------------------------------------------------------------------- /tests/metatype.t: -------------------------------------------------------------------------------- 1 | local function foo(T) 2 | terra T:what() 3 | return self.a + self.b 4 | end 5 | end 6 | struct A(foo) { 7 | a : int; 8 | b : int; 9 | } 10 | 11 | terra test() 12 | var a = A {1,2} 13 | return a:what() 14 | end 15 | 16 | assert(test() == 3) -------------------------------------------------------------------------------- /tests/method.t: -------------------------------------------------------------------------------- 1 | --the zero line 2 | struct A { b : B } 3 | struct B {a : int, b : int} 4 | 5 | B.methods.foo = terra(b : B) 6 | return b.a 7 | end 8 | terra bar() 9 | var b = B { 1,2 } 10 | return b:foo() 11 | end 12 | 13 | 14 | B.methods.foo2 = terra(b : &B) 15 | b.a = 6 16 | end 17 | 18 | terra bar2() 19 | var b = B { 1,2 } 20 | b:foo2() 21 | return b.a 22 | end 23 | 24 | B.methods.foo3 = terra(b : B) 25 | return b.a 26 | end 27 | 28 | terra bar3() 29 | var b = B { 1,2 } 30 | return (&b):foo3() 31 | end 32 | terra bar4() 33 | var b = B { 1,2 } 34 | (&b):foo2() 35 | return b.a 36 | end 37 | 38 | test = require("test") 39 | test.eq(bar(),1) 40 | test.eq(bar2(),6) 41 | test.eq(bar3(),1) 42 | test.eq(bar4(),6) -------------------------------------------------------------------------------- /tests/methodantiquote.t: -------------------------------------------------------------------------------- 1 | --the zero line 2 | M = {} 3 | struct M.B {a : int, b : int} 4 | 5 | terra M.B:foo(a : int) 6 | return self.a + a 7 | end 8 | 9 | 10 | local avar = "foo" 11 | 12 | terra bar() 13 | var b = M.B { 1,2 } 14 | return b:[avar](3) 15 | end 16 | 17 | test = require("test") 18 | test.eq(bar(),4) 19 | -------------------------------------------------------------------------------- /tests/methodmissing.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | A.metamethods.__methodmissing = macro(function(methodname,obj,anarg) 8 | print(methodname) 9 | return `anarg + [string.byte(methodname,1,1)] 10 | end) 11 | 12 | terra foobar() 13 | var a : A 14 | return a:a(3) + a:b(4) 15 | end 16 | 17 | assert(foobar() == 202) -------------------------------------------------------------------------------- /tests/methodrvalue.t: -------------------------------------------------------------------------------- 1 | --the zero line 2 | M = {} 3 | struct M.B {a : int, b : int} 4 | 5 | terra M.B:foo(a : int) 6 | return self.a + a 7 | end 8 | 9 | terra rify() 10 | var a = M.B { 1,2} 11 | return a 12 | end 13 | terra bar() 14 | return (rify()):foo(3) 15 | end 16 | 17 | test = require("test") 18 | test.eq(bar(),4) 19 | -------------------------------------------------------------------------------- /tests/methodsugar.t: -------------------------------------------------------------------------------- 1 | --the zero line 2 | M = {} 3 | struct M.B {a : int, b : int} 4 | 5 | terra M.B:foo(a : int) 6 | return self.a + a 7 | end 8 | 9 | terra bar() 10 | var b = M.B { 1,2 } 11 | return b:foo(3) 12 | end 13 | 14 | test = require("test") 15 | test.eq(bar(),4) 16 | -------------------------------------------------------------------------------- /tests/missingfields.t: -------------------------------------------------------------------------------- 1 | test = terralib.includecstring([[ 2 | typedef struct {} emptyanon; 3 | typedef struct foo foobar; 4 | void test(struct foo * f); 5 | struct foo { int x; }; 6 | union ufoo; 7 | typedef union { int a; int b; } anonunion; 8 | ]]) 9 | terra main() 10 | var s : test.foo 11 | s.x = 1 12 | end 13 | 14 | main() -------------------------------------------------------------------------------- /tests/mixed.t: -------------------------------------------------------------------------------- 1 | a = terra() return 4 end 2 | local d = {} 3 | do 4 | struct A{} 5 | local terra a() return 5 end 6 | terra d.d(a : B) end 7 | terra B:a() end 8 | struct B {} 9 | terra c() end 10 | struct C {} 11 | assert(a() == 5) 12 | end 13 | assert(a() == 4) -------------------------------------------------------------------------------- /tests/multiconstructor.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() : {double, double} 3 | return 1.0,3.0 4 | end 5 | 6 | struct A {c : int, a : int, b : double } 7 | 8 | terra bar() 9 | var r = foo() 10 | var a = A {1,unpackstruct(r)} 11 | var b = A {1,2,(foo())._0} 12 | var c = A {c = 1,a = 2,b = foo()._0} 13 | return a.c + a.a + a.b + b.c + c.c 14 | end 15 | 16 | local test = require("test") 17 | test.eq(7,bar()) -------------------------------------------------------------------------------- /tests/multimacro.t: -------------------------------------------------------------------------------- 1 | 2 | local bar = terralib.internalmacro(function(ctx,tree,typ,x) 3 | return {(`4.0).tree, x } 4 | end) 5 | 6 | local x,y,z = 1,2,3 7 | 8 | terra foo() : int 9 | var a,b = bar(int,x + y + z) 10 | var c = bar(int,0)._0 + 1 11 | --bar2(int) = bar2(int) + 5 12 | --bar3(up(&a),up(&a)) 13 | return a + b + c 14 | end 15 | 16 | local test = require("test") 17 | test.eq(15,foo()) 18 | -------------------------------------------------------------------------------- /tests/mytest.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int myfoobarthing(int a, double b, int c, int* d) { 4 | FILE* foo = fopen("what", "r"); 5 | printf("my foobar thing is alive %d %f %d\n", a, b, c); 6 | *d = 8; 7 | return 7; 8 | } 9 | int myotherthing(int a, int b) { return a + b; } 10 | int myfnptr(int (*foobar)(void)) { return foobar(); } 11 | 12 | typedef struct MyStruct { 13 | int a; 14 | double b; 15 | } MyStruct; 16 | typedef struct MyStruct2 { 17 | int a; 18 | union { 19 | int b; 20 | int c; 21 | }; 22 | } MyStruct2; -------------------------------------------------------------------------------- /tests/names.t: -------------------------------------------------------------------------------- 1 | struct A { 2 | } 3 | 4 | local B = (function() 5 | local struct A {} 6 | return A 7 | end)() 8 | C = terralib.types.newstruct("A$1") 9 | D = terralib.types.newstruct("A$1") 10 | 11 | local names = {} 12 | for i,t in ipairs {A,B,C,D} do 13 | local n = tostring(t) 14 | assert(not names[n]) 15 | names[n] = true 16 | end -------------------------------------------------------------------------------- /tests/nantest.t: -------------------------------------------------------------------------------- 1 | local nan = tonumber("NaN") 2 | 3 | terra isnanf (x : float) : bool 4 | return x ~= x 5 | end 6 | 7 | terra isnan (x : double) : bool 8 | return x ~= x 9 | end 10 | 11 | -- pass 12 | assert(isnan(1.5) == false) 13 | -- pass 14 | assert(isnanf(1.5) == false) 15 | -- fail 16 | assert(isnan(nan) == true) 17 | -- fail 18 | assert(isnanf(nan) == true) -------------------------------------------------------------------------------- /tests/nestedcalls.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo3(a : &int) : &int 3 | return a 4 | end 5 | 6 | terra bar2(a : int) 7 | return @(foo3(&a)) 8 | end 9 | 10 | local test = require("test") 11 | test.eq(bar2(42),42) -------------------------------------------------------------------------------- /tests/nestextract.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | count = global(int,0) 8 | 9 | terra twoAs(a : int) 10 | return A {a}, A { a } 11 | end 12 | 13 | function A.metamethods.__cast(fromt,tot,exp) 14 | if tot == A and fromt == int then 15 | return `twoAs(exp)._0 16 | end 17 | error("what") 18 | end 19 | 20 | terra twoInts() 21 | count = count + 1 22 | return count,2 23 | end 24 | 25 | terra takesAnA(a : A) 26 | return a.a 27 | end 28 | 29 | dotwice = macro(function(exp) 30 | return {exp,exp} 31 | end) 32 | 33 | terra doit() 34 | return dotwice(takesAnA((twoInts()._0))) 35 | end 36 | 37 | 38 | doit:printpretty() 39 | local test = require("test") 40 | test.meq({1,2},doit()) 41 | -------------------------------------------------------------------------------- /tests/nestnoerror.t: -------------------------------------------------------------------------------- 1 | 2 | local m = macro(function() 3 | local success = pcall(function() 4 | terra haserror() 5 | return (1):foo() 6 | end 7 | end) 8 | assert(not success) 9 | return 1 10 | end) 11 | 12 | terra noerror() 13 | return m() 14 | end 15 | 16 | assert(1 == noerror()) -------------------------------------------------------------------------------- /tests/new.t: -------------------------------------------------------------------------------- 1 | local c = terralib.includec("stdlib.h") 2 | 3 | new = macro(function(typquote) 4 | local typ = typquote:astype() 5 | return `[&typ](c.malloc(sizeof(typ))) 6 | end) 7 | 8 | local typ = int 9 | terra doit() 10 | var a : &int = new(int) 11 | return a 12 | end 13 | 14 | doit() -------------------------------------------------------------------------------- /tests/newerrortest.t: -------------------------------------------------------------------------------- 1 | print(xpcall(function() 2 | local a = macro(function(a) 3 | error("no") 4 | end) 5 | 6 | terra what() 7 | a() 8 | end 9 | end,debug.traceback)) -------------------------------------------------------------------------------- /tests/nillocal.t: -------------------------------------------------------------------------------- 1 | 2 | a = 4 3 | local a = nil 4 | 5 | terra foo() return [assert(not a)] end 6 | assert(foo() == true) -------------------------------------------------------------------------------- /tests/niltype.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | var a : &int = nil 4 | var b = nil 5 | return a == b 6 | end 7 | 8 | local test = require("test") 9 | test.eq(foo(),true) -------------------------------------------------------------------------------- /tests/nojit.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | --var a = 4 + 5 4 | a = global(4 + 5) 5 | terra foo() 6 | return a 7 | end 8 | 9 | 10 | foo:gettype() 11 | --we currently don't track what initializers need to be run after a nojit compile 12 | --hopefully we can just remove the need to have nojit entirely 13 | --otherwise we need to seperate the calling of variable initializers from the compilation process 14 | --so that they can be called when jit is invoked 15 | a:gettype() 16 | 17 | local test = require("test") 18 | test.eq(foo(),9) 19 | 20 | 21 | -------------------------------------------------------------------------------- /tests/nolengthop.t: -------------------------------------------------------------------------------- 1 | local a,b = terralib.loadstring([[terra g() return #a]]) 2 | assert(b:find("operator not supported")) -------------------------------------------------------------------------------- /tests/nonprototypec.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | int foobar() { 3 | return 3; 4 | } 5 | ]] 6 | 7 | 8 | terra doit() 9 | return C.foobar() 10 | end 11 | 12 | print(doit()) -------------------------------------------------------------------------------- /tests/nontemporal.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foobar(a : &vector(float,4),b : vector(int,4)) 4 | var x = terralib.attrload(a,{ nontemporal = true }) 5 | terralib.attrstore(a,b+x,{ nontemporal = true }) 6 | end 7 | 8 | function wrap(attrs) 9 | local fn = terra (a : &vector(float,4),b : vector(int,4)) 10 | var x = terralib.attrload(a,attrs) 11 | terralib.attrstore(a,b+x,attrs) 12 | end 13 | return fn 14 | end 15 | 16 | foobar:disas() 17 | wrap({ nontemporal = true }):disas() 18 | wrap({ nontemporal = false }):disas() 19 | -------------------------------------------------------------------------------- /tests/noopt.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | terra foo(x : int) : int 4 | return x 5 | end 6 | foo:setoptimized(false) 7 | 8 | test.eq(foo(42),42) 9 | -------------------------------------------------------------------------------- /tests/noreturn.t: -------------------------------------------------------------------------------- 1 | exit = terralib.externfunction("exit", {int} -> {}) 2 | exit:setnoreturn(true) 3 | 4 | terra foo() : {} 5 | exit(0) 6 | end 7 | 8 | foo:compile() 9 | foo:disas() 10 | -------------------------------------------------------------------------------- /tests/numliteral.t: -------------------------------------------------------------------------------- 1 | 2 | terra thetest() 3 | var a,b,c,d,e = 3,3.0,3.f,3LL, 3ULL 4 | return a,b,c,d,e 5 | end 6 | 7 | local exp = { "int32", "double", "float", "int64", "uint64" } 8 | 9 | local test = require("test") 10 | thetest:compile() 11 | local typ = thetest:gettype() 12 | for i,e in ipairs(typ.returntype:getentries()) do 13 | test.eq(tostring(e.type),exp[i]) 14 | end -------------------------------------------------------------------------------- /tests/objc2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/terralang/terra/c62d8e69bd0857968501832401ea16ace981555c/tests/objc2.jpg -------------------------------------------------------------------------------- /tests/offsetcalc.t: -------------------------------------------------------------------------------- 1 | local offsetinbytescache = {} 2 | function offsetinbytes(structtype,key) 3 | local typetable = offsetinbytescache[structtype] or {} 4 | local value = typetable[key] 5 | if value then 6 | return value 7 | end 8 | offsetinbytescache[structtype] = typetable 9 | 10 | local terra offsetcalc() : int 11 | var a : &structtype = [&structtype](0) 12 | return [&int8](&a.[key]) - [&int8](a) 13 | end 14 | 15 | local result = offsetcalc() 16 | 17 | typetable[key] = result 18 | return result 19 | end 20 | 21 | 22 | struct A { a : int8, c : int8, b : int } 23 | 24 | 25 | terra foo() 26 | return 4LL 27 | end 28 | 29 | local test = require("test") 30 | 31 | test.eq(offsetinbytes(A,"b"),4) 32 | 33 | 34 | -------------------------------------------------------------------------------- /tests/opaquealloc.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local r,e = xpcall(function() 5 | local struct A { 6 | a : opaque 7 | } 8 | local v = terralib.new(A) 9 | end,debug.traceback) 10 | 11 | assert(not r and e:match("attempting to use an opaque type")) 12 | 13 | 14 | local r,e = xpcall(function() 15 | local v = terralib.new(opaque) 16 | end,debug.traceback) 17 | 18 | assert(not r and e:match("attempting to use an opaque type")) -------------------------------------------------------------------------------- /tests/option_e.t: -------------------------------------------------------------------------------- 1 | local ffi = require("ffi") 2 | 3 | local function getcommand() 4 | local prefix = terralib and terralib.terrahome and 5 | terralib.terrahome .."/bin/terra" or "../terra" 6 | if ffi.os == "Windows" then 7 | prefix = prefix:gsub("[/\\]","\\\\") 8 | end 9 | return prefix 10 | end 11 | 12 | -- On windows, if the source exe has a space in it, we have to surround not only the paths with quotes, but also the entire command with quotes 13 | local cmd = " -e \"local c = terralib.includec([[stdio.h]]); terra f() c.printf([[hello]]) end; f(); print()\"" 14 | if ffi.os == "Windows" then 15 | cmd = [[cmd /c ""]] .. getcommand() .. "\"" .. cmd .. "\"" 16 | else 17 | cmd = getcommand() .. cmd 18 | end 19 | 20 | print(cmd) 21 | assert(os.execute(cmd) == 0) -------------------------------------------------------------------------------- /tests/or.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | terra foo(a : double, b : double, c : double) : bool 4 | return a < b or b < c 5 | end 6 | 7 | test.eq(foo(1,2,1),true) 8 | test.eq(foo(2,1,2),true) 9 | test.eq(foo(3,2,1),false) -------------------------------------------------------------------------------- /tests/ordercomplete.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | struct B { 8 | a : A 9 | } 10 | 11 | function A.metamethods.__staticinitialize() 12 | print("STATIC INIT A") 13 | local terra what(b : B) 14 | end 15 | end 16 | 17 | function B.metamethods.__staticinitialize() 18 | print("STATIC INIT B") 19 | end 20 | 21 | terra foo(b : B) 22 | end 23 | 24 | foo:compile() -------------------------------------------------------------------------------- /tests/output.t: -------------------------------------------------------------------------------- 1 | C = terralib.includec("stdio.h") 2 | terra main() 3 | C.printf("hello world\n") 4 | end 5 | local m = { main = main } 6 | terralib.saveobj("output.o",m) 7 | local a = terralib.saveobj(nil,"object",m) 8 | terralib.saveobj("output2.bc",m) 9 | local b = terralib.saveobj(nil,"bitcode",m) 10 | terralib.saveobj("output.ll",m) 11 | local c = terralib.saveobj(nil,"llvmir",m) 12 | terralib.saveobj("output",m) 13 | terralib.saveobj("output2","executable",m) 14 | 15 | assert(a:match("hello world")) 16 | assert(c:match("hello world")) -------------------------------------------------------------------------------- /tests/overload.t: -------------------------------------------------------------------------------- 1 | local foo = terralib.overloadedfunction("foo") 2 | foo:adddefinition(terra(a : int) 3 | return 1 4 | end) 5 | foo:adddefinition(terra(a : &int8) 6 | return 2 7 | end) 8 | 9 | print(#foo.definitions) 10 | 11 | terra doit() 12 | return foo(1) + foo("what") 13 | end 14 | 15 | local test = require("test") 16 | test.eq(doit(),3) -------------------------------------------------------------------------------- /tests/overload2.t: -------------------------------------------------------------------------------- 1 | local foo = terralib.overloadedfunction("foo") 2 | foo:adddefinition(terra(a : int) 3 | return 1 4 | end) 5 | 6 | foo:adddefinition(terra(a : double) 7 | return 2 8 | end) 9 | 10 | terra doit() 11 | return foo(2.5) 12 | end 13 | 14 | local test = require("test") 15 | test.eq(doit(),2) -------------------------------------------------------------------------------- /tests/overload3.t: -------------------------------------------------------------------------------- 1 | local foo = terralib.overloadedfunction("foo") 2 | foo:adddefinition(terra(a : {int} ) 3 | return 1 4 | end) 5 | 6 | foo:adddefinition(terra(a : {int,int} ) 7 | return 2 8 | end) 9 | 10 | terra doit() 11 | return foo({1,2}) + foo({1,2}) 12 | end 13 | 14 | local test = require("test") 15 | test.eq(doit(),4) -------------------------------------------------------------------------------- /tests/overloadcall.t: -------------------------------------------------------------------------------- 1 | 2 | local foo = terralib.overloadedfunction("foo") 3 | foo:adddefinition(terra(a : int) 4 | return a 5 | end) 6 | 7 | foo:adddefinition(terra(a : int, b : int) 8 | return a + b 9 | end) 10 | 11 | local test = require("test") 12 | test.eq(foo:getdefinitions()[1](1) + foo:getdefinitions()[2](3,4), 8) -------------------------------------------------------------------------------- /tests/overloadmethod.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { a : int } 4 | A.methods.foo = terralib.overloadedfunction("foo") 5 | A.methods.foo:adddefinition(terra (self : &A, a : int) 6 | return self.a + a 7 | end) 8 | 9 | A.methods.foo:adddefinition(terra (self : &A, a : &int8) 10 | return self.a 11 | end) 12 | 13 | 14 | terra doit() 15 | var a = A { 3 } 16 | return a:foo(1) + a:foo("what") 17 | end 18 | terra doit2() 19 | var a = A { 3 } 20 | var pa = &a 21 | return pa:foo(1) + pa:foo("what") 22 | end 23 | 24 | local test = require("test") 25 | test.eq(doit(),7) 26 | test.eq(doit2(),7) -------------------------------------------------------------------------------- /tests/overloadmethod2.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : int } 3 | A.methods.foo = terralib.overloadedfunction("foo") 4 | A.methods.foo:adddefinition(terra(self : A, a : int) 5 | return self.a + a 6 | end) 7 | 8 | A.methods.foo:adddefinition(terra(self :&A) 9 | return self.a 10 | end) 11 | 12 | 13 | terra doit() 14 | var a = A { 3 } 15 | return a:foo() + a:foo(1) 16 | end 17 | local test = require("test") 18 | test.eq(doit(),7) -------------------------------------------------------------------------------- /tests/overloadmethod3.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : int } 3 | 4 | A.methods.foo = terralib.overloadedfunction("foo") 5 | A.methods.foo:adddefinition(terra(self : A) 6 | return 2 7 | end) 8 | 9 | A.methods.foo:adddefinition(terra(self : &A) 10 | return 1 11 | end) 12 | 13 | 14 | terra doit() 15 | var a = A { 3 } 16 | var pa = &a 17 | return a:foo() + pa:foo() 18 | end 19 | local test = require("test") 20 | test.eq(doit(),4) -------------------------------------------------------------------------------- /tests/overloadrecv.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | A.methods.foo = terralib.overloadedfunction("foo") 8 | A.methods.foo:adddefinition(terra(self :&A, a : int, b : uint8) 9 | return 1 10 | end) 11 | A.methods.foo:adddefinition(terra(self : &A, a : double, b : uint8) 12 | return 2 13 | end) 14 | 15 | terra useit() 16 | var a = A { 3 } 17 | var pa = &a 18 | return a:foo(1,1) + a:foo(1.1,1) + pa:foo(1,1) + pa:foo(1.1,1) 19 | end 20 | 21 | assert(6 == useit()) -------------------------------------------------------------------------------- /tests/painfulrecstruct.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : A -> int; 5 | b : int 6 | } 7 | 8 | terra foo(a : A) 9 | return a.b 10 | end 11 | 12 | terra callit(a : A) 13 | return a.a(a) 14 | end 15 | 16 | terra bar() 17 | var a = A { foo, 3 } 18 | return callit(a) 19 | end 20 | 21 | local test = require("test") 22 | test.eq(bar(),3) -------------------------------------------------------------------------------- /tests/paren.t: -------------------------------------------------------------------------------- 1 | terra foo(a : int) 2 | return a + (a) 3 | end 4 | 5 | foo(3) -------------------------------------------------------------------------------- /tests/parsecrash.t: -------------------------------------------------------------------------------- 1 | local result,err = terralib.loadstring [[ 2 | terra result() 3 | [startXNeeded] = a + strip*L.stripWidth 4 | [endXNeeded] = 1 5 | end 6 | ]] 7 | assert(result ~= nil) -------------------------------------------------------------------------------- /tests/parsefail.t: -------------------------------------------------------------------------------- 1 | 2 | local self = 1 3 | local Rt = 1 4 | local i = 1 5 | local j = 1 6 | 7 | local r,e = terralib.loadstring[[ 8 | 9 | terra foo() 10 | var a = { [&double](4) = 3 } 11 | end 12 | ]] 13 | 14 | assert(r == nil and e:match("unexpected symbol near '='")) 15 | 16 | terra foo() 17 | var a = { [""] = 3 } 18 | end 19 | 20 | local s,l = symbol(int),label() 21 | 22 | local function getsym() 23 | return s 24 | end 25 | local function getlbl() return l end 26 | terra foo2() 27 | var [getsym()] = 3 28 | var a = { [getlbl()] = 4, _1 = [getsym()] } 29 | return a.[getlbl()] + a._1 30 | end 31 | 32 | assert(7 == foo2()) 33 | 34 | -------------------------------------------------------------------------------- /tests/parsefail2.t: -------------------------------------------------------------------------------- 1 | terra foo() 2 | var a : int[4] @&a[0] = 3 3 | return a[0] 4 | end 5 | assert(3 == foo()) -------------------------------------------------------------------------------- /tests/point.t: -------------------------------------------------------------------------------- 1 | 2 | terra bar(a : &int) 3 | @a = @a + 1 4 | end 5 | terra foo(a : int) : int 6 | var b : int 7 | b = a 8 | bar(&b) 9 | return b 10 | end 11 | 12 | local test = require("test") 13 | test.eq(foo(4),5) -------------------------------------------------------------------------------- /tests/pointerarith.t: -------------------------------------------------------------------------------- 1 | terra foo(a : &int) : ptrdiff 2 | var b : &int b = a + 10 3 | return b-a 4 | end 5 | 6 | terra bar() 7 | var b:int = 10; 8 | return foo(&b) 9 | end 10 | 11 | terra foo2(a : &int) : &int 12 | a = a + 6 13 | a = 3 + a 14 | a = a + 10 15 | return a 16 | end 17 | 18 | terra foo3(a : &int) : &int 19 | a = a - 11 20 | a = 2 + a 21 | a = a - 10 22 | return a 23 | end 24 | 25 | terra bar2(a : int) 26 | return @(foo3(foo2(&a))) 27 | end 28 | 29 | local test = require("test") 30 | test.eq(bar(),10) 31 | test.eq(bar2(42),42) 32 | -------------------------------------------------------------------------------- /tests/pointerlike.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | var a : int[4] 4 | var b : &int = a 5 | return a + 1, a - a, b - a, a - b, 1 + a 6 | end 7 | foo() -------------------------------------------------------------------------------- /tests/pow.t: -------------------------------------------------------------------------------- 1 | local N = 4 -- N is a Lua variable 2 | terra powN(a : double) 3 | var r = 1 4 | for i = 0, N do 5 | r = r * a 6 | end 7 | return r 8 | end 9 | 10 | 11 | 12 | 13 | local math = {} 14 | for N = 1,10 do 15 | math["pow"..tostring(N)] = terra(a : double) 16 | var r = 1 17 | for i = 0, N do 18 | r = r * a 19 | end 20 | return r 21 | end 22 | end 23 | 24 | local test = require("test") 25 | test.eq(powN(3),81) 26 | test.eq(math.pow1(2),2) 27 | test.eq(math.pow2(2),4) 28 | test.eq(math.pow3(2),8) -------------------------------------------------------------------------------- /tests/ppltalk.t: -------------------------------------------------------------------------------- 1 | 2 | --this is a comment. 3 | --top level is Lua code: 4 | function min(a,b) 5 | if a < b then return a 6 | else return b end 7 | end 8 | 9 | print(min(3,4)) --3 10 | 11 | terra mint(a : int, b : int) : int 12 | if a < b then return a 13 | else return b end 14 | end 15 | 16 | print(mint(3,4)) --3 17 | 18 | mint:disas() -------------------------------------------------------------------------------- /tests/ppnil.t: -------------------------------------------------------------------------------- 1 | terra foo() 2 | return nil 3 | end 4 | foo:printpretty() -------------------------------------------------------------------------------- /tests/pratttest1.t: -------------------------------------------------------------------------------- 1 | import "lib/pratttest" 2 | local a = goexp 1 + 3 * 4 ^ 5 ^ 6/(2 - 4 + -a) + -b(c) 3 | 4 | terralib.printraw(a) 5 | -------------------------------------------------------------------------------- /tests/prec.t: -------------------------------------------------------------------------------- 1 | local a = &double -> double 2 | assert(a:ispointer()) 3 | assert(a.type:isfunction()) 4 | assert(a.type.parameters[1]:ispointer()) -------------------------------------------------------------------------------- /tests/prec2.t: -------------------------------------------------------------------------------- 1 | terra foo(a : float, b : float, c : float, d : float) return a * (b - c) end 2 | foo:printpretty() -------------------------------------------------------------------------------- /tests/prettyprintsym.t: -------------------------------------------------------------------------------- 1 | local myStruct = terralib.types.newstruct('myStruct') 2 | local someFlagFld = label('someFlag') 3 | table.insert(myStruct.entries, {field=someFlagFld,type=bool}) 4 | myStruct : printpretty() 5 | -------------------------------------------------------------------------------- /tests/printd.t: -------------------------------------------------------------------------------- 1 | a = .4 2 | 3 | terra foo() return a end 4 | 5 | foo:printpretty(false) -------------------------------------------------------------------------------- /tests/printfarray.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | C = terralib.includec("stdio.h") 4 | 5 | terra char(a : &int8) : int8 6 | return a[0] 7 | end 8 | terra foobar() 9 | var a = arrayof(int8,char("a"),0) 10 | C.printf("%s\n",a) 11 | end 12 | foobar() -------------------------------------------------------------------------------- /tests/printfloat.t: -------------------------------------------------------------------------------- 1 | local IO = terralib.includec("stdio.h") 2 | 3 | terra foobar() 4 | IO.printf("%f\n",3.3f) 5 | end 6 | 7 | foobar() -------------------------------------------------------------------------------- /tests/proxystruct.t: -------------------------------------------------------------------------------- 1 | local c = terralib.includecstring [[ 2 | #include 3 | #include 4 | ]] 5 | 6 | 7 | struct Node { 8 | next : &Node; 9 | v : int; 10 | } 11 | 12 | terra foo() 13 | var cur : &Node = [&Node](c.malloc(sizeof(Node))) 14 | cur.v = 3 15 | return cur.v 16 | end 17 | 18 | local test = require("test") 19 | test.eq(foo(),3) 20 | -------------------------------------------------------------------------------- /tests/pt.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local a = global(double) 4 | 5 | terra getptr() 6 | return &a 7 | end 8 | 9 | local b = getptr() 10 | local c = terralib.pointertolightuserdata(b) 11 | print(b,c,a) 12 | 13 | terra foo(a : &&int) 14 | 15 | end 16 | 17 | foo(c) 18 | print("DONE") -------------------------------------------------------------------------------- /tests/quote.t: -------------------------------------------------------------------------------- 1 | 2 | local c = `10 3 | local a = `4 + c 4 | terra doit() 5 | var c = 3 6 | return a + a 7 | end 8 | 9 | local test = require("test") 10 | test.eq(doit(),28) -------------------------------------------------------------------------------- /tests/quote10.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | return 1,2 4 | end 5 | 6 | local q = `foo() 7 | 8 | terra bar() 9 | return q 10 | end 11 | 12 | local test = require("test") 13 | test.meq({1,2},bar()) -------------------------------------------------------------------------------- /tests/quote2.t: -------------------------------------------------------------------------------- 1 | 2 | local c = `10 3 | local a = `4 + c 4 | terra doit() 5 | return a 6 | end 7 | 8 | local test = require("test") 9 | test.eq(doit(),14) -------------------------------------------------------------------------------- /tests/quote3.t: -------------------------------------------------------------------------------- 1 | c = global(0) 2 | terra count() 3 | c = c + 1 4 | return c 5 | end 6 | foo = macro(function(a,b) return `a + a + a + b + count() end) 7 | terra doit() 8 | var a = -100 9 | return foo(count(),4) 10 | end 11 | 12 | local test = require("test") 13 | test.eq(doit(),14) -------------------------------------------------------------------------------- /tests/quote4.t: -------------------------------------------------------------------------------- 1 | 2 | local str2 = `{a = 4} 3 | local str = `str2 4 | terra doit() 5 | return str.a 6 | end 7 | 8 | local test = require("test") 9 | test.eq(doit(),4) -------------------------------------------------------------------------------- /tests/quote5.t: -------------------------------------------------------------------------------- 1 | 2 | c = global(0) 3 | terra foo(a : int) 4 | c = c + a 5 | end 6 | 7 | local stmts = quote 8 | foo(5) 9 | foo(6) 10 | foo(7) 11 | end 12 | 13 | local stmts2 = {stmts,stmts} 14 | terra doit() 15 | stmts 16 | stmts2 17 | return c 18 | end 19 | local test = require("test") 20 | test.eq(doit(),54) -------------------------------------------------------------------------------- /tests/quote6.t: -------------------------------------------------------------------------------- 1 | function mymacro() 2 | return {`4,`5} 3 | end 4 | mymacro = macro(mymacro) 5 | 6 | local exps = {`2,`3, `mymacro()} 7 | 8 | terra doit() 9 | return exps 10 | end 11 | local test = require("test") 12 | test.meq({2,3,4,5},doit()) -------------------------------------------------------------------------------- /tests/quote7.t: -------------------------------------------------------------------------------- 1 | a = {} 2 | 3 | local c = terralib.includec("stdio.h") 4 | 5 | 6 | a.c = quote 7 | c.printf("hello\n") 8 | end 9 | 10 | a.b = quote 11 | var d = 4 12 | a.c 13 | return d 14 | end 15 | 16 | 17 | terra foo() 18 | a.c 19 | a.b 20 | end 21 | 22 | local test = require("test") 23 | test.eq(foo(),4) -------------------------------------------------------------------------------- /tests/quote8.t: -------------------------------------------------------------------------------- 1 | a = {} 2 | 3 | local c = terralib.includec("stdio.h") 4 | 5 | 6 | a.c = {`1,`2,`3} 7 | 8 | a.b = quote 9 | return a.c,a.c 10 | end 11 | 12 | 13 | terra foo() 14 | a.b 15 | end 16 | 17 | 18 | local test = require("test") 19 | test.meq({1,2,3,1,2,3},foo()) 20 | -------------------------------------------------------------------------------- /tests/quote9.t: -------------------------------------------------------------------------------- 1 | a = {} 2 | 3 | local c = terralib.includec("stdio.h") 4 | 5 | 6 | a.c = {`1,`2,`3} 7 | 8 | a.b = quote 9 | return a.c._0,(a.c)._0 10 | end 11 | 12 | 13 | terra foo() 14 | a.b 15 | end 16 | 17 | 18 | local test = require("test") 19 | test.meq({1,1}, foo()) -------------------------------------------------------------------------------- /tests/quoteblock.t: -------------------------------------------------------------------------------- 1 | 2 | local a = symbol(int) 3 | local myquote = quote 4 | var [a] = 3 5 | end 6 | 7 | terra bar() 8 | [myquote]; 9 | return [a] 10 | end 11 | 12 | print(bar()) -------------------------------------------------------------------------------- /tests/quoteenv.t: -------------------------------------------------------------------------------- 1 | --quotes, unlike functions bind symbols eagerly 2 | --function have :compile() to bind their symbols, but no similar thing exists 3 | --for quotes, making it hard to control 4 | --furthermore there aren't many use cases for late-binding quotes, 5 | --but a bunch exist for needing early-bound quotes, like below: 6 | 7 | 8 | function times5(x) 9 | local c = `0 10 | for i = 1,5 do 11 | c = `c + x 12 | end 13 | return c 14 | end 15 | times5 = macro(times5) 16 | 17 | terra foo() 18 | var a = 3 19 | return times5(a) 20 | end 21 | 22 | foo:printpretty() 23 | foo:disas() 24 | local test = require("test") 25 | test.eq(foo(),15) 26 | -------------------------------------------------------------------------------- /tests/quoteselect.t: -------------------------------------------------------------------------------- 1 | 2 | local a = {} 3 | 4 | local c = terralib.includec("stdio.h") 5 | 6 | b = `c.printf("hello\n") 7 | 8 | terra foo() 9 | return b 10 | end 11 | 12 | foo() -------------------------------------------------------------------------------- /tests/rd.t: -------------------------------------------------------------------------------- 1 | C = terralib.includec("stdarg.h") -------------------------------------------------------------------------------- /tests/receivercasts.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : int } 3 | 4 | terra A.methods.foo(a : int) 5 | return a + 1 6 | end 7 | 8 | function A.metamethods.__cast(from,to,exp) 9 | if from == A and to == int then 10 | return `exp.a 11 | end 12 | error("what") 13 | end 14 | 15 | 16 | 17 | terra testit() 18 | var a = A { 5 } 19 | return a:foo() 20 | end 21 | 22 | assert(6 == testit()) -------------------------------------------------------------------------------- /tests/recfn.t: -------------------------------------------------------------------------------- 1 | local terra foo() : {} 2 | bar() 3 | end 4 | local terra bar() : {} 5 | foo() 6 | end 7 | 8 | bar = nil 9 | 10 | foo:printpretty() -------------------------------------------------------------------------------- /tests/recoverfromerror.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | dostuff = macro(function() 5 | pcall(function() 6 | terra what() 7 | return " " / 1 8 | end 9 | end) 10 | return 4 11 | end) 12 | 13 | terra foobar() 14 | return dostuff() 15 | end 16 | assert(4 == foobar()) -------------------------------------------------------------------------------- /tests/recstruct.t: -------------------------------------------------------------------------------- 1 | local c = terralib.includecstring [[ 2 | #include 3 | #include 4 | ]] 5 | 6 | 7 | struct Node { 8 | next : &Node; 9 | v : int; 10 | } 11 | 12 | local N = 10 13 | terra foo() 14 | var cur : &Node = nil 15 | for i = 0, N do 16 | var n = [&Node](c.malloc(sizeof(Node))) 17 | n.v = i 18 | n.next = cur 19 | cur = n 20 | end 21 | var sum = 0 22 | while cur ~= nil do 23 | c.printf("%d\n",cur.v) 24 | sum = sum + cur.v 25 | var old = cur 26 | cur = cur.next 27 | c.free(old) 28 | end 29 | return sum 30 | end 31 | 32 | local test = require("test") 33 | test.eq(foo(),N * (N - 1) / 2) -------------------------------------------------------------------------------- /tests/recstruct2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local struct A { b : &B } 4 | struct B { a : &A } 5 | 6 | struct C { a : &A, b : &B, c : &C } 7 | 8 | local struct D {} 9 | 10 | terra foo() 11 | var a : A, b : B, c : C 12 | a.b = &b 13 | b.a = &a 14 | c.c = &c 15 | end 16 | 17 | foo() -------------------------------------------------------------------------------- /tests/rename.t: -------------------------------------------------------------------------------- 1 | terralib.includecstring [[ 2 | int foo() { return 3; } 3 | ]] 4 | 5 | terra what() return 4 end 6 | 7 | 8 | terralib.saveobj("foo.o",{foo = what}) -------------------------------------------------------------------------------- /tests/renaming.t: -------------------------------------------------------------------------------- 1 | 2 | C = terralib.includec("stdio.h") 3 | terra main() 4 | C.printf("what\n") 5 | end 6 | main:setinlined(false) 7 | 8 | terra realmain() 9 | main() 10 | end 11 | 12 | terralib.saveobj("renamed",{ main = realmain }) 13 | 14 | terralib.dumpmodule() -------------------------------------------------------------------------------- /tests/requiretwice.t: -------------------------------------------------------------------------------- 1 | A = require("lib.golike") 2 | B = require("lib.golike") 3 | assert(A == B) -------------------------------------------------------------------------------- /tests/rvaluerecv.t: -------------------------------------------------------------------------------- 1 | struct A { a : int } 2 | 3 | terra A:foo() 4 | self.a = self.a + 1 5 | return self.a 6 | end 7 | 8 | terra A.methods.foo2(self : A) 9 | self.a = self.a + 1 10 | return self.a 11 | end 12 | 13 | 14 | mya = global(A) 15 | mya:set({0}) 16 | 17 | terra geta() 18 | return mya 19 | end 20 | 21 | terra bar() 22 | var v0 = mya.a 23 | var v1 = geta():foo() 24 | var v2 = mya.a 25 | var v3 = geta():foo2() 26 | var v4 = mya.a 27 | return v0,v1,v2,v3,v4 28 | end 29 | test = require("test") 30 | test.meq({0,1,0,1,0},bar()) -------------------------------------------------------------------------------- /tests/scope.t: -------------------------------------------------------------------------------- 1 | terra scope() 2 | var a = 4 3 | var b = 0 4 | b = b + a 5 | do 6 | var a = 5 7 | b = b + a 8 | end 9 | b = b + a 10 | return b 11 | end 12 | local test = require('test') 13 | test.eq(scope(),13) -------------------------------------------------------------------------------- /tests/selectoverload.t: -------------------------------------------------------------------------------- 1 | struct Vec { data : float[3] } 2 | 3 | 4 | local get = {} 5 | get.x = terra(self : &Vec) 6 | return self.data[0] 7 | end 8 | get.y = macro(function(self) 9 | return `self.data[1] 10 | end) 11 | 12 | Vec.metamethods.__entrymissing = macro(function(name,self) 13 | return `[get[name]](&self) 14 | end) 15 | 16 | terra bar() 17 | var a = Vec { array(1.f,2.f,3.f) } 18 | a.y = a.y + 1 19 | var pa = &a 20 | return a.x + a.y + pa.x 21 | end 22 | 23 | local test = require("test") 24 | test.eq(bar(),5) 25 | 26 | -------------------------------------------------------------------------------- /tests/setname.t: -------------------------------------------------------------------------------- 1 | 2 | two = terra() 3 | return 1 + 1 4 | end 5 | two:setname("two") 6 | 7 | two:disas() -------------------------------------------------------------------------------- /tests/shallowfreeze.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct B { 4 | d : int 5 | } 6 | struct A { 7 | b : &B; 8 | c : int; 9 | d : int; 10 | e : int; 11 | } 12 | 13 | 14 | terra foo() : A 15 | return A {nil} 16 | end 17 | 18 | foo:disas() 19 | 20 | print(foo()) 21 | 22 | 23 | 24 | terralib.dumpmodule() -------------------------------------------------------------------------------- /tests/sharedlib.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo(a : int, b : int) 4 | return a + b 5 | end 6 | 7 | local ffi = require 'ffi' 8 | 9 | local name = (ffi.os == "Windows" and "foo.dll" or "foo.so") 10 | 11 | local args = {} 12 | 13 | if ffi.os == "Windows" then 14 | args = {"/IMPLIB:foo.lib","/EXPORT:foo" } 15 | end 16 | 17 | terralib.saveobj(name,{ foo = foo }, args) 18 | 19 | local foo2 = terralib.externfunction("foo", {int,int} -> int ) 20 | terralib.linklibrary("./"..name) 21 | 22 | assert(4 == foo2(1,3)) 23 | -------------------------------------------------------------------------------- /tests/shift.t: -------------------------------------------------------------------------------- 1 | local test = require("test") 2 | 3 | terra foo(a : int) 4 | return 1 << 2, a >> 1, -4 >> 1, uint32(-a) >> 1 5 | end 6 | 7 | test.meq({4,2,-2,2147483646},foo(4)) 8 | -------------------------------------------------------------------------------- /tests/signext.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra bar() 4 | var a : uint8 = 255 5 | var b = foo(a) 6 | return a == b 7 | end 8 | terra foo(a : uint8) 9 | return a 10 | end 11 | 12 | print(bar()) -------------------------------------------------------------------------------- /tests/simple.t: -------------------------------------------------------------------------------- 1 | 2 | terra simple(a : int) 3 | return a + a 4 | end 5 | local test = require("test") 6 | test.eq(simple(2),4) -------------------------------------------------------------------------------- /tests/simpleadd.t: -------------------------------------------------------------------------------- 1 | struct Complex { real : float, imag : float } 2 | 3 | terra Complex.metamethods.__add(a : Complex, b : Complex) 4 | return Complex { a.real + b.real, a.imag + b.imag } 5 | end 6 | 7 | 8 | terra foo() 9 | var a = Complex { 1, 2 } 10 | var b = Complex { 3, 4 } 11 | var c = a + b 12 | return c.real,c.imag 13 | end 14 | 15 | print(foo()) -------------------------------------------------------------------------------- /tests/simpleapply.t: -------------------------------------------------------------------------------- 1 | struct Vec { data : int[4] } 2 | Vec.metamethods.__apply = terra(self : &Vec, i : int) 3 | return self.data[i] 4 | end 5 | 6 | struct Vec2 { data : int[4] } 7 | Vec2.metamethods.__apply = macro(function(self,b) 8 | return `self.data[b] 9 | end) 10 | 11 | terra bar() 12 | var a = Vec { array(1,2,3,4) } 13 | var b = Vec2 { array(1,2,3,4) } 14 | b(2) = b(2) + 1 15 | return b(2) + a(2) 16 | end 17 | 18 | local test = require("test") 19 | test.eq(bar(),7) -------------------------------------------------------------------------------- /tests/simpleglobal.t: -------------------------------------------------------------------------------- 1 | 2 | local a = terralib.global(int) 3 | local b = terralib.global(5) 4 | local c = terralib.global(int,3) 5 | terra foo() 6 | a = 4 7 | end 8 | 9 | 10 | terra bar() 11 | return c + a + b 12 | end 13 | 14 | foo() 15 | assert(bar() == 12) 16 | 17 | b:set(4) 18 | 19 | assert(bar() == 11) -------------------------------------------------------------------------------- /tests/simplerec.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | local terra bar() 5 | return 1 6 | end 7 | struct A { 8 | a : int 9 | } 10 | local struct B { 11 | a : int 12 | } 13 | terra B:foo() 14 | return self.a 15 | end 16 | terra foo() 17 | var b , a = B{4}, A{5} 18 | return 1 + b:foo() 19 | end 20 | local terra mydecl :: {} -> {} 21 | struct mystructdecl 22 | 23 | print(bar()) 24 | 25 | print(foo()) 26 | -------------------------------------------------------------------------------- /tests/simplestruct.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { 3 | a : int 4 | } 5 | 6 | terra foo(a : &A) 7 | return a 8 | end 9 | 10 | print(foo(terralib.new(A,{3}))) 11 | -------------------------------------------------------------------------------- /tests/simplevec.t: -------------------------------------------------------------------------------- 1 | 2 | terra sum(input : &float, N : int) 3 | var acc : vector(float,4) = vector(0.f,0.f,0.f,0.f) 4 | for i = 0,N,4 do 5 | --cast the floats to float4s and load 6 | var entry = @[&vector(float,4)](input + i) 7 | acc = acc + entry 8 | end 9 | return acc[0] + acc[1] + acc[2] + acc[3] 10 | end 11 | 12 | sum:disas() 13 | -------------------------------------------------------------------------------- /tests/sintable.t: -------------------------------------------------------------------------------- 1 | local N = 32 2 | 3 | local tbl = terralib.newlist() 4 | for i = 1,N do 5 | tbl:insert(math.sin( 2 * math.pi * (i-1)/N)) 6 | end 7 | local ctable = terralib.constant(`arrayof(float,[tbl])) 8 | 9 | terra sintable(a : float) : float 10 | var idx = int(a / (2 * math.pi) * N) 11 | return ctable[idx] 12 | end 13 | 14 | sintable:disas() 15 | 16 | print(sintable(0)) 17 | print(sintable(math.pi/4)) -------------------------------------------------------------------------------- /tests/speed.t: -------------------------------------------------------------------------------- 1 | local C = terralib.includecstring [[ 2 | #include 3 | #include 4 | ]] 5 | 6 | terra doit(N : int64) 7 | var cur,last = 1ULL,1ULL 8 | for i = 0ULL, (N-2ULL) do 9 | cur,last = cur+last,cur 10 | end 11 | return cur 12 | end 13 | terra main(argc : int, argv : &&int8) 14 | var N = 4ULL 15 | if argc == 2 then 16 | N = C.atoi(argv[1]) 17 | end 18 | var result = doit(N) 19 | C.printf("%lld\n",result) 20 | end 21 | 22 | terra what() 23 | return C.atoi("54") 24 | end 25 | 26 | local test = require("test") 27 | print(what()) 28 | print(test.time( function() doit:compile() end)) 29 | print(test.time( function() doit(100000000) end)) 30 | print(test.time( function() terralib.saveobj("speed",{main = main}) end)) 31 | -------------------------------------------------------------------------------- /tests/splitprimary.t: -------------------------------------------------------------------------------- 1 | r = 1 2 | local function foo(a) 3 | r = a 4 | return r 5 | end 6 | local b = foo(1) 7 | (foo)(3) 8 | assert(b + r == 4) 9 | 10 | terra testescapes() 11 | var a = [4] 12 | [ quote 13 | a = a + 1 14 | end 15 | ] 16 | return [ `&a ][0] 17 | end 18 | 19 | assert(testescapes() == 5) -------------------------------------------------------------------------------- /tests/splitquote.t: -------------------------------------------------------------------------------- 1 | local _,err = terralib.loadstring("moo\nquote\nend") 2 | assert(err:match("unexpected symbol near 'quote'")) -------------------------------------------------------------------------------- /tests/staticmethod.t: -------------------------------------------------------------------------------- 1 | struct A { 2 | } 3 | A.metamethods.__getmethod = function(self,idx) 4 | return tonumber(string.sub(idx,2,-1)) 5 | end 6 | terra foo() 7 | return A.f33 + A.b34 8 | end 9 | assert(67 == foo()) -------------------------------------------------------------------------------- /tests/stattest.t: -------------------------------------------------------------------------------- 1 | -- WARNING: any changes to this file need to change this constant to match! 2 | local stattest_t_file_size = 480 3 | 4 | local ffi = require("ffi") 5 | -- Windows doesn't have stat() and FreeBSD's version of struct stat uses 6 | -- bit fields, which terra doesn't seem to handle 7 | if ffi.os == "Windows" or ffi.os == "BSD" then 8 | return 9 | end 10 | 11 | C,T = terralib.includec("sys/stat.h") 12 | terra dostat() 13 | var s : T.stat 14 | C.stat("stattest.t",&s) 15 | return s.st_size 16 | end 17 | 18 | assert(dostat() == stattest_t_file_size) 19 | -------------------------------------------------------------------------------- /tests/stdio.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring[[ 2 | #include 3 | inline int testfunc(int a, int b) { return a + b; }]] 4 | 5 | terra foobar() : int 6 | var a : int = 5 7 | C.printf("C: %i\n", C.testfunc(a, 9)) 8 | return C.testfunc(a, 9) 9 | end 10 | assert(foobar() == 14) 11 | 12 | terralib.saveobj("stdio.exe", {main = foobar}) -------------------------------------------------------------------------------- /tests/string.t: -------------------------------------------------------------------------------- 1 | 2 | local c = terralib.includec("stdio.h") 3 | 4 | terra foo() 5 | var a = "whatwhat\n" 6 | return c.puts(a) 7 | end 8 | 9 | local test = require("test") 10 | local ffi = require("ffi") 11 | if ffi.os == "Windows" then 12 | test.eq(foo(),0) 13 | else 14 | test.eq(foo(),10) 15 | end -------------------------------------------------------------------------------- /tests/stringconst.t: -------------------------------------------------------------------------------- 1 | local C = terralib.includecstring [[ 2 | extern int printf (const char *__restrict __format, ...); 3 | void testC() { 4 | printf("C\n"); 5 | } 6 | void testC2() { 7 | printf("C\n"); 8 | } 9 | ]] 10 | local terra testTerra() 11 | C.printf("Terra\n") 12 | end 13 | testTerra() 14 | C.testC() 15 | C.testC2() 16 | terralib.dumpmodule(terralib.jitcompilationunit.llvm_cu) -------------------------------------------------------------------------------- /tests/structarg.t: -------------------------------------------------------------------------------- 1 | terra foobar() 2 | var a = { a=3,b=4} 3 | var b = {a=5,b=6.0} 4 | b = a 5 | return a.a 6 | end 7 | 8 | local test = require("test") 9 | test.eq(foobar(),3) -------------------------------------------------------------------------------- /tests/structcast.t: -------------------------------------------------------------------------------- 1 | struct A { a : int } 2 | struct B {a : int, b : A} 3 | 4 | local D = tuple(double, A) 5 | 6 | terra anon() 7 | var b : B 8 | b.a = 4 9 | b.b.a = 3 10 | 11 | var d : D 12 | d._0 = 1.0 13 | d._1.a = 2 14 | 15 | b = B(d) 16 | 17 | return b.a + b.b.a 18 | end 19 | 20 | terra anon2() 21 | var b = B { b = A{ 2.0 }, a = 4 } 22 | var b2 = B { a = 4, b = A{ 2.0 } } 23 | var b3 = B{ 4, A{2.0} } 24 | return b.a + b.b.a + b2.a + b2.b.a + b3.a + b3.b.a 25 | end 26 | 27 | terra anon3() 28 | return {5,A{6}} 29 | end 30 | 31 | terra anon4() 32 | var b = B(anon3()) 33 | return b.a + b.b.a 34 | end 35 | test = require("test") 36 | test.eq(anon(),3) 37 | test.eq(anon2(),18) 38 | test.eq(anon4(),11) -------------------------------------------------------------------------------- /tests/structconstructor.t: -------------------------------------------------------------------------------- 1 | struct A { a : int, b : int} 2 | 3 | terra foobar() 4 | var a = A { 3, 4} 5 | return a.b 6 | end 7 | 8 | local test = require("test") 9 | test.eq(foobar(),4) -------------------------------------------------------------------------------- /tests/structrvalue.t: -------------------------------------------------------------------------------- 1 | --the zero line 2 | struct A { b : B } 3 | struct B {a : int, b : int} 4 | 5 | 6 | terra foo() 7 | var b = B {1,2} 8 | return b 9 | end 10 | 11 | terra bar() 12 | return foo().b 13 | end 14 | 15 | local test = require("test") 16 | test.eq(bar(),2) -------------------------------------------------------------------------------- /tests/structsyntax.t: -------------------------------------------------------------------------------- 1 | struct A { 2 | a : int 3 | b : float 4 | union { 5 | c : int 6 | d : float 7 | } 8 | } 9 | 10 | terra foo() 11 | return A { a = 4, b = 5, c = 3} 12 | end 13 | 14 | assert(foo().c == 3) -------------------------------------------------------------------------------- /tests/stuff.t: -------------------------------------------------------------------------------- 1 | function foo(T) 2 | local fn = terra(base : &int8, size : int64) : T 3 | -- works if attributes are an immediate table 4 | return terralib.attrload([&T](base + 64), { isvolatile = true }) 5 | end 6 | return fn 7 | end 8 | 9 | function bar(T, attrs) 10 | local fn = terra(base : &int8, size : int64) : T 11 | -- doesn't work if attributs are a table that's passed in 12 | return terralib.attrload([&T](base + 64), attrs) 13 | end 14 | return fn 15 | end 16 | 17 | foo(int64):disas() 18 | bar(int64, `{ isvolatile = true }):disas() -------------------------------------------------------------------------------- /tests/sugar.t: -------------------------------------------------------------------------------- 1 | 2 | local foo = &int 3 | 4 | 5 | local a = int -> {bool,int} 6 | 7 | print(a.name) 8 | 9 | terra foo2() 10 | 11 | end 12 | 13 | 14 | 15 | local test = require("test") 16 | -------------------------------------------------------------------------------- /tests/sumlanguage1.t: -------------------------------------------------------------------------------- 1 | import "lib/sumlanguage" 2 | assert((sum 1.0f,2LL,2ULL,3.0,1U,1 done)==10) -------------------------------------------------------------------------------- /tests/sumlanguage2.t: -------------------------------------------------------------------------------- 1 | import "lib/sumlanguage2" 2 | a = 2 3 | local b = 3 4 | assert(sum 1,a,b done == 6) -------------------------------------------------------------------------------- /tests/symbolmangling.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | #include 3 | ]] 4 | C.fopen("broken.t","r") 5 | -------------------------------------------------------------------------------- /tests/symbolvar.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local a = symbol(int) 4 | 5 | terra foo() 6 | var [a] = 3 7 | return [a] 8 | end 9 | 10 | local test = require("test") 11 | test.eq(3,foo()) -------------------------------------------------------------------------------- /tests/symbolvar2.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local a = symbol(int) 4 | 5 | local q = quote 6 | [a] = [a] + 1 7 | end 8 | 9 | terra foo() 10 | var [a] = 2 11 | q 12 | return [a] 13 | end 14 | 15 | local test = require("test") 16 | test.eq(3,foo()) -------------------------------------------------------------------------------- /tests/symbolvar3.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local a = symbol(int) 4 | 5 | local q = quote 6 | var [a] = 2 7 | [a] = [a] + 1 8 | end 9 | 10 | terra foo() 11 | q 12 | return [a] 13 | end 14 | 15 | local test = require("test") 16 | test.eq(3,foo()) -------------------------------------------------------------------------------- /tests/symbolvar4.t: -------------------------------------------------------------------------------- 1 | local a = symbol(double) 2 | local b = symbol(int) 3 | 4 | local c = symbol(int) 5 | local d = symbol(double) 6 | 7 | 8 | terra foo() 9 | var [a],[b] = 1.25,1.25 10 | var [c],[d] = 3.25,3.25 11 | return [a] + [b] + [c] + [d] 12 | end 13 | 14 | local test = require("test") 15 | 16 | test.eq(foo(),8.50) -------------------------------------------------------------------------------- /tests/symbolvar5.t: -------------------------------------------------------------------------------- 1 | local a = symbol(double) 2 | local b = { symbol(int), symbol(int) } 3 | 4 | local c = symbol(int) 5 | local d = { symbol(double), symbol(double) } 6 | 7 | 8 | terra foo() 9 | var [a],[b] = 1.25,1.25,1.25 10 | var [c],[d] = 3.25,3.25,3.25 11 | return [a] + [b[1]] + [b[2]] + [c] + [d[1]] + [d[2]] 12 | end 13 | 14 | local test = require("test") 15 | 16 | test.eq(foo(),12.75) -------------------------------------------------------------------------------- /tests/symbolvar6.t: -------------------------------------------------------------------------------- 1 | local a = symbol(double) 2 | local b = {} 3 | 4 | local c = symbol(int) 5 | local d = {} 6 | 7 | 8 | terra foo() 9 | var [a],[b] = 1.25 10 | var [c],[d] = 3.25 11 | return [a] + [c] 12 | end 13 | 14 | local test = require("test") 15 | 16 | test.eq(foo(),4.25) -------------------------------------------------------------------------------- /tests/symbolvar7.t: -------------------------------------------------------------------------------- 1 | local a = symbol(double) 2 | local b = { symbol(int), symbol(int) } 3 | 4 | local c = symbol(int) 5 | local d = symbol(double) 6 | 7 | terra foo() 8 | var [a],[b] = 1.25,1.25,1.25 9 | var [c],[d] = 3.25,3.25 10 | return [a] + [b[1]] + [b[2]] + [c] + [d] 11 | end 12 | 13 | local test = require("test") 14 | 15 | test.eq(foo(),9.50) -------------------------------------------------------------------------------- /tests/symparam.t: -------------------------------------------------------------------------------- 1 | 2 | local a = symbol(int) 3 | terra foo([a], b : int) 4 | return [a] + b 5 | end 6 | 7 | local test = require("test") 8 | 9 | test.eq(foo(1,2),3) -------------------------------------------------------------------------------- /tests/symparam2.t: -------------------------------------------------------------------------------- 1 | 2 | local a = symbol(int) 3 | terra foo([a], [a], b : int) 4 | return [a] + b 5 | end 6 | 7 | local test = require("test") 8 | 9 | test.eq(foo(1,2,4),6) -------------------------------------------------------------------------------- /tests/symparam3.t: -------------------------------------------------------------------------------- 1 | 2 | local a = symbol(int) 3 | local c = {} 4 | terra foo([a], b : int, [c]) 5 | return [a] + b 6 | end 7 | local d = {symbol(int),symbol(int)} 8 | terra foo2([a], b : int, [d]) 9 | return [a] + b + [d[1]] + [d[2]] 10 | end 11 | 12 | local test = require("test") 13 | 14 | test.eq(foo2(1,2,3,4),10) 15 | test.eq(foo(1,2),3) -------------------------------------------------------------------------------- /tests/tag.tt: -------------------------------------------------------------------------------- 1 | #!../terra 2 | print("FOO") -------------------------------------------------------------------------------- /tests/takedefinition.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo(a : int) return a + 3 end 4 | 5 | terra bar(a : int) : int 6 | if a == 0 then return 0 7 | else return bar(a - 1) + 1 end 8 | end 9 | 10 | foo:resetdefinition(bar) 11 | 12 | assert(bar(7) == 7) 13 | assert(foo(5) == 5) 14 | 15 | 16 | assert(foo:compile() == bar:compile()) 17 | 18 | bar:disas() -------------------------------------------------------------------------------- /tests/template.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | 4 | end 5 | 6 | local test = require("test") 7 | -------------------------------------------------------------------------------- /tests/terracast.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | function addone(a) 5 | return a + 1 6 | end 7 | 8 | 9 | local a1 = terralib.cast(int -> int, addone) 10 | 11 | terra dofn(a : int -> int) 12 | return a(3) 13 | end 14 | local test = require("test") 15 | test.eq(dofn(a1),4) -------------------------------------------------------------------------------- /tests/terralua.t: -------------------------------------------------------------------------------- 1 | function dog(...) 2 | print("Hi. This is Dog.",...) 3 | end 4 | dog0 = terralib.cast({} -> {}, dog) 5 | terra foo() 6 | dog0() 7 | end 8 | 9 | dog4 = terralib.cast({int,int,int,&opaque} -> {}, dog) 10 | terra passanarg() 11 | dog4(3,4,5,nil) 12 | end 13 | 14 | function takesastruct(a) 15 | print(a.a,a.b) 16 | a.a = a.a + 1 17 | end 18 | struct A { a : int , b : double } 19 | takesastruct = terralib.cast(&A -> {},takesastruct) 20 | terra passastruct() 21 | var a = A {1,3.4} 22 | --takesastruct(a) luajit doesn't like having structs passed to its callbacks by value? 23 | var b = a.a 24 | takesastruct(&a) 25 | var c = a.a 26 | return b + c 27 | end 28 | 29 | foo() 30 | passanarg() 31 | 32 | local test = require("test") 33 | 34 | test.eq(passastruct(),3) 35 | -------------------------------------------------------------------------------- /tests/terraluamethod.t: -------------------------------------------------------------------------------- 1 | struct A { a : int } 2 | 3 | local up = function(self,b) 4 | self.a = self.a + 1 + (b or 0) 5 | end 6 | A.methods.up0 = terralib.cast(&A -> {},up) 7 | A.methods.up1 = terralib.cast({&A,int} -> {}, up) 8 | 9 | terra foo() 10 | var a = A { 1 } 11 | a:up0() 12 | var b = &a 13 | b:up1(4) 14 | return a.a 15 | end 16 | 17 | local test = require("test") 18 | test.eq(foo(),7) -------------------------------------------------------------------------------- /tests/terranew.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | A = terralib.new(int[4],{1,2,3,4}) 4 | 5 | terra foo(a : &int) 6 | var sum = 0 7 | for i = 0,4 do 8 | sum = sum + a[i] 9 | end 10 | return sum,a 11 | end 12 | 13 | local test = require("test") 14 | 15 | local a,b = terralib.unpackstruct(foo(A)) 16 | 17 | test.eq(a,10) 18 | test.eq(terralib.typeof(A),int[4]) 19 | test.eq(terralib.typeof(b),&int) -------------------------------------------------------------------------------- /tests/test.lua: -------------------------------------------------------------------------------- 1 | local test = {} 2 | 3 | function test.eq(a,b) 4 | if a ~= b then 5 | error(tostring(a) .. " ~= " .. tostring(b),2) 6 | end 7 | end 8 | function test.neq(a,b) 9 | if a == b then 10 | error(tostring(a) .. " == " .. tostring(b),2) 11 | end 12 | end 13 | function test.meq(a,b) 14 | local lst = {terralib.unpackstruct(b)} 15 | if #lst ~= #a then 16 | error("size mismatch",2) 17 | end 18 | for i,e in ipairs(a) do 19 | if e ~= lst[i] then 20 | error(tostring(i) .. ": "..tostring(e) .. " ~= " .. tostring(lst[i]),2) 21 | end 22 | end 23 | end 24 | 25 | function test.time(fn) 26 | local s = os.clock() 27 | fn() 28 | local e = os.clock() 29 | return e - s 30 | end 31 | return test -------------------------------------------------------------------------------- /tests/testimport.t: -------------------------------------------------------------------------------- 1 | 2 | local r,e = terralib.loadstring [[ 3 | import "lib.fakeimport" 4 | ]] 5 | 6 | assert(e:match("stack traceback")) -------------------------------------------------------------------------------- /tests/testlang1.t: -------------------------------------------------------------------------------- 1 | import "lib/testlang" 2 | 3 | print("hello") 4 | local a = image "astring" 5 | image 4 6 | image and,or,& 7 | image image 8 | image akeyword 9 | image foobar 10 | print(a) 11 | image bar 3 12 | print(bar,bar1) 13 | 14 | local image what 4 3 15 | print(what) 16 | local a = 4 17 | local b = 5 18 | c = 6 19 | print(foolist { a, b, c}) 20 | --test eos token 21 | image 22 | -------------------------------------------------------------------------------- /tests/testlang2.t: -------------------------------------------------------------------------------- 1 | import "lib/testlang" 2 | 3 | local a = image "astring" 4 | print(a) 5 | -------------------------------------------------------------------------------- /tests/testlog.t: -------------------------------------------------------------------------------- 1 | terra foo(a : bool, b : bool, c : bool) return a and b or c end 2 | 3 | foo:disas() 4 | terra callfoo() 5 | var r = foo(true,false,true) 6 | return r 7 | end 8 | assert(callfoo()) -------------------------------------------------------------------------------- /tests/testrequire.t: -------------------------------------------------------------------------------- 1 | local f = assert(io.popen("uname", 'r')) 2 | local s = assert(f:read('*a')) 3 | f:close() 4 | 5 | if s~="Darwin\n" then 6 | print("Warning, not running test b/c this isn't a mac") 7 | else 8 | 9 | local A = require("lib.objc") 10 | local B = require("lib.objc") 11 | 12 | assert(A == B) 13 | 14 | end -------------------------------------------------------------------------------- /tests/teststd.t: -------------------------------------------------------------------------------- 1 | local S = require "std" 2 | 3 | c = global(int,0) 4 | 5 | struct A(S.Object) { 6 | a : int; 7 | b : int[5]; 8 | } 9 | struct C { 10 | a : int 11 | } 12 | struct B(S.Object) { 13 | a : A; 14 | b : A[2]; 15 | c : C[3]; 16 | c2 : C; 17 | c3 : C[2]; 18 | } 19 | 20 | terra A:__destruct() 21 | S.printf("A dtor!\n") 22 | c = c + 1 23 | end 24 | terra B:__destruct() 25 | c = c + 10 26 | end 27 | terra foo() 28 | var a = A.salloc() 29 | end 30 | 31 | foo() 32 | assert(c:get() == 1) 33 | 34 | terra bar() 35 | var b = B.salloc() 36 | end 37 | bar() 38 | assert(c:get() == 14) 39 | 40 | terra baz() 41 | var b = B.alloc() 42 | b:delete() 43 | end 44 | baz() 45 | assert(c:get() == 27) -------------------------------------------------------------------------------- /tests/toterraexpression.t: -------------------------------------------------------------------------------- 1 | local x = setmetatable({a = 4}, { __toterraexpression = function(self) return self.a end}) 2 | 3 | 4 | local terra foo() 5 | return x 6 | end 7 | 8 | assert(foo() == 4) -------------------------------------------------------------------------------- /tests/twolang.t: -------------------------------------------------------------------------------- 1 | 2 | do 3 | import "lib/foolang" 4 | a = foo bar 5 | import "lib/barlang" 6 | b = bar foo 7 | assert( foo bar + bar foo == 3) 8 | assert(a + b == 3) 9 | end 10 | 11 | local function failparse(str,match) 12 | local r,msg = terralib.loadstring(str) 13 | assert(not r) 14 | local match = msg:match(match) 15 | if not match then print(msg) end 16 | assert(match) 17 | end 18 | 19 | failparse("return (foo bar + bar foo)", "near 'bar'") 20 | failparse('import "lib/foolang";return (foo bar + bar foo)', "near foo") 21 | failparse('import "lib/foolang"; import "lib/foolang";',"entrypoint 'foo' already defined") 22 | 23 | do import "lib/foolang" end do import "lib/foolang" end -------------------------------------------------------------------------------- /tests/typeexp.t: -------------------------------------------------------------------------------- 1 | atype = int 2 | terra foo(a : atype) 3 | var b : &atype = &a 4 | return @b 5 | end 6 | 7 | local test = require("test") 8 | 9 | test.eq(foo(3),3) -------------------------------------------------------------------------------- /tests/union.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : int, union { c : int8[6], b : int }, d : float, union { e : int, f : float }, g : int } 3 | struct B { union { b : int , c : int8[6] }, a : int } 4 | local C = struct { union { union { b : int} , c : int8[6] }, w : int } 5 | terra foo() 6 | var a : A 7 | var b : B 8 | var c : C 9 | a.b = 4 10 | a.a = 5 11 | a.c[4] = 3 12 | a.d = 4 13 | return a.a + a.b + a.c[4] + a.d 14 | end 15 | 16 | struct Stuff { union { a : int, b : float } } 17 | 18 | terra what() 19 | var a : Stuff, b : Stuff 20 | a.a,b.a = 3,4 21 | return a.b/b.b --== .75, this is the best bad interview question 22 | end 23 | 24 | local test = require("test") 25 | test.eq(foo(),16) 26 | test.eq(what(),.75) -------------------------------------------------------------------------------- /tests/unm.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | struct A { 4 | a : int 5 | } 6 | 7 | A.metamethods.__unm = terra(self : &A) 8 | return A { -self.a } 9 | end 10 | 11 | A.metamethods.__sub = terra(self : &A, rhs : &A) 12 | return A { self.a - rhs.a } 13 | end 14 | 15 | terra doit() 16 | var a,b = A { 1 } , A { 2 } 17 | return (-(a - b)).a 18 | end 19 | 20 | assert(doit() == 1) -------------------------------------------------------------------------------- /tests/unpacktuple.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | terra foo() 4 | return 1,2 5 | end 6 | 7 | terra foo2() 8 | return {a = 1, b = 2} 9 | end 10 | 11 | assert(unpacktuple(1) == 1) 12 | assert(unpacktuple(foo()) == 1) 13 | 14 | assert(unpacktuple(foo(),2) == 2) 15 | 16 | assert(unpacktuple(foo(),nil,1) == 1) 17 | assert(unpacktuple(foo2()).a == 1) 18 | 19 | assert(2 == #{unpacktuple(foo())}) 20 | 21 | assert(1 == #{unpacktuple(foo(),2)}) 22 | 23 | terra usefoo() 24 | var a = foo() 25 | var c,d = unpacktuple(a) 26 | var e = unpacktuple(a,2) 27 | return e 28 | end 29 | 30 | assert(usefoo() == 2) -------------------------------------------------------------------------------- /tests/unsafesym.t: -------------------------------------------------------------------------------- 1 | 2 | local mymacro = macro(function(a) 3 | print(a:gettype()) 4 | return {} 5 | end) 6 | 7 | 8 | 9 | terra foo(a : int) 10 | mymacro(a) 11 | end 12 | 13 | foo:compile() -------------------------------------------------------------------------------- /tests/unstrict.t: -------------------------------------------------------------------------------- 1 | Strict.__newindex,Strict.__index = nil,nil 2 | 3 | print(b) 4 | terra foo() end 5 | print(c) -------------------------------------------------------------------------------- /tests/usercast.t: -------------------------------------------------------------------------------- 1 | 2 | struct A { a : int, b : float } 3 | 4 | function A.metamethods.__cast(from,to,exp) 5 | if from == int and to == A then 6 | return `A {exp, 1.f } 7 | elseif from == float and to == A then 8 | return `A { 1, exp } 9 | end 10 | error("invalid") 11 | end 12 | 13 | 14 | terra moo(a : A) 15 | return a.a + a.b 16 | end 17 | 18 | terra bar() 19 | return moo(1) + moo(1.f) 20 | end 21 | 22 | local test = require("test") 23 | 24 | test.eq(bar(), 4) -------------------------------------------------------------------------------- /tests/ustore.t: -------------------------------------------------------------------------------- 1 | local terra unalignedload(addr : &float) 2 | return terralib.attrload([&vector(float,4)](addr), { align = 4 }) 3 | end 4 | local terra unalignedstore(addr : &float, value : vector(float,4)) 5 | terralib.attrstore([&vector(float,4)](addr),value, { align = 4 }) 6 | end 7 | 8 | terra foo(a : &float) 9 | unalignedstore(a,unalignedload(a+3)) 10 | end 11 | foo:disas() -------------------------------------------------------------------------------- /tests/varargcstring.t: -------------------------------------------------------------------------------- 1 | C = terralib.includec("stdio.h") 2 | C.printf("hello, %d\n",3) -------------------------------------------------------------------------------- /tests/varargluacall.t: -------------------------------------------------------------------------------- 1 | local function createluawrapper(luafn) 2 | local typecache = {} 3 | return macro(function(...) 4 | local args = terralib.newlist {...} 5 | local argtypes = args:map("gettype") 6 | local fntype = argtypes -> {} 7 | if not typecache[fntype] then 8 | print("new",fntype) 9 | typecache[fntype] = terralib.cast(fntype,luafn) 10 | end 11 | local fn = typecache[fntype] 12 | return `fn([args]) 13 | end) 14 | end 15 | 16 | local tprint = createluawrapper(print) 17 | terra test() 18 | tprint(1,2,3.5) 19 | tprint(1) 20 | tprint(2) 21 | end 22 | 23 | test() -------------------------------------------------------------------------------- /tests/varrec.broken: -------------------------------------------------------------------------------- 1 | var a,b = foo() 2 | 3 | terra foo() : (int,int) 4 | return a,b 5 | end 6 | 7 | print(foo()) -------------------------------------------------------------------------------- /tests/vars.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | a = {} 4 | a.b, a.c = global(3),global(double,4) 5 | d = global(5) 6 | --print(a.b) 7 | 8 | --a.b:gettype(nil) 9 | 10 | terra bar() 11 | a.b = a.b + 1 12 | a.c = a.c + 1 13 | d = d + 1 14 | end 15 | terra foo() 16 | return a.b,a.c,d 17 | end 18 | 19 | 20 | local test = require("test") 21 | 22 | bar() 23 | test.meq({4,5,6},foo()) -------------------------------------------------------------------------------- /tests/vars2.t: -------------------------------------------------------------------------------- 1 | 2 | a = global(int) --testing variable with no initializer 3 | 4 | 5 | terra foo() 6 | a = 3 7 | end 8 | 9 | terra bar() 10 | return a 11 | end 12 | 13 | 14 | local test = require("test") 15 | 16 | foo() 17 | test.eq(bar(),3) -------------------------------------------------------------------------------- /tests/vec.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo(a : double) 3 | var v0 : vector(double,3) = a 4 | var v1 : vector(int,3) = 4 5 | var v2 = v0 / v1 6 | var v3 = (v0 <= v1) or (v1 >= v0) 7 | var ptr = [&double](&v2) 8 | return v3[0] or v3[1] or v3[2] 9 | --return ptr[0] + ptr[1] + ptr[2] 10 | end 11 | 12 | 13 | N = 64 14 | terra addsomevecs(a : vector(double,N), b : vector(double,N), c : vector(double,N) ) 15 | return (a + b) / c 16 | end 17 | 18 | 19 | terra foo2(a : double, b : double) 20 | var c = addsomevecs(a,b,b) 21 | return c[0] + c[1] 22 | end 23 | 24 | local test = require("test") 25 | 26 | test.eq(foo(3), true) 27 | test.eq(foo2(3,4), 3.5) -------------------------------------------------------------------------------- /tests/veclit.t: -------------------------------------------------------------------------------- 1 | 2 | terra foo() 3 | var a = 2* vector(1,2,3) 4 | return a[0] + a[1] + a[2] 5 | end 6 | 7 | terra foo2() 8 | var a = vector(1,2.5,3) 9 | return a[1] 10 | end 11 | terra what() 12 | return 3,4.5 13 | end 14 | expwhat = macro(function(a) return {`a._0, `a._1} end) 15 | terra foo3() 16 | var a = vector(1,2.5,expwhat(what())) 17 | return a[3] 18 | end 19 | 20 | terra foo4() 21 | var a = vectorof(int,1,2.5,3) 22 | return a[1] 23 | end 24 | 25 | local test = require("test") 26 | test.eq(foo(),12) 27 | test.eq(foo2(),2.5) 28 | test.eq(foo3(),4.5) 29 | test.eq(foo4(),2) -------------------------------------------------------------------------------- /tests/vecptr.t: -------------------------------------------------------------------------------- 1 | terra foo(a : &float) 2 | var b = [&vector(float,4)](a) 3 | end 4 | foo:compile() -------------------------------------------------------------------------------- /tests/vecsize.t: -------------------------------------------------------------------------------- 1 | 2 | assert(terralib.sizeof(int)*3 <= terralib.sizeof(vector(int,3))) 3 | assert(terralib.sizeof(vector(int,1)) == 4) 4 | assert(terralib.sizeof(vector(int,4)) == 16) 5 | assert(terralib.sizeof(vector(int,5)) == 32) -------------------------------------------------------------------------------- /tests/version.t: -------------------------------------------------------------------------------- 1 | assert(type(terralib.version) == "string") 2 | -------------------------------------------------------------------------------- /tests/vtablerec.t: -------------------------------------------------------------------------------- 1 | 2 | struct V { 3 | entry : {&A} -> {} 4 | } 5 | struct A { 6 | vtable : &V 7 | } 8 | 9 | terra what(a : &A) : {} 10 | end 11 | 12 | what:compile() 13 | 14 | terra bar() 15 | var v : V 16 | v.entry = what 17 | end 18 | 19 | bar:compile() 20 | 21 | --[[ 22 | {&A}->{} 23 | &A 24 | A 25 | &V 26 | V 27 | &{&A}->{} 28 | {&A}->{} 29 | 30 | ]] -------------------------------------------------------------------------------- /tests/weirdheader.t: -------------------------------------------------------------------------------- 1 | C = terralib.includecstring [[ 2 | #define GASNETI_RELEASE_VERSION 1.26.0 3 | ]] -------------------------------------------------------------------------------- /tests/zeroargs.t: -------------------------------------------------------------------------------- 1 | local b = {} 2 | terra foo() 3 | return 1,b 4 | end 5 | foo() -------------------------------------------------------------------------------- /tests/zeroreturn.t: -------------------------------------------------------------------------------- 1 | 2 | 3 | local b = global(0) 4 | 5 | terra foo(a : int) 6 | if a > 3 then 7 | return 8 | end 9 | b = a 10 | end 11 | 12 | terra getb() return b end 13 | 14 | local test = require("test") 15 | foo(4) 16 | test.eq(getb(),0) 17 | foo(2) 18 | test.eq(getb(),2) -------------------------------------------------------------------------------- /tests/zeroreturn2.t: -------------------------------------------------------------------------------- 1 | 2 | a = global(0) 3 | 4 | terra doit() 5 | a = a + 1 6 | end 7 | terra geta() 8 | return a 9 | end 10 | 11 | terra bar() 12 | return doit() 13 | end 14 | 15 | bar() 16 | local test = require("test") 17 | test.eq(geta(),1) --------------------------------------------------------------------------------