├── .dir-locals.el ├── .github ├── FUNDING.yml └── workflows │ └── push.yml ├── .gitignore ├── README.md ├── bench ├── bench.rkt ├── plot.rkt ├── profile.rkt ├── programs │ ├── binary-trees.lua │ ├── fannkuch.lua │ ├── mandelbrot.lua │ ├── nbody.lua │ └── spectral-norm.lua └── results.json ├── lua-lib ├── LICENSE ├── base.rkt ├── cli.rkt ├── env.rkt ├── info.rkt ├── lang │ ├── ast.rkt │ ├── compiler.rkt │ ├── lexer.rkt │ ├── parser.rkt │ ├── read-syntax.rkt │ ├── reader.rkt │ ├── runtime-config.rkt │ └── tool.rkt ├── private │ ├── adjust.rkt │ ├── arithmetic.rkt │ ├── concat.rkt │ ├── env.rkt │ ├── error.rkt │ ├── exn.rkt │ ├── iter.rkt │ ├── json.rkt │ ├── length.rkt │ ├── logic.rkt │ ├── mark.rkt │ ├── nil.rkt │ ├── number.rkt │ ├── realm.rkt │ ├── relation.rkt │ ├── string.rkt │ ├── table.rkt │ └── type.rkt ├── repl.rkt ├── stdlib │ ├── coroutine.lua │ ├── file.lua │ ├── info.rkt │ ├── io.lua │ ├── math.lua │ ├── os.lua │ ├── racket.lua │ ├── string.lua │ └── table.lua └── value.rkt ├── lua-test ├── LICENSE ├── info.rkt └── lua │ ├── compiler.rkt │ ├── examples │ ├── assignment-00.lua │ ├── assignment-00.stdout.rktd │ ├── assignment-00.tokens.rktd │ ├── assignment-01.lua │ ├── assignment-01.stdout.rktd │ ├── assignment-01.tokens.rktd │ ├── assignment-02.lua │ ├── assignment-02.stdout.rktd │ ├── assignment-02.tokens.rktd │ ├── bitops.lua │ ├── bitops.stdout.rktd │ ├── bitops.tokens.rktd │ ├── break.lua │ ├── break.stdout.rktd │ ├── break.tokens.rktd │ ├── concat.lua │ ├── concat.stdout.rktd │ ├── concat.tokens.rktd │ ├── coroutine.lua │ ├── coroutine.stdout.rktd │ ├── coroutine.tokens.rktd │ ├── do.lua │ ├── do.stdout.rktd │ ├── do.tokens.rktd │ ├── dunder-call.lua │ ├── dunder-call.stdout.rktd │ ├── dunder-call.tokens.rktd │ ├── dunder-eq.lua │ ├── dunder-eq.stdout.rktd │ ├── dunder-eq.tokens.rktd │ ├── dunder-index.lua │ ├── dunder-index.stdout.rktd │ ├── dunder-index.tokens.rktd │ ├── dunder-len.lua │ ├── dunder-len.stdout.rktd │ ├── dunder-len.tokens.rktd │ ├── dunder-metatable.lua │ ├── dunder-metatable.stdout.rktd │ ├── dunder-metatable.tokens.rktd │ ├── dunder-newindex.lua │ ├── dunder-newindex.stdout.rktd │ ├── dunder-newindex.tokens.rktd │ ├── env.lua │ ├── env.stdout.rktd │ ├── env.tokens.rktd │ ├── error.lua │ ├── error.stdout.rktd │ ├── error.tokens.rktd │ ├── fib.lua │ ├── fib.stdout.rktd │ ├── fib.tokens.rktd │ ├── file.lua │ ├── file.stdout.rktd │ ├── file.tokens.rktd │ ├── for-in-closing.lua │ ├── for-in-closing.stdout.rktd │ ├── for-in-closing.tokens.rktd │ ├── for-in.lua │ ├── for-in.stdout.rktd │ ├── for-in.tokens.rktd │ ├── for.lua │ ├── for.stdout.rktd │ ├── for.tokens.rktd │ ├── function.lua │ ├── function.stdout.rktd │ ├── function.tokens.rktd │ ├── functioncall.lua │ ├── functioncall.stdout.rktd │ ├── functioncall.tokens.rktd │ ├── goto-01.lua │ ├── goto-01.stdout.rktd │ ├── goto-01.tokens.rktd │ ├── if.lua │ ├── if.stdout.rktd │ ├── if.tokens.rktd │ ├── io-file.lua │ ├── io-file.stdout.rktd │ ├── io-file.tokens.rktd │ ├── io.lua │ ├── io.stdout.rktd │ ├── io.tokens.rktd │ ├── ipairs.lua │ ├── ipairs.stdout.rktd │ ├── ipairs.tokens.rktd │ ├── json.lua │ ├── json.stdout.rktd │ ├── json.tokens.rktd │ ├── kitchen-sink.lua │ ├── kitchen-sink.tokens.rktd │ ├── local-env.lua │ ├── local-env.stdout.rktd │ ├── local-env.tokens.rktd │ ├── local-function-name.lua │ ├── local-function-name.stdout.rktd │ ├── local-function-name.tokens.rktd │ ├── local-function.lua │ ├── local-function.stdout.rktd │ ├── local-function.tokens.rktd │ ├── local.lua │ ├── local.stdout.rktd │ ├── local.tokens.rktd │ ├── longbrakets.lua │ ├── longbrakets.stdout.rktd │ ├── longbrakets.tokens.rktd │ ├── math.lua │ ├── math.stdout.rktd │ ├── math.tokens.rktd │ ├── methods.lua │ ├── methods.stdout.rktd │ ├── methods.tokens.rktd │ ├── next.lua │ ├── next.stdout.rktd │ ├── next.tokens.rktd │ ├── npe.lua │ ├── npe.stdout.rktd │ ├── npe.tokens.rktd │ ├── os.lua │ ├── os.stdout.rktd │ ├── os.tokens.rktd │ ├── pairs-meta.lua │ ├── pairs-meta.stdout.rktd │ ├── pairs-meta.tokens.rktd │ ├── pairs.lua │ ├── pairs.stdout.rktd │ ├── pairs.tokens.rktd │ ├── parens.lua │ ├── parens.stdout.rktd │ ├── parens.tokens.rktd │ ├── repeat.lua │ ├── repeat.stdout.rktd │ ├── repeat.tokens.rktd │ ├── string-format.lua │ ├── string-format.stdout.rktd │ ├── string-format.tokens.rktd │ ├── string.lua │ ├── string.stdout.rktd │ ├── string.tokens.rktd │ ├── subscript.lua │ ├── subscript.stdout.rktd │ ├── subscript.tokens.rktd │ ├── table-borders.lua │ ├── table-borders.stdout.rktd │ ├── table-borders.tokens.rktd │ ├── table-operator.lua │ ├── table-operator.stdout.rktd │ ├── table-operator.tokens.rktd │ ├── table-separator.lua │ ├── table-separator.stdout.rktd │ ├── table-separator.tokens.rktd │ ├── table-sort.lua │ ├── table-sort.stdout.rktd │ ├── table-sort.tokens.rktd │ ├── table-with-nil.lua │ ├── table-with-nil.stdout.rktd │ ├── table-with-nil.tokens.rktd │ ├── table.lua │ ├── table.stdout.rktd │ ├── table.tokens.rktd │ ├── tostring.lua │ ├── tostring.stdout.rktd │ ├── tostring.tokens.rktd │ ├── values-00.lua │ ├── values-00.stdout.rktd │ ├── values-00.tokens.rktd │ ├── values-01.lua │ ├── values-01.stdout.rktd │ ├── values-01.tokens.rktd │ ├── values-02.lua │ ├── values-02.stdout.rktd │ ├── values-02.tokens.rktd │ ├── values-03.lua │ ├── values-03.stdout.rktd │ ├── values-03.tokens.rktd │ ├── while.lua │ ├── while.stdout.rktd │ └── while.tokens.rktd │ ├── info.rkt │ ├── lexer.rkt │ ├── parser.rkt │ └── table.rkt └── lua ├── LICENSE ├── info.rkt └── lua-manual.scrbl /.dir-locals.el: -------------------------------------------------------------------------------- 1 | (("lua-lib/lang/compiler.rkt" . ((nil . ((eval . (cl-dolist (s '("#%cond" "#%define" "#%dynamic-wind" "#%lambda" "#%let/cc" "#%let" "#%letrec" "#%unless" "#%when" "syntax/loc*")) 2 | (put (intern s) 'racket-indent-function #'defun)))))))) 3 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: Bogdanp 2 | -------------------------------------------------------------------------------- /.github/workflows/push.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@master 14 | - uses: Bogdanp/setup-racket@v1.9 15 | with: 16 | architecture: 'x64' 17 | distribution: 'full' 18 | variant: 'CS' 19 | version: '8.6' 20 | - run: raco pkg install --auto --batch lua-lib/ lua-test/ 21 | - run: raco test lua-test/ 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | compiled/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # racket-lua 2 | 3 | A `#lang` implementation of Lua 5.4 for Racket. Documentation is 4 | available [on the package server][doc]. 5 | 6 | ## License 7 | 8 | lua is licensed under the 3-Clause BSD license. 9 | 10 | [doc]: https://docs.racket-lang.org/lua-manual@lua-lang/index.html 11 | -------------------------------------------------------------------------------- /bench/bench.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require json 4 | racket/match 5 | racket/port 6 | racket/runtime-path 7 | racket/sandbox 8 | racket/system) 9 | 10 | (define-runtime-path programs-dir "programs") 11 | 12 | (struct config (name path args hide-stdout?) 13 | #:transparent) 14 | 15 | (define (make-config name args 16 | #:path [path (build-path programs-dir (format "~a.lua" name))] 17 | #:hide-stdout? [hide-stdout? #f]) 18 | (config name path args hide-stdout?)) 19 | 20 | (define programs 21 | (list 22 | (make-config "binary-trees" '("15")) 23 | (make-config "fannkuch" '("10")) 24 | (make-config "spectral-norm" '("1000")) 25 | (make-config "mandelbrot" '("400") #:hide-stdout? #t) 26 | (make-config "nbody" '("500000")))) 27 | 28 | (define-runtime-path results.json 29 | "results.json") 30 | 31 | (define-runtime-path HEAD 32 | "../.git/refs/heads/master") 33 | 34 | (define (get-git-commit) 35 | (call-with-input-file HEAD port->string)) 36 | 37 | (define lua 38 | (or (find-executable-path "lua") 39 | (find-executable-path "lua5.4"))) 40 | 41 | (define (run-lua . args) 42 | (match-define (list stdout _stdin _pid stderr control) 43 | (apply process* lua args)) 44 | (control 'wait) 45 | (define code 46 | (control 'exit-code)) 47 | (unless (zero? code) 48 | (error 'run-lua "lua process failed: ~a" (port->string stderr))) 49 | (port->string stdout)) 50 | 51 | (define (get-lua-version) 52 | (run-lua "-v")) 53 | 54 | (define (eval-lua lua-in args) 55 | (define-values (in out) 56 | (make-pipe)) 57 | (thread 58 | (lambda () 59 | (displayln "#lang lua" out) 60 | (copy-port lua-in out) 61 | (close-output-port out))) 62 | (call-with-trusted-sandbox-configuration 63 | (lambda () 64 | (parameterize ([sandbox-input (current-input-port)] 65 | [sandbox-output (current-output-port)] 66 | [sandbox-error-output (current-error-port)] 67 | [sandbox-init-hook (λ () (current-command-line-arguments (list->vector args)))]) 68 | ((make-module-evaluator in) '#%chunk))))) 69 | 70 | (define (bench proc) 71 | (define-values (_ cpu real gc) 72 | (time-apply proc null)) 73 | (hasheq 'cpu cpu 'real real 'gc gc)) 74 | 75 | (module+ main 76 | (require racket/cmdline) 77 | 78 | (define programs-to-run 79 | (command-line 80 | #:args program-names 81 | (if (null? program-names) 82 | programs 83 | (for/list ([conf (in-list programs)] 84 | #:when (member (config-name conf) program-names)) 85 | conf)))) 86 | 87 | (define data 88 | (with-handlers ([exn:fail? (λ (_) null)]) 89 | (call-with-input-file results.json read-json))) 90 | (define lua-version (get-lua-version)) 91 | 92 | (define results 93 | (for/hash ([conf (in-list programs-to-run)]) 94 | (match-define (config name path args hide-stdout?) conf) 95 | (printf "== Running ~a (lua) ==~n" name) 96 | (define lua-result 97 | (hash-set* 98 | (bench 99 | (lambda () 100 | (define stdout-data 101 | (apply run-lua (path->string path) args)) 102 | (unless hide-stdout? 103 | (display stdout-data)))) 104 | 'version lua-version)) 105 | (printf "== Running ~a (racket) ==~n" name) 106 | (define racket-result 107 | (bench 108 | (lambda () 109 | (call-with-input-file path 110 | (lambda (in) 111 | (parameterize ([current-output-port (if hide-stdout? 112 | (open-output-nowhere) 113 | (current-output-port))]) 114 | (eval-lua in args))))))) 115 | (values 116 | (string->symbol name) 117 | (hasheq 118 | 'lua lua-result 119 | 'racket racket-result)))) 120 | 121 | (define new-data 122 | (cons 123 | (hasheq 124 | 'timestamp (current-seconds) 125 | 'commit (get-git-commit) 126 | 'machine (system-type 'machine) 127 | 'results results) 128 | data)) 129 | (call-with-output-file results.json 130 | #:exists 'replace 131 | (lambda (out) 132 | (write-json new-data out)))) 133 | -------------------------------------------------------------------------------- /bench/plot.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require racket/runtime-path) 4 | 5 | (define-runtime-path results.json 6 | "results.json") 7 | 8 | (module+ main 9 | (require json 10 | plot 11 | racket/cmdline) 12 | 13 | (define commit-re #rx".*") 14 | (define machine-re #rx".*") 15 | (command-line 16 | #:once-each 17 | ["--commit-re" COMMIT-RE 18 | "the regular expression to use on commit messages when selecting results" 19 | (set! commit-re (regexp COMMIT-RE))] 20 | ["--machine-re" MACHINE-RE 21 | "the regular expression to use on machine messages when selecting results" 22 | (set! machine-re (regexp MACHINE-RE))]) 23 | 24 | (define data 25 | (findf 26 | (λ (e) 27 | (and (regexp-match? commit-re (hash-ref e 'commit)) 28 | (regexp-match? machine-re (hash-ref e 'machine)))) 29 | (with-handlers ([exn:fail? (λ (_) 30 | (eprintf "error: failed to read results.json~n") 31 | (exit 1))]) 32 | (call-with-input-file results.json read-json)))) 33 | (unless data 34 | (eprintf "error: no matching results~n") 35 | (exit 1)) 36 | (define results (hash-ref data 'results)) 37 | 38 | (define (make-histogram key x-min) 39 | (discrete-histogram 40 | #:label (string-titlecase (symbol->string key)) 41 | #:x-min x-min #:skip 2.5 42 | #:color (add1 x-min) #:line-color (add1 x-min) 43 | (for*/list ([program (in-list (sort (hash-keys results) symbolstring program)) 47 | (hash-ref (hash-ref program-res key) 'real))))) 48 | 49 | (parameterize ([plot-new-window? #t] 50 | [plot-width 800] 51 | [plot-height 400] 52 | [plot-title (format "Commit: ~a" (hash-ref data 'commit))] 53 | [plot-x-label "Program"] 54 | [plot-y-label "Wall Clock Time (ms)"]) 55 | (plot 56 | (for/list ([(k idx) (in-indexed (in-list '(lua racket)))]) 57 | (make-histogram k idx))))) 58 | -------------------------------------------------------------------------------- /bench/profile.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require profile 4 | racket/cmdline 5 | racket/file 6 | racket/port) 7 | 8 | (define (eval-lua lua-in args) 9 | (define path (make-temporary-file "lua~a.lua")) 10 | (call-with-output-file path 11 | #:exists 'truncate/replace 12 | (lambda (out) 13 | (displayln "#lang lua" out) 14 | (copy-port lua-in out))) 15 | (parameterize ([current-command-line-arguments (list->vector args)]) 16 | (dynamic-require path '#%chunk))) 17 | 18 | (module+ main 19 | (require racket/cmdline) 20 | 21 | (define profile? #t) 22 | (define-values (program args) 23 | (command-line 24 | #:once-each 25 | [("-q") "disable profiling" (set! profile? #f)] 26 | #:args [program . args] 27 | (values program args))) 28 | 29 | (call-with-input-file program 30 | (lambda (in) 31 | (if profile? 32 | (profile-thunk 33 | #:delay 0 34 | #:use-errortrace? #t 35 | (lambda () (eval-lua in args))) 36 | (time (eval-lua in args)))))) 37 | -------------------------------------------------------------------------------- /bench/programs/binary-trees.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Benchmarks Game 2 | -- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ 3 | -- contributed by Mike Pall 4 | -- *reset* 5 | 6 | local function BottomUpTree(depth) 7 | if depth > 0 then 8 | depth = depth - 1 9 | local left, right = BottomUpTree(depth), BottomUpTree(depth) 10 | return { left, right } 11 | else 12 | return { } 13 | end 14 | end 15 | 16 | local function ItemCheck(tree) 17 | if tree[1] then 18 | return 1 + ItemCheck(tree[1]) + ItemCheck(tree[2]) 19 | else 20 | return 1 21 | end 22 | end 23 | 24 | local N = tonumber(arg and arg[1]) or 0 25 | local mindepth = 4 26 | local maxdepth = mindepth + 2 27 | if maxdepth < N then maxdepth = N end 28 | 29 | do 30 | local stretchdepth = maxdepth + 1 31 | local stretchtree = BottomUpTree(stretchdepth) 32 | print("stretch tree of depth", stretchdepth, "check:", ItemCheck(stretchtree)) 33 | end 34 | 35 | local longlivedtree = BottomUpTree(maxdepth) 36 | 37 | for depth=mindepth,maxdepth,2 do 38 | local iterations = 2 ^ (maxdepth - depth + mindepth) 39 | local check = 0 40 | for i=1,iterations do 41 | check = check + ItemCheck(BottomUpTree(depth)) 42 | end 43 | print(iterations, "trees of depth", depth, "check:", check) 44 | end 45 | 46 | print("long lived tree of depth", maxdepth, "check:", ItemCheck(longlivedtree)) 47 | -------------------------------------------------------------------------------- /bench/programs/fannkuch.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Benchmarks Game 2 | -- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ 3 | -- contributed by Mike Pall 4 | 5 | local function fannkuch(n) 6 | local p, q, s, sign, maxflips, sum = {}, {}, {}, 1, 0, 0 7 | for i=1,n do p[i] = i; q[i] = i; s[i] = i end 8 | repeat 9 | -- Copy and flip. 10 | local q1 = p[1] -- Cache 1st element. 11 | if q1 ~= 1 then 12 | for i=2,n do q[i] = p[i] end -- Work on a copy. 13 | local flips = 1 14 | repeat 15 | local qq = q[q1] 16 | if qq == 1 then -- ... until 1st element is 1. 17 | sum = sum + sign*flips 18 | if flips > maxflips then maxflips = flips end -- New maximum? 19 | break 20 | end 21 | q[q1] = q1 22 | if q1 >= 4 then 23 | local i, j = 2, q1 - 1 24 | repeat q[i], q[j] = q[j], q[i]; i = i + 1; j = j - 1; until i >= j 25 | end 26 | q1 = qq; flips = flips + 1 27 | until false 28 | end 29 | -- Permute. 30 | if sign == 1 then 31 | p[2], p[1] = p[1], p[2]; sign = -1 -- Rotate 1<-2. 32 | else 33 | p[2], p[3] = p[3], p[2]; sign = 1 -- Rotate 1<-2 and 1<-2<-3. 34 | for i=3,n do 35 | local sx = s[i] 36 | if sx ~= 1 then s[i] = sx-1; break end 37 | if i == n then return sum, maxflips end -- Out of permutations. 38 | s[i] = i 39 | -- Rotate 1<-...<-i+1. 40 | local t = p[1]; for j=1,i do p[j] = p[j+1] end; p[i+1] = t 41 | end 42 | end 43 | until false 44 | end 45 | 46 | local n = tonumber(arg and arg[1]) or 1 47 | local sum, flips = fannkuch(n) 48 | io.write(sum, "\nPfannkuchen(", n, ") = ", flips, "\n") 49 | -------------------------------------------------------------------------------- /bench/programs/mandelbrot.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Benchmarks Game 2 | -- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ 3 | -- contributed by Mike Pall 4 | -- Updated for Lua 5.3 by Robin 5 | 6 | local write, char, unpack = io.write, string.char, table.unpack 7 | local N = tonumber(arg and arg[1]) or 100 8 | local M, ba, bb, buf = 2/N, 2^(N%8+1)-1, 2^(8-N%8), {} 9 | write("P4\n", N, " ", N, "\n") 10 | for y=0,N-1 do 11 | local Ci, b, p = y*M-1, 1, 0 12 | for x=0,N-1 do 13 | local Cr = x*M-1.5 14 | local Zr, Zi, Zrq, Ziq = Cr, Ci, Cr*Cr, Ci*Ci 15 | b = b + b 16 | for i=1,49 do 17 | Zi = Zr*Zi*2 + Ci 18 | Zr = Zrq-Ziq + Cr 19 | Ziq = Zi*Zi 20 | Zrq = Zr*Zr 21 | if Zrq+Ziq > 4.0 then b = b + 1; break; end 22 | end 23 | if b >= 256 then p = p + 1; buf[p] = 511 - b; b = 1; end 24 | end 25 | if b ~= 1 then p = p + 1; buf[p] = (ba-b)*bb; end 26 | write(char(unpack(buf, 1, p))) 27 | end 28 | -------------------------------------------------------------------------------- /bench/programs/nbody.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Benchmarks Game 2 | -- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ 3 | -- contributed by Mike Pall 4 | 5 | local sqrt = math.sqrt 6 | 7 | local PI = 3.141592653589793 8 | local SOLAR_MASS = 4 * PI * PI 9 | local DAYS_PER_YEAR = 365.24 10 | local bodies = { 11 | { -- Sun 12 | x = 0, 13 | y = 0, 14 | z = 0, 15 | vx = 0, 16 | vy = 0, 17 | vz = 0, 18 | mass = SOLAR_MASS 19 | }, 20 | { -- Jupiter 21 | x = 4.84143144246472090e+00, 22 | y = -1.16032004402742839e+00, 23 | z = -1.03622044471123109e-01, 24 | vx = 1.66007664274403694e-03 * DAYS_PER_YEAR, 25 | vy = 7.69901118419740425e-03 * DAYS_PER_YEAR, 26 | vz = -6.90460016972063023e-05 * DAYS_PER_YEAR, 27 | mass = 9.54791938424326609e-04 * SOLAR_MASS 28 | }, 29 | { -- Saturn 30 | x = 8.34336671824457987e+00, 31 | y = 4.12479856412430479e+00, 32 | z = -4.03523417114321381e-01, 33 | vx = -2.76742510726862411e-03 * DAYS_PER_YEAR, 34 | vy = 4.99852801234917238e-03 * DAYS_PER_YEAR, 35 | vz = 2.30417297573763929e-05 * DAYS_PER_YEAR, 36 | mass = 2.85885980666130812e-04 * SOLAR_MASS 37 | }, 38 | { -- Uranus 39 | x = 1.28943695621391310e+01, 40 | y = -1.51111514016986312e+01, 41 | z = -2.23307578892655734e-01, 42 | vx = 2.96460137564761618e-03 * DAYS_PER_YEAR, 43 | vy = 2.37847173959480950e-03 * DAYS_PER_YEAR, 44 | vz = -2.96589568540237556e-05 * DAYS_PER_YEAR, 45 | mass = 4.36624404335156298e-05 * SOLAR_MASS 46 | }, 47 | { -- Neptune 48 | x = 1.53796971148509165e+01, 49 | y = -2.59193146099879641e+01, 50 | z = 1.79258772950371181e-01, 51 | vx = 2.68067772490389322e-03 * DAYS_PER_YEAR, 52 | vy = 1.62824170038242295e-03 * DAYS_PER_YEAR, 53 | vz = -9.51592254519715870e-05 * DAYS_PER_YEAR, 54 | mass = 5.15138902046611451e-05 * SOLAR_MASS 55 | } 56 | } 57 | 58 | local function advance(bodies, nbody, dt) 59 | for i=1,nbody do 60 | local bi = bodies[i] 61 | local bix, biy, biz, bimass = bi.x, bi.y, bi.z, bi.mass 62 | local bivx, bivy, bivz = bi.vx, bi.vy, bi.vz 63 | for j=i+1,nbody do 64 | local bj = bodies[j] 65 | local dx, dy, dz = bix-bj.x, biy-bj.y, biz-bj.z 66 | local distance = sqrt(dx*dx + dy*dy + dz*dz) 67 | local mag = dt / (distance * distance * distance) 68 | local bim, bjm = bimass*mag, bj.mass*mag 69 | bivx = bivx - (dx * bjm) 70 | bivy = bivy - (dy * bjm) 71 | bivz = bivz - (dz * bjm) 72 | bj.vx = bj.vx + (dx * bim) 73 | bj.vy = bj.vy + (dy * bim) 74 | bj.vz = bj.vz + (dz * bim) 75 | end 76 | bi.vx = bivx 77 | bi.vy = bivy 78 | bi.vz = bivz 79 | end 80 | for i=1,nbody do 81 | local bi = bodies[i] 82 | bi.x = bi.x + (dt * bi.vx) 83 | bi.y = bi.y + (dt * bi.vy) 84 | bi.z = bi.z + (dt * bi.vz) 85 | end 86 | end 87 | 88 | local function energy(bodies, nbody) 89 | local e = 0 90 | for i=1,nbody do 91 | local bi = bodies[i] 92 | local vx, vy, vz, bim = bi.vx, bi.vy, bi.vz, bi.mass 93 | e = e + (0.5 * bim * (vx*vx + vy*vy + vz*vz)) 94 | for j=i+1,nbody do 95 | local bj = bodies[j] 96 | local dx, dy, dz = bi.x-bj.x, bi.y-bj.y, bi.z-bj.z 97 | local distance = sqrt(dx*dx + dy*dy + dz*dz) 98 | e = e - ((bim * bj.mass) / distance) 99 | end 100 | end 101 | return e 102 | end 103 | 104 | local function offsetMomentum(b, nbody) 105 | local px, py, pz = 0, 0, 0 106 | for i=1,nbody do 107 | local bi = b[i] 108 | local bim = bi.mass 109 | px = px + (bi.vx * bim) 110 | py = py + (bi.vy * bim) 111 | pz = pz + (bi.vz * bim) 112 | end 113 | b[1].vx = -px / SOLAR_MASS 114 | b[1].vy = -py / SOLAR_MASS 115 | b[1].vz = -pz / SOLAR_MASS 116 | end 117 | 118 | local N = tonumber(arg and arg[1]) or 1000 119 | local nbody = #bodies 120 | 121 | offsetMomentum(bodies, nbody) 122 | print(energy(bodies, nbody)) 123 | for i=1,N do advance(bodies, nbody, 0.01) end 124 | print(energy(bodies, nbody)) 125 | -------------------------------------------------------------------------------- /bench/programs/spectral-norm.lua: -------------------------------------------------------------------------------- 1 | -- The Computer Language Benchmarks Game 2 | -- https://salsa.debian.org/benchmarksgame-team/benchmarksgame/ 3 | -- contributed by Mike Pall 4 | 5 | local function A(i, j) 6 | local ij = i+j-1 7 | return 1.0 / (ij * (ij-1) * 0.5 + i) 8 | end 9 | 10 | local function Av(x, y, N) 11 | for i=1,N do 12 | local a = 0 13 | for j=1,N do a = a + x[j] * A(i, j) end 14 | y[i] = a 15 | end 16 | end 17 | 18 | local function Atv(x, y, N) 19 | for i=1,N do 20 | local a = 0 21 | for j=1,N do a = a + x[j] * A(j, i) end 22 | y[i] = a 23 | end 24 | end 25 | 26 | local function AtAv(x, y, t, N) 27 | Av(x, t, N) 28 | Atv(t, y, N) 29 | end 30 | 31 | local N = tonumber(arg and arg[1]) or 100 32 | local u, v, t = {}, {}, {} 33 | for i=1,N do u[i] = 1 end 34 | 35 | for i=1,10 do AtAv(u, v, t, N) AtAv(v, u, t, N) end 36 | 37 | local vBv, vv = 0, 0 38 | for i=1,N do 39 | local ui, vi = u[i], v[i] 40 | vBv = vBv + ui*vi 41 | vv = vv + vi*vi 42 | end 43 | 44 | print(math.sqrt(vBv / vv)) 45 | -------------------------------------------------------------------------------- /lua-lib/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Bogdan Popa 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | 3. Neither the name of the copyright holder nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /lua-lib/base.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | racket/syntax 5 | syntax/parse/pre) 6 | "private/adjust.rkt" 7 | "private/arithmetic.rkt" 8 | "private/concat.rkt" 9 | "private/env.rkt" 10 | "private/error.rkt" 11 | "private/length.rkt" 12 | "private/logic.rkt" 13 | "private/mark.rkt" 14 | "private/nil.rkt" 15 | "private/relation.rkt" 16 | "private/table.rkt") 17 | 18 | (provide 19 | #%app 20 | #%datum 21 | #%declare 22 | #%top-interaction 23 | (rename-out 24 | [apply #%apply] 25 | [begin #%begin] 26 | [call/cc #%call/cc] 27 | [cons #%cons] 28 | [define #%define] 29 | [dynamic-wind #%dynamic-wind] 30 | [else #%else] 31 | [lambda #%lambda] 32 | [let #%let] 33 | [letrec #%letrec] 34 | [let/cc #%let/cc] 35 | [provide #%provide] 36 | [quote #%quote] 37 | [time #%time] 38 | [truthy? #%truthy?] 39 | [values #%values] 40 | [void #%void] 41 | [with-continuation-mark #%with-mark]) 42 | 43 | nil 44 | (rename-out 45 | [current-global-environment #%global] 46 | [function-mark #%function-mark] 47 | [load-standard-library! #%load-stdlib!] 48 | [lua:% %] 49 | [lua:& &] 50 | [lua:* *] 51 | [lua:+ +] 52 | [lua:- -] 53 | [lua:.. ..] 54 | [lua:/ /] 55 | [lua:// //] 56 | [lua:< < ] 57 | [lua:<< <<] 58 | [lua:<= <=] 59 | [lua:== ==] 60 | [lua:> > ] 61 | [lua:>= >=] 62 | [lua:>> >>] 63 | [lua:^ ^] 64 | [lua:adjust #%adjust] 65 | [lua:adjust-va #%adjust-va] 66 | [lua:and and] 67 | [lua:bnegate #%bnegate] 68 | [lua:bor \|] 69 | [lua:cond #%cond] 70 | [lua:error #%error] 71 | [lua:length #%length] 72 | [lua:module-begin #%module-begin] 73 | [lua:negate #%negate] 74 | [lua:not not] 75 | [lua:or or] 76 | [lua:procedure-rename #%procedure-rename] 77 | [lua:set! #%set!] 78 | [lua:subscript #%subscript] 79 | [lua:top #%top] 80 | [lua:unless #%unless] 81 | [lua:va-args #%va-args] 82 | [lua:when #%when] 83 | [lua:~ ~] 84 | [lua:~= ~=] 85 | [make-table #%table])) 86 | 87 | (begin-for-syntax 88 | (define (id-stx->bytes-stx stx) 89 | (datum->syntax stx (string->bytes/utf-8 (symbol->string (syntax->datum stx)))))) 90 | 91 | (define-syntax (lua:top stx) 92 | (syntax-parse stx 93 | [(_ . id:id) 94 | #:with name (id-stx->bytes-stx #'id) 95 | #:with env (format-id #'id "_ENV") 96 | #'(lua:subscript env name)])) 97 | 98 | (define-syntax (lua:set! stx) 99 | (syntax-parse stx 100 | #:literals (lua:subscript) 101 | [(_ (lua:subscript t:expr k:expr) v:expr) 102 | #'(table-set! t k v)] 103 | [(_ id:id e:expr) 104 | #:when (identifier-binding #'id) 105 | #'(set! id e)] 106 | [(_ id:id e:expr) 107 | #:with name (id-stx->bytes-stx #'id) 108 | #:with env (format-id #'id "_ENV") 109 | #'(table-set! env name e)])) 110 | 111 | (define-syntax (lua:cond stx) 112 | (syntax-parse stx 113 | #:literals (else) 114 | [(_ [test-expr body-expr ...+] ... 115 | [else else-body-expr ...+]) 116 | #'(cond 117 | [(truthy? test-expr) body-expr ...] ... 118 | [else else-body-expr ...])])) 119 | 120 | (define-syntax (lua:unless stx) 121 | (syntax-parse stx 122 | [(_ cond-expr body ...+) 123 | #'(unless (truthy? cond-expr) 124 | body ...)])) 125 | 126 | (define-syntax (lua:when stx) 127 | (syntax-parse stx 128 | [(_ cond-expr body ...+) 129 | #'(when (truthy? cond-expr) 130 | body ...)])) 131 | 132 | (define-syntax (lua:subscript stx) 133 | (syntax-parse stx 134 | [(_ t:expr k:expr) 135 | #'(table-ref t k)])) 136 | 137 | (define-syntax (lua:adjust stx) 138 | (syntax-parse stx 139 | [(_ e) #'(lua:adjust* (λ () e))])) 140 | 141 | (define-syntax (lua:adjust-va stx) 142 | (syntax-parse stx 143 | [(_ e) #'(lua:adjust-va* (λ () e))])) 144 | 145 | (define-syntax (lua:va-args stx) 146 | (syntax-parse stx 147 | [(_) 148 | #:with #%rest (format-id stx "#%rest") 149 | #'(apply values #%rest)])) 150 | 151 | (define-syntax (lua:module-begin stx) 152 | (syntax-parse stx 153 | [(_ form ...) 154 | #'(#%module-begin 155 | (#%declare #:realm lua) 156 | (module configure-runtime racket/base 157 | (require lua/lang/runtime-config) 158 | (configure-runtime)) 159 | form ...)])) 160 | 161 | (define-syntax (lua:procedure-rename stx) 162 | (syntax-parse stx 163 | [(_ proc:expr name:id) 164 | #'(procedure-rename proc 'name 'lua)])) 165 | -------------------------------------------------------------------------------- /lua-lib/cli.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require racket/port) 4 | 5 | (define (make-#lang-lua-port src-in) 6 | (define-values (in out) 7 | (make-pipe #f (object-name src-in))) 8 | (begin0 in 9 | (thread 10 | (lambda () 11 | (displayln "#lang lua" out) 12 | (copy-port src-in out) 13 | (close-output-port out))))) 14 | 15 | (module+ main 16 | (require racket/cmdline 17 | racket/sandbox) 18 | 19 | (define ins 20 | (map make-#lang-lua-port 21 | (command-line 22 | #:args paths 23 | (if (null? paths) 24 | (list (current-input-port)) 25 | (map open-input-file paths))))) 26 | 27 | (call-with-trusted-sandbox-configuration 28 | (lambda () 29 | (parameterize ([sandbox-input (current-input-port)] 30 | [sandbox-output (current-output-port)] 31 | [sandbox-error-output (current-error-port)]) 32 | (for ([in (in-list ins)]) 33 | ((make-module-evaluator in) '#%chunk)))))) 34 | -------------------------------------------------------------------------------- /lua-lib/env.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require racket/contract/base 4 | "private/env.rkt" 5 | "private/table.rkt") 6 | 7 | (provide 8 | (contract-out 9 | [make-initial-environment (-> table?)] 10 | [current-racket-imports-enabled? (parameter/c boolean?)] 11 | [current-global-environment (parameter/c table?)] 12 | [current-standard-library-modules (parameter/c 13 | (listof 14 | (cons/c bytes? (or/c module-path? 15 | resolved-module-path? 16 | module-path-index? 17 | (-> table? bytes? void?)))))])) 18 | -------------------------------------------------------------------------------- /lua-lib/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define license 'BSD-3-Clause) 4 | (define collection "lua") 5 | (define version "0.4.2") 6 | (define deps '("base" 7 | "iso-printf-lib" 8 | "sandbox-lib")) 9 | (define raco-commands '(("lua" (submod lua/cli main) "run lua scripts" #f))) 10 | -------------------------------------------------------------------------------- /lua-lib/lang/ast.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | racket/syntax 5 | syntax/parse/pre)) 6 | 7 | (provide 8 | (struct-out Node)) 9 | 10 | (struct Node (loc) #:transparent) 11 | 12 | (define-syntax (define-node-types stx) 13 | (syntax-parse stx 14 | [(_ [Id:id (fld:id ...)] ...) 15 | #:with (Id? ...) 16 | (for/list ([stx (in-list (syntax-e #'(Id ...)))]) 17 | (format-id stx "~a?" stx)) 18 | #:with ((Id-fld ...) ...) 19 | (for/list ([id-stx (in-list (syntax-e #'(Id ...)))] 20 | [flds-stx (in-list (syntax-e #'((fld ...) ...)))]) 21 | (for/list ([fld-stx (in-list (syntax-e flds-stx))]) 22 | (format-id fld-stx "~a-~a" id-stx fld-stx))) 23 | #'(begin 24 | (struct Id Node (fld ...) #:transparent) ... 25 | (provide ->sexp (struct-out Id) ...) 26 | (define (->sexp n) 27 | (cond 28 | [(Id? n) `(Id ,(->sexp (Id-fld n)) ...)] ... 29 | [(list? n) (map ->sexp n)] 30 | [else n])))])) 31 | 32 | (define-node-types 33 | [Assignment (vars exprs)] 34 | [Attribute (e name)] 35 | [Binop (name lhs-expr rhs-expr)] 36 | [Block (stmts)] 37 | [Break ()] 38 | [Call (expr args)] 39 | [CallMethod (expr name args)] 40 | [Do (block)] 41 | [Field (expr)] 42 | [FieldExpr (expr value)] 43 | [FieldLit (name value)] 44 | [For (name initial-expr limit-expr step-expr block)] 45 | [ForIn (names exprs block)] 46 | [Func (params block)] 47 | [FuncDef (name params block)] 48 | [Goto (label)] 49 | [If (cond-expr then-block else-block)] 50 | [Label (name)] 51 | [LocalAssignment (name expr)] 52 | [LocalFunction (name params block)] 53 | [MethodDef (name attr params block)] 54 | [Name (symbol)] 55 | [Repeat (cond-expr block)] 56 | [Return (exprs)] 57 | [Subscript (e sub-e)] 58 | [Table (fields)] 59 | [Unop (name expr)] 60 | [While (cond-expr block)] 61 | 62 | ;; L1 63 | [Let (vars exprs stmts)] 64 | [LetFunction (name params block stmts)] 65 | [Protect (value-stmts post-stmts)]) 66 | -------------------------------------------------------------------------------- /lua-lib/lang/read-syntax.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require syntax/readerr 4 | syntax/strip-context 5 | "compiler.rkt" 6 | "lexer.rkt" 7 | "parser.rkt") 8 | 9 | (provide 10 | read-lua-syntax) 11 | 12 | (define (read-lua-syntax src in) 13 | (define stmts 14 | (strip-context 15 | (compile-chunk 16 | (parameterize ([current-source-name src]) 17 | (with-handlers ([exn:fail:lexer? reraise-lexer-error]) 18 | (parse-chunk (make-lexer in))))))) 19 | (begin0 stmts 20 | (debug src stmts))) 21 | 22 | (define (debug src stmts) 23 | (when (getenv "RACKET_LUA_DEBUG") 24 | (printf ";; <<~a>>~n" src) 25 | ((dynamic-require 'racket/pretty 'pretty-write) 26 | (syntax->datum stmts)))) 27 | 28 | (define (reraise-lexer-error e) 29 | (raise-read-error 30 | (exn-message e) 31 | (current-source-name) 32 | (exn:fail:lexer-line e) 33 | (exn:fail:lexer-col e) 34 | (exn:fail:lexer-pos e) 35 | #f)) 36 | -------------------------------------------------------------------------------- /lua-lib/lang/reader.rkt: -------------------------------------------------------------------------------- 1 | (module reader syntax/module-reader 2 | #:language 'lua/base 3 | #:read custom-read 4 | #:read-syntax custom-read-syntax 5 | #:whole-body-readers? #t 6 | #:info get-info 7 | 8 | (require "read-syntax.rkt") 9 | 10 | (define (custom-read _in) 11 | (error 'read "not supported")) 12 | 13 | (define (custom-read-syntax src in) 14 | (read-lua-syntax src in)) 15 | 16 | (define (get-info key defval default) 17 | (case key 18 | [(color-lexer) (dynamic-require 'lua/lang/tool 'get-color-token)] 19 | [(drracket:default-extension) "lua"] 20 | [(drracket:default-filters) '(["Lua Script" "*.lua"])] 21 | [(drracket:indentation) (dynamic-require 'lua/lang/tool 'get-indentation)] 22 | [else (default key defval)]))) 23 | -------------------------------------------------------------------------------- /lua-lib/lang/runtime-config.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require racket/match 4 | "../private/exn.rkt" 5 | "../private/mark.rkt" 6 | "../private/realm.rkt") 7 | 8 | (provide 9 | configure-runtime) 10 | 11 | (define (configure-runtime) 12 | (error-display-handler 13 | (let ([base-handler (error-display-handler)]) 14 | (lambda (message e) 15 | (if (exn:fail:lua? e) 16 | (display message (current-error-port)) 17 | (base-handler message e))))) 18 | 19 | (current-error-message-adjuster 20 | (lambda (protocol) 21 | (case protocol 22 | [(message) 23 | (lambda (name name-realm message message-realm) 24 | (match* (name name-realm) 25 | [(_ (== lua-realm)) 26 | (define stack-str 27 | (~call-stack)) 28 | (define adjusted-message 29 | (if stack-str 30 | (format "~a~n call stack:~a" message stack-str) 31 | message)) 32 | (values name name-realm adjusted-message message-realm)] 33 | [(_ _) 34 | (values name name-realm message message-realm)]))] 35 | [else 36 | #f])))) 37 | -------------------------------------------------------------------------------- /lua-lib/lang/tool.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require racket/class 4 | racket/string 5 | "lexer.rkt") 6 | 7 | (provide 8 | get-color-token 9 | get-indentation) 10 | 11 | (define (get-color-token in) 12 | (with-handlers ([exn:fail:lexer? 13 | (lambda (e) 14 | (values "" 'error #f (exn:fail:lexer-pos e) (add1 (exn:fail:lexer-pos e))))]) 15 | (define l (make-lexer in #:skip-comments? #f #:partial-strings? #t)) 16 | (define t (lexer-take l)) 17 | (define s (token-str t)) 18 | (values 19 | s 20 | (case (token-type t) 21 | [(keyword) 'keyword] 22 | [(lparen rparen lsqbrace rsqbrace lcubrace rcubrace comma commacomma dot dotdot dotdotdot) 'parenthesis] 23 | [(name) 'symbol] 24 | [(number) 'constant] 25 | [(op) 'operator] 26 | [(whitespace) 'white-space] 27 | [else (token-type t)]) 28 | (case (token-type t) 29 | [(lparen rparen lsqbrace rsqbrace lcubrace rcubrace) (string->symbol s)] 30 | [else #f]) 31 | (and (not (eof-object? s)) (token-pos t)) 32 | (and (not (eof-object? s)) (+ (token-pos t) (string-length s)))))) 33 | 34 | ;; NOTE: These reqexp-quotes should really be pregexp-quotes, but 35 | ;; pregexp-quote was only added in ~8.12. The quoted expressions in 36 | ;; this case don't have any special pregexp characters, so this is 37 | ;; fine. Just something to keep in mind if this needs to expand. 38 | (define indent-phrase-re 39 | (let ([phrases '("function" "local function" "if" "elseif" "else" "for" "while" "do" "repeat")]) 40 | (pregexp (string-append "[[:space:]]*" "(" (string-join (map regexp-quote phrases) "|") ")")))) 41 | 42 | (define indent-sym-re 43 | (regexp (string-append "[" (regexp-quote (string #\( #\[ #\{)) "]$"))) 44 | 45 | (define dedent-re 46 | #px"^[[:space:]]+(elseif|else|end|until|[)}\\]])[[:space:]]*") 47 | 48 | (define tab-size 4) 49 | 50 | (define (get-indent-size line) 51 | (string-length (car (regexp-match #px"[[:space:]]*" line)))) 52 | 53 | (define (get-indentation t pos) 54 | (define prev-line 55 | (let-values ([(s _e) (get-line-range t pos)]) 56 | (and (positive? s) 57 | (get-line t (sub1 s))))) 58 | (define this-line 59 | (get-line t pos)) 60 | (cond 61 | [(not prev-line) #f] 62 | [(regexp-match? dedent-re this-line) 63 | (max 0 (- (get-indent-size prev-line) tab-size))] 64 | [else 65 | (define prev-indent (get-indent-size prev-line)) 66 | (if (or (regexp-match? indent-phrase-re prev-line) 67 | (regexp-match? indent-sym-re prev-line)) 68 | (+ prev-indent tab-size) 69 | prev-indent)])) 70 | 71 | (define (get-line-range t pos) 72 | (define para 73 | (send t position-paragraph pos #t)) 74 | (values 75 | (send t paragraph-start-position para) 76 | (send t paragraph-end-position para))) 77 | 78 | (define (get-line t pos) 79 | (define-values (s e) 80 | (get-line-range t pos)) 81 | (send t get-text s e)) 82 | -------------------------------------------------------------------------------- /lua-lib/private/adjust.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "nil.rkt") 4 | 5 | (provide 6 | lua:adjust* 7 | lua:adjust-va*) 8 | 9 | (define (lua:adjust* proc) 10 | (call-with-values proc (λ vals (if (null? vals) nil (car vals))))) 11 | 12 | (define (lua:adjust-va* proc) 13 | (call-with-values proc list)) 14 | -------------------------------------------------------------------------------- /lua-lib/private/concat.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "adjust.rkt" 4 | "error.rkt" 5 | "nil.rkt" 6 | "string.rkt" 7 | "table.rkt") 8 | 9 | ;; operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | ;; https://www.lua.org/manual/5.4/manual.html#3.4.6 11 | 12 | (provide 13 | lua:..) 14 | 15 | (define (lua:.. a b) 16 | (cond 17 | [(and (bytes? a) (bytes? b)) 18 | (bytes-append a b)] 19 | [(and (bytes? a) (number? b)) 20 | (bytes-append a (lua:tostring b))] 21 | [(and (number? a) (bytes? b)) 22 | (bytes-append (lua:tostring a) b)] 23 | [(and (number? a) (number? b)) 24 | (bytes-append (lua:tostring a) (lua:tostring b))] 25 | [else 26 | (define lhs-dunder-proc (and (table? a) (table-meta-ref a #"__concat"))) 27 | (define rhs-dunder-proc (and (table? b) (table-meta-ref b #"__concat"))) 28 | (cond 29 | [(procedure?* lhs-dunder-proc) 30 | (lua:adjust* (λ () (lhs-dunder-proc a b)))] 31 | [(procedure?* rhs-dunder-proc) 32 | (lua:adjust* (λ () (rhs-dunder-proc b a)))] 33 | [else 34 | (lua:error (format "..: cannot concatenate ~a and ~a" 35 | (lua:tostring a) 36 | (lua:tostring b)))])])) 37 | -------------------------------------------------------------------------------- /lua-lib/private/error.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "exn.rkt" 4 | "mark.rkt" 5 | "nil.rkt" 6 | "string.rkt") 7 | 8 | (provide 9 | lua:pcall 10 | lua:error 11 | lua:assert) 12 | 13 | (define (lua:pcall proc . args) 14 | (with-handlers ([exn:fail:lua? (λ (e) (values #f (exn:fail:lua-value e)))] 15 | [exn:fail? (λ (e) (values #f (exn-message e)))]) 16 | (call-with-values 17 | (lambda () 18 | (apply proc args)) 19 | (lambda results 20 | (apply values #t results))))) 21 | 22 | (define (lua:error v [level 1] . _) 23 | (define message (bytes->string/utf-8 (lua:tostring v))) 24 | (raise-lua-error 'error message v level)) 25 | 26 | (define (lua:assert v [message #"assertion failed!"] . _) 27 | (when (falsy? v) 28 | (lua:error message))) 29 | -------------------------------------------------------------------------------- /lua-lib/private/exn.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "realm.rkt") 4 | 5 | (provide 6 | (struct-out exn:fail:lua) 7 | raise-lua-error) 8 | 9 | (struct exn:fail:lua exn:fail (value level)) 10 | 11 | (define missing 12 | (string->uninterned-symbol "missing")) 13 | 14 | (define (raise-lua-error who message [value missing] [level 1]) 15 | (define adjusted-message (error-message->adjusted-string who lua-realm message lua-realm)) 16 | (define adjusted-value (if (eq? value missing) (string->bytes/utf-8 adjusted-message) value)) 17 | (raise (exn:fail:lua adjusted-message (current-continuation-marks) adjusted-value level))) 18 | -------------------------------------------------------------------------------- /lua-lib/private/iter.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "error.rkt" 4 | "nil.rkt" 5 | "string.rkt" 6 | "table.rkt") 7 | 8 | (provide 9 | lua:next 10 | lua:pairs 11 | lua:ipairs) 12 | 13 | (define (lua:next t [given-idx nil] . _) 14 | (unless (table? t) 15 | (lua:error (format "next: expected a table, but received ~a" (lua:tostring t)))) 16 | (define keys (list->vector (table-keys t))) 17 | (define len (vector-length keys)) 18 | (define key 19 | (cond 20 | [(nil? given-idx) 21 | (cond 22 | [(zero? len) nil] 23 | [else (vector-ref keys 0)])] 24 | [else 25 | (define idx 26 | (for/first ([idx (in-naturals)] 27 | [k (in-vector keys)] 28 | #:when (equal? k given-idx)) 29 | (add1 idx))) 30 | (unless idx 31 | (lua:error (format "next: invalid key ~a" (lua:tostring given-idx)))) 32 | (cond 33 | [(< idx len) (vector-ref keys idx)] 34 | [else nil])])) 35 | (if (nil? key) 36 | (values nil nil) 37 | (values key (table-ref t key)))) 38 | 39 | (define (lua:pairs t . _) 40 | (define __pairs 41 | (table-meta-ref t #"__pairs")) 42 | (if (nil? __pairs) 43 | (values lua:next t nil) 44 | (__pairs t))) 45 | 46 | (define (lua:ipairs t . _) 47 | (define (next s idx) 48 | (define v (table-ref s (add1 idx))) 49 | (if (nil? v) 50 | (values nil nil) 51 | (values (add1 idx) v))) 52 | (values next t 0)) 53 | -------------------------------------------------------------------------------- /lua-lib/private/json.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require json 4 | "error.rkt" 5 | "nil.rkt" 6 | "string.rkt" 7 | "table.rkt") 8 | 9 | (provide 10 | make-json-module) 11 | 12 | (define (make-json-module) 13 | (make-table 14 | `(#"decode" . ,decode) 15 | `(#"encode" . ,encode))) 16 | 17 | (define (decode s . _) 18 | (jsexpr->lua-value (bytes->jsexpr s))) 19 | 20 | (define (encode v . _) 21 | (jsexpr->bytes (lua-value->jsexpr v))) 22 | 23 | (define (lua-value->jsexpr v) 24 | (define js-null (json-null)) 25 | (let loop ([v v]) 26 | (cond 27 | [(nil? v) js-null] 28 | [(boolean? v) v] 29 | [(number? v) v] 30 | [(bytes? v) 31 | (bytes->string/utf-8 v)] 32 | [(array? v) 33 | (map loop (table-values v))] 34 | [(table? v) 35 | (for/hasheq ([(k v) (in-hash (table-ht v))]) 36 | (unless (bytes? k) 37 | (lua:error (format "json.encode: object keys must be strings, received ~a" (lua:tostring k)))) 38 | (define k-sym (string->symbol (bytes->string/utf-8 k))) 39 | (values k-sym (loop v)))] 40 | [else 41 | (lua:error (format "json.encode: unexpected value ~a" (lua:tostring v)))]))) 42 | 43 | (define (jsexpr->lua-value v) 44 | (define js-null (json-null)) 45 | (let loop ([v v]) 46 | (cond 47 | [(eq? v js-null) nil] 48 | [(boolean? v) v] 49 | [(number? v) v] 50 | [(string? v) 51 | (string->bytes/utf-8 v)] 52 | [(list? v) 53 | (apply make-table (map loop v))] 54 | [(hash? v) 55 | (apply make-table (for/list ([(k v) (in-hash v)]) 56 | (define k-str (string->bytes/utf-8 (symbol->string k))) 57 | (cons k-str (loop v))))] 58 | [else 59 | (raise-argument-error 'jsexpr->lua-value "jsexpr?" v)]))) 60 | 61 | ;; TODO: Sparse check. 62 | (define (array? v) 63 | (and (table? v) 64 | (not (nil? (lua:rawget v 1))))) 65 | -------------------------------------------------------------------------------- /lua-lib/private/length.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "adjust.rkt" 4 | "error.rkt" 5 | "nil.rkt" 6 | "string.rkt" 7 | "table.rkt") 8 | 9 | ;; operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10 | ;; https://www.lua.org/manual/5.4/manual.html#3.4.7 11 | 12 | (provide 13 | lua:length) 14 | 15 | (define (lua:length v) 16 | (cond 17 | [(bytes? v) 18 | (bytes-length v)] 19 | [(table? v) 20 | (define __len 21 | (table-meta-ref v #"__len")) 22 | (if (procedure?* __len) 23 | (lua:adjust* (λ () (__len v))) 24 | (table-length v))] 25 | [else 26 | (lua:error (format "#: expected a string or a table, received ~a" (lua:tostring v)))])) 27 | 28 | 29 | ;; procedures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 30 | 31 | (provide 32 | lua:rawlen) 33 | 34 | (define (lua:rawlen v . _) 35 | (cond 36 | [(bytes? v) 37 | (bytes-length v)] 38 | [(table? v) 39 | (table-length v)] 40 | [else 41 | (lua:error (format "rawlen: expected a string or a table, received ~a" (lua:tostring v)))])) 42 | -------------------------------------------------------------------------------- /lua-lib/private/logic.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "nil.rkt") 4 | 5 | (provide 6 | lua:not 7 | lua:and 8 | lua:or) 9 | 10 | ;; operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 11 | ;; https://www.lua.org/manual/5.4/manual.html#3.4.5 12 | 13 | (define (lua:not v) 14 | (if (nil? v) #t (not v))) 15 | 16 | (define-syntax-rule (lua:and a b) 17 | (if (falsy? a) nil b)) 18 | 19 | (define-syntax-rule (lua:or a b) 20 | (let ([t a]) 21 | (if (falsy? t) b t))) 22 | -------------------------------------------------------------------------------- /lua-lib/private/mark.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require racket/port) 4 | 5 | (provide 6 | ~call-stack 7 | function-mark) 8 | 9 | (define (~call-stack [indent 3]) 10 | (define indent-str 11 | (make-string indent #\space)) 12 | (define stack 13 | (continuation-mark-set->list 14 | (current-continuation-marks) 15 | function-mark)) 16 | (and (not (null? stack)) 17 | (call-with-output-string 18 | (lambda (out) 19 | (for/fold ([prev #f] [n 0]) 20 | ([pair (in-list stack)]) 21 | (cond 22 | [(equal? pair prev) 23 | (values pair (add1 n))] 24 | [else 25 | (when (> n 1) 26 | (fprintf out "~n~a [repeated ~a times]" indent-str n)) 27 | (fprintf out "~n~a~a: ~a" 28 | indent-str 29 | (srcloc->string* (car pair)) 30 | (cdr pair)) 31 | (values pair 1)])))))) 32 | 33 | (define function-mark 34 | (make-continuation-mark-key)) 35 | 36 | 37 | ;; help ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 38 | 39 | (define (srcloc->string* loc) 40 | (srcloc->string 41 | (struct-copy 42 | srcloc loc 43 | [source (and 44 | (srcloc-source loc) 45 | (~truncated-source (srcloc-source loc)))]))) 46 | 47 | (define (~truncated-source source) 48 | (define source-str 49 | (if (path? source) 50 | (path->string source) 51 | (format "~a" source))) 52 | (if (> (string-length source-str) 20) 53 | (format "...~a" (substring source-str (- (string-length source-str) 17))) 54 | source-str)) 55 | -------------------------------------------------------------------------------- /lua-lib/private/nil.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | syntax/parse/pre) 5 | racket/lazy-require 6 | racket/port 7 | "exn.rkt") 8 | 9 | (lazy-require 10 | ["string.rkt" (lua:tostring)]) 11 | 12 | (provide 13 | nil? 14 | nil 15 | nil~> 16 | falsy? 17 | truthy? 18 | procedure?*) 19 | 20 | (define nil 21 | (let () 22 | (struct nil () 23 | #:methods gen:custom-write 24 | [(define (write-proc _ out _mode) 25 | (display "nil" out))] 26 | #:property prop:procedure 27 | (lambda (_ . args) 28 | (cond 29 | [(null? args) 30 | (raise-lua-error #f "attempt to call a nil value")] 31 | [else 32 | (define args-str 33 | (call-with-output-string 34 | (lambda (out) 35 | (for ([arg (in-list args)]) 36 | (define arg-str 37 | (bytes->string/utf-8 (lua:tostring arg) #\uFFFD)) 38 | (fprintf out "~n ~a" arg-str))))) 39 | (raise-lua-error #f (format "attempt to call a nil value~n call args: ~a" args-str))]))) 40 | (nil))) 41 | (define (nil? v) 42 | (eq? v nil)) 43 | 44 | (define-syntax (nil~> stx) 45 | (syntax-parse stx 46 | [(_ a) #'a] 47 | [(_ a (rator rand ...)) #'(if (nil? a) nil (rator a rand ...))] 48 | [(_ a b c ...) #'(nil~> (nil~> a b) c ...)])) 49 | 50 | (define (falsy? v) 51 | (or (nil? v) (not v))) 52 | 53 | (define (truthy? v) 54 | (and (not (nil? v)) v)) 55 | 56 | (define (procedure?* v) 57 | (and (procedure? v) 58 | (not (nil? v)))) 59 | -------------------------------------------------------------------------------- /lua-lib/private/number.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "error.rkt" 4 | "nil.rkt" 5 | "string.rkt") 6 | 7 | (provide 8 | lua:tonumber) 9 | 10 | (define (lua:tonumber e [base nil] . _) 11 | (cond 12 | [(nil? e) nil] 13 | [(nil? base) 14 | (cond 15 | [(number? e) e] 16 | [(bytes? e) (string->number (bytes->string/utf-8 e))] 17 | [else (lua:error (format "tonumber: expected a number or a string, received ~a" (lua:tostring e)))])] 18 | [else 19 | (unless (bytes? e) 20 | (lua:error (format "tonumber: expected a string, received ~a" (lua:tostring e)))) 21 | (unless (and (exact-integer? base) 22 | (not (zero? base))) 23 | (lua:error (format "tonumber: expected a positive base, received ~a" (lua:tostring base)))) 24 | (or (string->number (bytes->string/utf-8 e) base) nil)])) 25 | -------------------------------------------------------------------------------- /lua-lib/private/realm.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (provide 4 | lua-realm) 5 | 6 | (define lua-realm 'lua) 7 | -------------------------------------------------------------------------------- /lua-lib/private/relation.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | syntax/parse/pre) 5 | "adjust.rkt" 6 | "error.rkt" 7 | "nil.rkt" 8 | "string.rkt" 9 | "table.rkt") 10 | 11 | ;; operators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 12 | ;; https://www.lua.org/manual/5.4/manual.html#3.4.4 13 | 14 | (provide 15 | lua:== 16 | lua:~=) 17 | 18 | (define (lua:== a b) 19 | (or (equal? a b) 20 | (and (table? a) 21 | (table? b) 22 | (let ([lhs-eq (table-meta-ref a #"__eq")] 23 | [rhs-eq (table-meta-ref a #"__eq")]) 24 | (cond 25 | [(procedure?* lhs-eq) (and (lhs-eq a b) #t)] 26 | [(procedure?* rhs-eq) (and (rhs-eq b a) #t)] 27 | [else #f]))))) 28 | 29 | (define (lua:~= a b) 30 | (not (lua:== a b))) 31 | 32 | (define ((make-binop-dunder-proc who dunder-name) a b) 33 | (cond 34 | [(and dunder-name 35 | (or (table? a) 36 | (table? b))) 37 | (define lhs-dunder-proc (and (table? a) (table-meta-ref a dunder-name))) 38 | (define rhs-dunder-proc (and (table? b) (table-meta-ref b dunder-name))) 39 | (cond 40 | [(procedure?* lhs-dunder-proc) 41 | (lua:adjust* (λ () (lhs-dunder-proc a b)))] 42 | [(procedure?* rhs-dunder-proc) 43 | (lua:adjust* (λ () (rhs-dunder-proc b a)))] 44 | [else 45 | (lua:error (format "~a: expected two numbers or two strings, received ~a and ~a" 46 | who 47 | (lua:tostring a) 48 | (lua:tostring b)))])] 49 | [else 50 | (lua:error (format "~a: expected two numbers or two strings, received ~a and ~a" 51 | who 52 | (lua:tostring a) 53 | (lua:tostring b)))])) 54 | 55 | (define-syntax (define-binop stx) 56 | (syntax-parse stx 57 | [(_ id:id who:string dunder-name:expr number-proc:expr string-proc:expr) 58 | #'(begin 59 | (provide id) 60 | (define dunder-proc 61 | (make-binop-dunder-proc 'who dunder-name)) 62 | (define (id a b) 63 | (cond 64 | [(and (number? a) 65 | (number? b)) 66 | (number-proc a b)] 67 | [(and (bytes? a) 68 | (bytes? b)) 69 | (string-proc a b)] 70 | [else 71 | (dunder-proc a b)])))])) 72 | 73 | (define-syntax-rule (define-binops [def ...] ...) 74 | (begin (define-binop def ...) ...)) 75 | 76 | (define (bytes<=? a b) 77 | (or (bytes=? a b) (bytes=? a b) 80 | (or (bytes=? a b) (bytes>? a b))) 81 | 82 | (define-binops 83 | [lua:< "<" #"__lt" < bytes ">" #f > bytes>?] 85 | [lua:<= "<=" #"__le" <= bytes<=?] 86 | [lua:>= ">=" #f >= bytes>=?]) 87 | 88 | 89 | ;; procedures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 90 | 91 | (provide 92 | lua:rawequal) 93 | 94 | (define (lua:rawequal a b . _) 95 | (equal? a b)) 96 | -------------------------------------------------------------------------------- /lua-lib/private/string.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require iso-printf 4 | iso-printf/custom 5 | racket/format 6 | racket/port 7 | "exn.rkt" 8 | "nil.rkt" 9 | "table.rkt") 10 | 11 | (provide 12 | current-print-ids? 13 | current-string-metatable 14 | lua:tostring 15 | lua:print 16 | lua:format) 17 | 18 | (define current-print-ids? 19 | (make-parameter #t)) 20 | 21 | (define current-string-metatable 22 | (make-parameter nil)) 23 | 24 | (define (->string what v) 25 | (if (current-print-ids?) 26 | (format "<~a: 0x~a>" what (number->string (eq-hash-code v) 16)) 27 | (format "<~a>" what))) 28 | 29 | (define (lua:toliteral v) 30 | (cond 31 | [(eq? v #t) "true"] 32 | [(eq? v #f) "false"] 33 | [(eq? v nil) "nil"] 34 | [(exact-integer? v) 35 | (number->string v 10)] 36 | [(number? v) 37 | (~a "0x" 38 | (~r v 39 | #:base 16 40 | #:notation 'exponential 41 | #:format-exponent "p"))] 42 | [(bytes? v) 43 | (quote-string (bytes->string/utf-8 v))] 44 | [else 45 | (raise-lua-error 'string.format "value has no literal form")])) 46 | 47 | (define (lua:tostring v . _) 48 | (define str 49 | (cond 50 | [(eq? v #t) "true"] 51 | [(eq? v #f) "false"] 52 | [(eq? v nil) "nil"] 53 | [(table? v) 54 | (define dunder-proc 55 | (table-meta-ref v #"__tostring")) 56 | (cond 57 | [(nil? dunder-proc) 58 | (define name (table-meta-ref v #"__name" (λ () #"table"))) 59 | (->string name v)] 60 | [else 61 | (dunder-proc v)])] 62 | [(procedure?* v) 63 | (->string "function" v)] 64 | [else 65 | (~a v)])) 66 | (cond 67 | [(bytes? str) str] 68 | [else (string->bytes/utf-8 str)])) 69 | 70 | (define (lua:print . vs) 71 | (for ([i (in-naturals)] 72 | [v (in-list vs)]) 73 | (unless (zero? i) 74 | (display #\tab)) 75 | (display (lua:tostring v))) 76 | (newline)) 77 | 78 | (define custom-printf-table 79 | (hasheq #\q (lambda (flags arg width precision) 80 | (define (formatter _precision v) 81 | (values "" (lua:toliteral v))) 82 | (directive arg flags width precision formatter nil #t)))) 83 | 84 | (define (lua:format fmt . args) 85 | (parameterize ([current-custom-conversions custom-printf-table]) 86 | (string->bytes/utf-8 (apply sprintf (bytes->string/utf-8 fmt) args)))) 87 | 88 | (define (quote-string s) 89 | (define escaped 90 | (call-with-output-string 91 | (lambda (out) 92 | (for/fold ([escaped? #f]) 93 | ([c (in-string s)]) 94 | (case c 95 | [(#\\ #\" #\newline) 96 | (cond 97 | [escaped? 98 | (begin0 #f 99 | (write-char c out))] 100 | [(eqv? #\\ c) 101 | (begin0 #t 102 | (write-char c out))] 103 | [else 104 | (begin0 #f 105 | (write-char #\\ out) 106 | (write-char c out))])] 107 | [else 108 | (begin0 #f 109 | (write-char c out))]))))) 110 | (string-append "\"" escaped "\"")) 111 | -------------------------------------------------------------------------------- /lua-lib/private/type.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require "nil.rkt" 4 | "table.rkt") 5 | 6 | (provide 7 | lua:type) 8 | 9 | (define (lua:type v . _) 10 | (cond 11 | [(nil? v) #"nil"] 12 | [(table? v) #"table"] 13 | [(bytes? v) #"string"] 14 | [(number? v) #"number"] 15 | [(boolean? v) #"boolean"] 16 | [(procedure?* v) #"procedure"] 17 | [else (raise-argument-error 'lua:type "unexpected value" v)])) 18 | -------------------------------------------------------------------------------- /lua-lib/repl.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require (for-syntax racket/base 4 | syntax/parse/pre) 5 | (prefix-in racket: racket/base) 6 | "base.rkt" 7 | (prefix-in lua: "private/table.rkt")) 8 | 9 | (provide 10 | (except-out (all-from-out "base.rkt") #%top #%set!) 11 | (rename-out 12 | [repl:top #%top] 13 | [repl:set! #%set!])) 14 | 15 | (#%define _ENV (#%global)) 16 | (#%load-stdlib! _ENV) 17 | 18 | (begin-for-syntax 19 | (define (id-stx->bytes-stx stx) 20 | (datum->syntax stx (string->bytes/utf-8 (symbol->string (syntax->datum stx)))))) 21 | 22 | (define-syntax (repl:top stx) 23 | (syntax-parse stx 24 | [(_ . id:id) 25 | #:with name (id-stx->bytes-stx #'id) 26 | #'(#%subscript _ENV name)])) 27 | 28 | (define-syntax (repl:set! stx) 29 | (syntax-parse stx 30 | #:literals (#%subscript) 31 | [(_ (#%subscript t:expr k:expr) v:expr) 32 | #'(lua:table-set! t k v)] 33 | [(_ id:id e:expr) 34 | #:when (identifier-binding #'id) 35 | #'(racket:set! id e)] 36 | [(_ id:id e:expr) 37 | #:with name (id-stx->bytes-stx #'id) 38 | #'(lua:table-set! _ENV name e)])) 39 | 40 | (module configure-runtime racket/base 41 | (require (submod "lang/compiler.rkt" private) 42 | "lang/lexer.rkt" 43 | "lang/parser.rkt" 44 | "lang/runtime-config.rkt" 45 | "private/string.rkt" 46 | racket/port 47 | racket/pretty 48 | syntax/strip-context) 49 | 50 | (configure-runtime) 51 | 52 | ;; Avoid depending on readline-lib. 53 | (define readline-prompt 54 | (or (dynamic-require 'readline/pread 'readline-prompt) 55 | (make-parameter #f))) 56 | 57 | (define debug? 58 | (not (not (getenv "RACKET_LUA_DEBUG")))) 59 | 60 | (define (read-interaction src in) 61 | (define (interact code-in) 62 | (define code 63 | (strip-context 64 | #`(#%let/cc #%return 65 | #,(compile-block 66 | (parameterize ([current-source-name src]) 67 | (parse-chunk (make-lexer code-in)))) 68 | (#%values)))) 69 | (begin0 code 70 | (when debug? 71 | (pretty-print (syntax->datum code))))) 72 | (define line (read-line in)) 73 | (cond 74 | [(eof-object? line) eof] 75 | [(string=? ":{" line) 76 | (interact 77 | (open-input-string 78 | (call-with-output-string 79 | (lambda (out) 80 | (let loop () 81 | (parameterize ([readline-prompt #"| "]) 82 | (define code-line (read-line in)) 83 | (unless (string=? ":}" code-line) 84 | (displayln code-line out) 85 | (loop))))))))] 86 | [else 87 | (interact (open-input-string line))])) 88 | 89 | (define (this-print v) 90 | (unless (void? v) 91 | (lua:print v))) 92 | 93 | (current-read-interaction read-interaction) 94 | (current-print this-print)) 95 | -------------------------------------------------------------------------------- /lua-lib/stdlib/coroutine.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local callcc = racket["call/cc"] 4 | 5 | local suspended, running, dead = 1, 2, 3 6 | local statuses = {"suspended", "running", "dead"} 7 | local thread = {} 8 | thread.__name = "thread" 9 | thread.__index = thread 10 | function thread.new(proc, status) 11 | o = { k = proc, status = status or suspended } 12 | setmetatable(o, thread) 13 | return o 14 | end 15 | 16 | local coroutine = {} 17 | local current = thread.new(nil, running) 18 | local main = current 19 | 20 | function coroutine.close(coro) 21 | if coro.status == suspended then 22 | error("coroutine.close: cannot close a running coroutine") 23 | end 24 | coro.status = "dead" 25 | coro.k = nil 26 | coro.yield = nil 27 | return true 28 | end 29 | 30 | function coroutine.create(proc) 31 | return thread.new(proc) 32 | end 33 | 34 | function coroutine.isyieldable(coro) 35 | return (coro or current).yield ~= nil 36 | end 37 | 38 | function coroutine.resume(coro, ...) 39 | if coro.status ~= suspended then 40 | return false, "coroutine.resume: cannot resume non-suspended coroutine" 41 | end 42 | 43 | local args = {...} 44 | local function impl(k) 45 | if current.status ~= dead then 46 | current.status = suspended 47 | current.k = k 48 | end 49 | current.yield = nil 50 | local old = current 51 | 52 | current = coro 53 | coro.status = running 54 | coro.yield = function(...) 55 | return coroutine.resume(old, true, ...) 56 | end 57 | 58 | local function resume() 59 | return table.pack(coro.k(table.unpack(args))) 60 | end 61 | local status, res = pcall(resume) 62 | coro.status = dead 63 | if status then 64 | coroutine.resume(old, true, table.unpack(res)) 65 | else 66 | coroutine.resume(old, false, res) 67 | end 68 | end 69 | return callcc(impl) 70 | end 71 | 72 | function coroutine.running() 73 | return current, current == main 74 | end 75 | 76 | function coroutine.status(c) 77 | return statuses[c.status] 78 | end 79 | 80 | function coroutine.wrap(proc) 81 | local coro = coroutine.create(proc) 82 | local wrapped = function(...) 83 | return select(2, coroutine.resume(coro, ...)) 84 | end 85 | return wrapped 86 | end 87 | 88 | function coroutine.yield(...) 89 | if not current.yield then 90 | error("coroutine.yield: attempt to yield from outside a coroutine") 91 | end 92 | return current.yield(...) 93 | end 94 | 95 | return coroutine 96 | -------------------------------------------------------------------------------- /lua-lib/stdlib/file.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local flush_output = racket["flush-output"] 4 | local port_to_bytes = racket.lib("racket/port", "port->bytes") 5 | local port_closed_p = racket["port-closed?"] 6 | local close_input_port = racket["close-input-port"] 7 | local close_output_port = racket["close-output-port"] 8 | local eof = racket["eof"] 9 | local eof_p = racket["eof-object?"] 10 | local read_bytes = racket["read-bytes"] 11 | local read_bytes_line = racket["read-bytes-line"] 12 | local write_bytes = racket["write-bytes"] 13 | local file_position = racket["file-position"] 14 | local file_stream_buffer_mode = racket["file-stream-buffer-mode"] 15 | local bytes_to_symbol = racket.compose1(racket["string->symbol"], racket["bytes->string/utf-8"]) 16 | 17 | local buf_syms = { 18 | no = bytes_to_symbol("none"), 19 | line = bytes_to_symbol("line"), 20 | full = bytes_to_symbol("block") 21 | } 22 | 23 | local file = {} 24 | file.__name = "file" 25 | file.__index = file 26 | function file.new(path, inp, out) 27 | if inp and out then 28 | error("file.new: either inp or out args must be provided, but not both") 29 | end 30 | local o = { 31 | path = path, 32 | _inp = inp, 33 | _out = out 34 | } 35 | setmetatable(o, file) 36 | return o 37 | end 38 | 39 | function file:close() 40 | if self._inp then 41 | close_input_port(self._inp) 42 | elseif self._out then 43 | close_output_port(self._out) 44 | end 45 | end 46 | 47 | function file:isclosed() 48 | return port_closed_p(self._inp or self._out) 49 | end 50 | 51 | function file:flush() 52 | if not self._out then 53 | error("file:flush: not writable") 54 | end 55 | flush_output(self._out) 56 | end 57 | 58 | function file:read(...) 59 | if not self._inp then 60 | error("file:read: not readable") 61 | elseif self:isclosed() then 62 | error("file:read: closed") 63 | end 64 | local function go(fmt, ...) 65 | if fmt ~= nil then 66 | local res 67 | if fmt == "a" or fmt == "*all" then 68 | res = port_to_bytes(self._inp) 69 | elseif fmt == "l" then 70 | res = read_bytes_line(self._inp) 71 | elseif type(fmt) == "number" then 72 | res = read_bytes(fmt, self._inp) 73 | else 74 | error("file:read: invalid format") 75 | end 76 | if eof_p(res) then 77 | return nil 78 | end 79 | return res, go(...) 80 | end 81 | end 82 | return go(...) 83 | end 84 | 85 | function file:write(...) 86 | if not self._out then 87 | error("file:write: not writable") 88 | elseif self:isclosed() then 89 | error("file:write: closed") 90 | end 91 | local port = self._out 92 | for _, arg in ipairs({...}) do 93 | write_bytes(tostring(arg), port) 94 | end 95 | end 96 | 97 | function file:seek(whence, offset) 98 | whence = whence or "curr" 99 | offset = offset or 0 100 | local port = self._inp or self._out 101 | if whence == "curr" then 102 | return file_position(port) + offset 103 | elseif whence == "set" then 104 | file_position(port, offset) 105 | return offset 106 | elseif whence == "end" then 107 | file_position(port, eof) 108 | local pos = file_position(port) - offset 109 | file_position(port, pos) 110 | return pos 111 | else 112 | error("file:seek: whence must be 'curr', 'set' or 'end'", whence) 113 | end 114 | end 115 | 116 | function file:lines(...) 117 | local fmt = {...} 118 | if #fmt == 0 then 119 | fmt = {"l"} 120 | end 121 | return function() 122 | return self:read(table.unpack(fmt)) 123 | end 124 | end 125 | 126 | function file:setvbuf(mode, size) 127 | if size ~= nil then 128 | error("file:setvbuf: size argument not supported") 129 | end 130 | local port = self._inp or self._out 131 | local modeval = buf_syms[mode] 132 | if modeval == nil then 133 | error("file:setvbuf: invalid mode", mode) 134 | end 135 | file_stream_buffer_mode(port, modeval) 136 | end 137 | 138 | return file 139 | -------------------------------------------------------------------------------- /lua-lib/stdlib/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define module-suffixes '(#"lua")) 4 | (define test-omit-paths '(#rx".lua")) 5 | -------------------------------------------------------------------------------- /lua-lib/stdlib/io.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local bytes_to_path = racket["bytes->path"] 4 | local open_input_file = racket["open-input-file"] 5 | local open_output_file = racket["open-output-file"] 6 | local get_current_input_port = racket["current-input-port"] 7 | local get_current_output_port = racket["current-output-port"] 8 | local get_current_error_port = racket["current-error-port"] 9 | 10 | local STDIN, STDOUT, STDERR = 0, 1, 2 11 | 12 | local io = { 13 | stdin = STDIN, 14 | stdout = STDOUT, 15 | stderr = STDERR 16 | } 17 | 18 | function io.open(filename, mode) 19 | if type(filename) ~= "string" then 20 | error("io.open: expected a string filename", filename) 21 | end 22 | mode = mode or "r" 23 | path = bytes_to_path(filename) 24 | if mode == "r" or mode == "rb" then 25 | return file.new(filename, open_input_file(path)) 26 | elseif mode == "w" or mode == "wb" then 27 | return file.new(filename, nil, open_output_file(path)) 28 | else 29 | error("io.open: unsupported mode", mode) 30 | end 31 | end 32 | 33 | function io.close(f) 34 | if f then 35 | f:close() 36 | else 37 | io.output():close() 38 | end 39 | end 40 | 41 | function io.input(name) 42 | if not name or name == STDIN then 43 | return file.new("", get_current_input_port()) 44 | end 45 | return io.open(name, "r") 46 | end 47 | 48 | function io.output(name) 49 | if not name or name == STDOUT then 50 | return file.new("", nil, get_current_output_port()) 51 | elseif name == STDERR then 52 | return file.new("", nil, get_current_error_port()) 53 | end 54 | return io.open(name, "w") 55 | end 56 | 57 | function io.tmpfile() 58 | return io.output(os.tmpname()) 59 | end 60 | 61 | function io.type(f) 62 | if getmetatable(f) == file then 63 | if f.isclosed() then 64 | return "closed file" 65 | end 66 | return "file" 67 | end 68 | end 69 | 70 | function io.write(...) 71 | io.output():write(...) 72 | end 73 | 74 | return io 75 | -------------------------------------------------------------------------------- /lua-lib/stdlib/math.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local abs = racket.abs 4 | local acos = racket.acos 5 | local asin = racket.asin 6 | local atan = racket.atan 7 | local ceil = racket.ceiling 8 | local cos = racket.cos 9 | local deg = racket.lib("racket/math", "radians->degrees") 10 | local exp = racket.exp 11 | local floor = racket.floor 12 | local log = racket.log 13 | local pi = racket.lib("racket/math", "pi") 14 | local rad = racket.lib("racket/math", "degrees->radians") 15 | local random = racket.random 16 | local random_seed = racket["random-seed"] 17 | local sin = racket.sin 18 | local sqrt = racket.sqrt 19 | local tan = racket.tan 20 | local number_p = racket["number?"] 21 | local integer_p = racket["integer?"] 22 | 23 | local math = {} 24 | local function check(who, n, argn) 25 | if type(n) ~= "number" then 26 | argn = argn or 1 27 | error("math." .. who .. ": bad argument #" .. tostring(argn) .. "; not a number") 28 | end 29 | end 30 | 31 | function math.abs(n) 32 | check("abs", n) 33 | return abs(n) 34 | end 35 | 36 | function math.acos(n) 37 | check("acos", n) 38 | return acos(n) 39 | end 40 | 41 | function math.asin(n) 42 | check("acos", n) 43 | return asin(n) 44 | end 45 | 46 | function math.atan(y, x) 47 | check("atan", y) 48 | if x ~= nil then 49 | check("atan", x, 2) 50 | return atan(y, x) 51 | end 52 | return atan(y) 53 | end 54 | 55 | function math.ceil(n) 56 | check("ceil", n) 57 | return ceil(n) 58 | end 59 | 60 | function math.cos(n) 61 | check("cos", n) 62 | return cos(n) 63 | end 64 | 65 | function math.deg(n) 66 | check("deg", n) 67 | return deg(n) 68 | end 69 | 70 | function math.exp(n) 71 | check("exp", n) 72 | return exp(n) 73 | end 74 | 75 | function math.floor(n) 76 | check("floor", n) 77 | return floor(n) 78 | end 79 | 80 | function math.log(n, base) 81 | check("log", n) 82 | if base ~= nil then 83 | check("log", base, 2) 84 | return log(n, base) 85 | end 86 | return log(n) 87 | end 88 | 89 | function math.max(n, ...) 90 | if n == nil then 91 | error("math.max: bad argument #1 to max (value expected)") 92 | end 93 | local max = n 94 | for _, n in ipairs({...}) do 95 | if max < n then 96 | max = n 97 | end 98 | end 99 | return max 100 | end 101 | 102 | function math.min(n, ...) 103 | if n == nil then 104 | error("math.min: bad argument #1 to min (value expected)") 105 | end 106 | local min = n 107 | for _, n in ipairs({...}) do 108 | if n < min then 109 | min = n 110 | end 111 | end 112 | return min 113 | end 114 | 115 | math.pi = pi 116 | 117 | function math.rad(n) 118 | check("rad", n) 119 | return rad(n) 120 | end 121 | 122 | function math.random(m, n) 123 | if m ~= nil then 124 | check("random", m) 125 | if n ~= nil then 126 | check("random", n, 2) 127 | return random(m, n) 128 | end 129 | return random(m) 130 | end 131 | return random() 132 | end 133 | 134 | function math.randomseed(x, y) 135 | x = x or os.time() 136 | y = y or 0 137 | random_seed((x ~ y) & 0x7FFFFFFF) 138 | return x, y 139 | end 140 | 141 | function math.sin(n) 142 | check("sin", n) 143 | return sin(n) 144 | end 145 | 146 | function math.sqrt(n) 147 | check("sqrt", n) 148 | return sqrt(n) 149 | end 150 | 151 | function math.tan(n) 152 | check("tan", n) 153 | return tan(n) 154 | end 155 | 156 | function math.tointeger(n) 157 | return tointeger(n) 158 | end 159 | 160 | function math.type(n) 161 | if integer_p(n) then 162 | return "integer" 163 | elseif number_p(n) then 164 | return "float" 165 | end 166 | return nil 167 | end 168 | 169 | return math 170 | -------------------------------------------------------------------------------- /lua-lib/stdlib/os.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local exit = racket.exit 4 | local getenv = racket.getenv 5 | local delete_file = racket["delete-file"] 6 | local make_tmpfile = racket.lib("racket/file", "make-temporary-file") 7 | local bytes_to_path = racket["bytes->path"] 8 | local path_to_bytes = racket["path->bytes"] 9 | local bytes_to_string = racket["bytes->string/utf-8"] 10 | local string_to_bytes = racket["string->bytes/utf-8"] 11 | local current_seconds = racket["current-seconds"] 12 | local current_process_millis = racket["current-process-milliseconds"] 13 | local find_seconds = racket.lib("racket/date", "find-seconds") 14 | local seconds_to_date = racket["seconds->date"] 15 | local date_second = racket["date-second"] 16 | local date_minute = racket["date-minute"] 17 | local date_hour = racket["date-hour"] 18 | local date_day = racket["date-day"] 19 | local date_month = racket["date-month"] 20 | local date_year = racket["date-year"] 21 | local date_week_day = racket["date-week-day"] 22 | local date_year_day = racket["date-year-day"] 23 | local date_is_dst = racket["date-dst?"] 24 | local date_to_string = racket.lib("racket/date", "date->string") 25 | 26 | local os = {} 27 | 28 | function os.clock() 29 | return current_process_millis() 30 | end 31 | 32 | function os.date(format, time) 33 | local isutc = false 34 | if format and format:byte(1) == 33 then 35 | isutc = true 36 | format = format:sub(2) 37 | end 38 | time = time or os.time() 39 | local date = seconds_to_date(time, not isutc) 40 | if format == "*t" then 41 | return { 42 | year = date_year(date), 43 | month = date_month(date), 44 | day = date_day(date), 45 | hour = date_hour(date), 46 | min = date_minute(date), 47 | sec = date_second(date), 48 | wday = date_week_day(date), 49 | yday = date_year_day(date), 50 | isdst = date_is_dst(date) 51 | } 52 | elseif format == "" or not format then 53 | return string_to_bytes(date_to_string(date, true)) 54 | else 55 | error"os.date: '[!]' and '[!]*t' are the only formats currently supported" 56 | end 57 | end 58 | 59 | function os.difftime(t2, t1) 60 | return t2 - t1 61 | end 62 | 63 | function os.execute() 64 | error("os.execute: not supported") 65 | end 66 | 67 | function os.exit(code) 68 | exit(code) 69 | end 70 | 71 | function os.getenv(varname) 72 | return getenv(bytes_to_string(varname)) 73 | end 74 | 75 | function os.remove(path) 76 | return pcall(function() delete_file(bytes_to_path(path)) end) 77 | end 78 | 79 | function os.rename(oldname, newname) 80 | local oldpath = bytes_to_path(oldname) 81 | local newpath = bytes_to_path(newname) 82 | return pcall(function() rename_file_or_directory(oldpath, newpath) end) 83 | end 84 | 85 | function os.setlocale() 86 | error("os.setlocale: not supported") 87 | end 88 | 89 | function os.time(t) 90 | if t ~= nil then 91 | if t.isdst ~= nil then 92 | error"os.time: the isdst field is not supported" 93 | end 94 | for k, v in pairs(t) do 95 | if k ~= "isutc" and v < 0 then 96 | error"os.time: negative fields are not supported" 97 | end 98 | end 99 | local localtime = true 100 | if t.isutc ~= nil and t.isutc then 101 | localtime = false 102 | end 103 | return find_seconds( 104 | t.sec or 0, 105 | t.min or 0, 106 | t.hour or 12, 107 | t.day or 1, 108 | t.month or 1, 109 | t.year or 1970, 110 | localtime 111 | ) 112 | end 113 | return current_seconds() 114 | end 115 | 116 | function os.tmpname() 117 | local path = make_tmpfile(bytes_to_string("luatmp~a")) 118 | delete_file(path) 119 | return path_to_bytes(path) 120 | end 121 | 122 | return os 123 | -------------------------------------------------------------------------------- /lua-lib/stdlib/racket.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local Racket = {} 4 | local require = racket["#%require"] 5 | function Racket:__index(id) 6 | return require(id) 7 | end 8 | 9 | local racket = {} 10 | setmetatable(racket, Racket) 11 | function racket.lib(mod, id) 12 | return require(id, mod) 13 | end 14 | 15 | local list = racket.list 16 | local null = racket.null 17 | local apply = racket.apply 18 | local values = racket.values 19 | local time_apply = racket["time-apply"] 20 | function racket.time(proc, ...) 21 | local res, cpu, real, gc = time_apply(proc, list(...)) 22 | print("cpu time:", cpu, "real time:", real, "gc:", gc) 23 | return apply(values, res) 24 | end 25 | 26 | return racket 27 | -------------------------------------------------------------------------------- /lua-lib/stdlib/string.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local alloc = racket["make-bytes"] 4 | local str = racket["bytes"] 5 | local len = racket["bytes-length"] 6 | local ref = racket["bytes-ref"] 7 | local set = racket["bytes-set!"] 8 | local copy = racket["bytes-copy!"] 9 | local sub = racket.subbytes 10 | local downcase = racket["string-downcase"] 11 | local upcase = racket["string-upcase"] 12 | local to_str = racket["bytes->string/locale"] 13 | local to_bytes = racket["string->bytes/locale"] 14 | 15 | local string = {} 16 | string.__index = string 17 | local function check(who, v, argn, typ) 18 | typ = typ or "string" 19 | if type(v) ~= typ then 20 | argn = argn or 1 21 | error("string." .. who .. ": bad argument #" .. tostring(argn) .. "; expected a " .. typ, v) 22 | end 23 | end 24 | 25 | local function indices(s, i, j) 26 | local len = #s 27 | if i < 1 then 28 | i = 1 + (i % len) 29 | end 30 | j = j or -1 31 | if j < 1 then 32 | j = 1 + (j % len) 33 | end 34 | return i, j 35 | end 36 | 37 | local function numbers(who, a, b) 38 | a = tonumber(a) 39 | if not a then 40 | error("string." .. who .. ": bad lhs; expected a numeric string") 41 | end 42 | b = tonumber(b) 43 | if not b then 44 | error("string." .. who .. ": bad rhs; expected a numeric string") 45 | end 46 | return a, b 47 | end 48 | 49 | function string.__add(a, b) 50 | a, b = numbers("__add", a, b) 51 | return a + b 52 | end 53 | 54 | function string.__mul(a, b) 55 | a, b = numbers("__mul", a, b) 56 | return a * b 57 | end 58 | 59 | function string.byte(s, i, j) 60 | check("byte", s) 61 | local i, j = indices(s, i or 1, j or i or 1) 62 | if i > j then 63 | return nil 64 | end 65 | local function impl(i) 66 | if i <= j then 67 | return ref(s, i - 1), impl(i + 1) 68 | end 69 | end 70 | return impl(i) 71 | end 72 | 73 | function string.char(...) 74 | return str(...) 75 | end 76 | 77 | function string.dump() 78 | error"string.dump: not implemented" 79 | end 80 | 81 | function string.find(s, pattern, init, plain) 82 | error"string.find: not implemented" 83 | end 84 | 85 | function string.format(fmt, ...) 86 | error"string.format: bootstrap failed" 87 | end 88 | 89 | function string.gmatch(s, pattern, init) 90 | error"string.gmatch: not implemented" 91 | end 92 | 93 | function string.gsub(s, pattern, repl, init) 94 | error"string.gsub: not implemented" 95 | end 96 | 97 | function string.len(s) 98 | return len(s) 99 | end 100 | 101 | function string.lower(s) 102 | check("lower", s) 103 | return to_bytes(downcase(to_str(s))) 104 | end 105 | 106 | function string.match(s, pattern, init) 107 | error"string.match: not implemented" 108 | end 109 | 110 | function string.pack() 111 | error"string.pack: not implemented" 112 | end 113 | 114 | function string.packsize() 115 | error"string.packsize: not implemented" 116 | end 117 | 118 | function string.rep(s, n, sep) 119 | check("rep", s) 120 | check("rep", n, 2, "number") 121 | if n < 1 then 122 | return "" 123 | end 124 | sep = sep or "" 125 | check("rep", sep, 3) 126 | local srclen = #s 127 | local seplen = #sep 128 | local step = srclen + seplen 129 | local len = step * n - seplen 130 | local dst = alloc(len) 131 | copy(dst, 0, s) 132 | for i = srclen, len - 1, step do 133 | copy(dst, i, sep) 134 | copy(dst, i + seplen, s) 135 | end 136 | return dst 137 | end 138 | 139 | function string.reverse(s) 140 | check("reverse", s) 141 | local len = #s 142 | if len == 0 then 143 | return "" 144 | end 145 | local dst = alloc(len) 146 | local j = 0 147 | for i = len-1, 0, -1 do 148 | set(dst, j, ref(s, i)) 149 | j = j + 1 150 | end 151 | return dst 152 | end 153 | 154 | function string.sub(s, i, j) 155 | check("sub", s) 156 | check("sub", i, 1, "number") 157 | local i, j = indices(s, i, j or -1) 158 | if i > j then 159 | return "" 160 | end 161 | return sub(s, i - 1, j) 162 | end 163 | 164 | function string.unpack(s) 165 | error"string.unpack: not implemented" 166 | end 167 | 168 | function string.upper(s) 169 | check("upper", s) 170 | return to_bytes(upcase(to_str(s))) 171 | end 172 | 173 | return string 174 | -------------------------------------------------------------------------------- /lua-lib/stdlib/table.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local call_bytes = racket.lib("racket/port", "call-with-output-bytes") 4 | local write_bytes = racket["write-bytes"] 5 | 6 | local table = {} 7 | 8 | function table.select(index, ...) 9 | local args = {...} 10 | if index == "#" then 11 | return #args 12 | elseif type(index) ~= "number" then 13 | error("select: expected an integer index or '#'") 14 | end 15 | if index == 0 then 16 | error("select: 0 index") 17 | elseif index < 0 then 18 | index = 1 + (index % #args) 19 | end 20 | return table.unpack(args, index) 21 | end 22 | 23 | function table.pack(v, ...) 24 | if v == nil then 25 | return {} 26 | end 27 | local args = {v, ...} 28 | return {n = #args, table.unpack(args)} 29 | end 30 | 31 | function table.unpack(t, i, j) 32 | local function go(i, j) 33 | if i <= j then 34 | return t[i], go(i + 1, j) 35 | end 36 | end 37 | return go(i or 1, j or #t) 38 | end 39 | 40 | function table.insert(t, pos, value) 41 | local len = #t 42 | if value == nil then 43 | value = pos 44 | pos = len + 1 45 | end 46 | if pos == len + 1 then 47 | t[pos] = value 48 | return nil 49 | end 50 | if pos < 1 or pos > len + 1 then 51 | error("table.insert: position out of bounds", pos) 52 | end 53 | for i = len + 1, pos + 1, -1 do 54 | t[i] = t[i-1] 55 | end 56 | t[pos] = value 57 | end 58 | 59 | function table.remove(t, pos) 60 | local len = #t 61 | pos = pos or len 62 | if pos == 0 and len == 0 then 63 | return nil 64 | end 65 | if pos < 1 or pos > len then 66 | error("table.remove: position out of bounds", pos) 67 | end 68 | local value = t[pos] 69 | for i = pos, len do 70 | t[i] = t[i+1] 71 | end 72 | return value 73 | end 74 | 75 | function table.concat(t, sep, i, j) 76 | sep = sep or "" 77 | if type(sep) ~= "string" then 78 | error("table.concat: invalid separator value") 79 | end 80 | i = i or 1 81 | j = j or #t 82 | local function impl(out) 83 | for k = i, j do 84 | local v = t[k] 85 | if k > i then 86 | write_bytes(sep, out) 87 | end 88 | if type(v) == "string" then 89 | write_bytes(v, out) 90 | elseif type(v) == "number" then 91 | write_bytes(tostring(v), out) 92 | else 93 | error("table.concat: invalid table value", v) 94 | end 95 | end 96 | end 97 | return call_bytes(impl) 98 | end 99 | 100 | -- Replaced during bootstrap. 101 | function table.sort(...) 102 | error("table.sort: implementation missing") 103 | end 104 | 105 | return table 106 | -------------------------------------------------------------------------------- /lua-lib/value.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require racket/contract/base 4 | "private/nil.rkt" 5 | "private/table.rkt" 6 | "private/string.rkt") 7 | 8 | (provide 9 | lua-value/c 10 | nil 11 | nil? 12 | table? 13 | (contract-out 14 | [make-table (-> (or/c lua-value/c (cons/c lua-value/c lua-value/c)) ... table?)] 15 | [in-table (-> table? sequence?)] 16 | [table-ref (->* (table? lua-value/c) ((-> lua-value/c)) lua-value/c)] 17 | [table-set! (-> table? lua-value/c lua-value/c void?)] 18 | [table-length (-> table? exact-nonnegative-integer?)] 19 | [rename lua:getmetatable table-metatable (-> table? (or/c nil? table?))] 20 | [rename lua:setmetatable set-table-metatable! (-> table? table? table?)] 21 | [table-sort! (->* (table?) ((-> any/c any/c boolean?)) void?)] 22 | [rename lua:tostring ~lua-bytes (-> lua-value/c bytes?)] 23 | [~lua (-> lua-value/c string?)])) 24 | 25 | (define lua-value/c 26 | (or/c boolean? bytes? number? nil? procedure? table?)) 27 | 28 | (define (~lua v) 29 | (bytes->string/utf-8 (lua:tostring v) #\uFFFD)) 30 | -------------------------------------------------------------------------------- /lua-test/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Bogdan Popa 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | 3. Neither the name of the copyright holder nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /lua-test/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define license 'BSD-3-Clause) 4 | (define collections "tests") 5 | (define deps '("base" 6 | "lua-lib" 7 | "rackcheck-lib" 8 | "rackunit-lib")) 9 | (define build-deps '("rackunit-lib")) 10 | (define implies '("lua-lib")) 11 | -------------------------------------------------------------------------------- /lua-test/lua/compiler.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require lua/private/env 4 | lua/private/string 5 | racket/date 6 | racket/path 7 | racket/port 8 | racket/runtime-path 9 | rackunit) 10 | 11 | (define-runtime-path examples 12 | "examples") 13 | 14 | (define compiler-tests 15 | (test-suite 16 | "compiler" 17 | 18 | (test-suite 19 | "examples" 20 | 21 | (for ([path (in-list (directory-list examples #:build? #t))] 22 | #:when (path-has-extension? path #".lua") 23 | #:unless (equal? (string->path "kitchen-sink.lua") (file-name-from-path path))) 24 | (define stdout-path 25 | (path-replace-extension path #".stdout.rktd")) 26 | (define stdout 27 | (call-with-output-string 28 | (lambda (out) 29 | (with-handlers ([exn:fail? (λ (e) (fail-check (format "~a: ~a" path (exn-message e))))]) 30 | (parameterize ([current-global-environment (make-initial-environment)] 31 | [current-print-ids? #f] 32 | [current-error-port out] 33 | [current-output-port out] 34 | [date-display-format 'iso-8601]) 35 | (dynamic-require `(submod (file ,(path->string path)) configure-runtime) #f void) 36 | (dynamic-require `(file ,(path->string path)) #f)))))) 37 | (with-handlers ([exn:fail:filesystem? 38 | (λ (_) 39 | (call-with-output-file stdout-path 40 | #:exists 'replace 41 | (lambda (out) 42 | (write stdout out))))]) 43 | (define expected-stdout 44 | (call-with-input-file stdout-path read)) 45 | (check-equal? stdout expected-stdout (path->string path))))))) 46 | 47 | (module+ test 48 | (require rackunit/text-ui) 49 | (run-tests compiler-tests)) 50 | -------------------------------------------------------------------------------- /lua-test/lua/examples/assignment-00.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | a = {} 4 | i = 3 5 | i, a[i] = i + 1, 20 6 | print(i, a[3]) 7 | 8 | x, y = 1, 2 9 | x, y = y, x 10 | print(x, y) 11 | 12 | local a = {} 13 | local i = 3 14 | i, a[i] = i + 5, 20 15 | print(i, a[3]) 16 | 17 | local x, y = 1, 2 18 | local x, y = y, x 19 | print(x, y) 20 | 21 | local function f() 22 | return 1, 2 23 | end 24 | local x, y = f() 25 | print(x, y) 26 | 27 | local x, y = 5, f() 28 | print(x, y) 29 | 30 | x, y = 5, f() 31 | print(x, y) 32 | 33 | x, y, z = 5, f() 34 | print(x, y, z) 35 | -------------------------------------------------------------------------------- /lua-test/lua/examples/assignment-00.stdout.rktd: -------------------------------------------------------------------------------- 1 | "4\t20\n2\t1\n8\t20\n2\t1\n1\t2\n5\t1\n5\t1\n5\t1\t2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/assignment-01.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | a = {} 4 | a.x = 5 5 | print(a.x) 6 | 7 | a.x, a[a.x] = 1, 2 8 | print(a.x, a[5]) 9 | -------------------------------------------------------------------------------- /lua-test/lua/examples/assignment-01.stdout.rktd: -------------------------------------------------------------------------------- 1 | "5\n1\t2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/assignment-01.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "a" a 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token lcubrace "{" "{" 3 4 16)) (10 . #s(token rcubrace "}" "}" 3 5 17)) (11 . #s(token whitespace "\n" "\n" 3 6 18)) (12 . #s(token name "a" a 4 0 19)) (13 . #s(token dot "." |.| 4 1 20)) (14 . #s(token name "x" x 4 2 21)) (15 . #s(token whitespace " " " " 4 3 22)) (16 . #s(token op "=" = 4 4 23)) (17 . #s(token whitespace " " " " 4 5 24)) (18 . #s(token number "5" 5 4 6 25)) (19 . #s(token whitespace "\n" "\n" 4 7 26)) (20 . #s(token name "print" print 5 0 27)) (21 . #s(token lparen "(" "(" 5 5 32)) (22 . #s(token name "a" a 5 6 33)) (23 . #s(token dot "." |.| 5 7 34)) (24 . #s(token name "x" x 5 8 35)) (25 . #s(token rparen ")" ")" 5 9 36)) (26 . #s(token whitespace "\n\n" "\n\n" 5 10 37)) (27 . #s(token name "a" a 7 0 39)) (28 . #s(token dot "." |.| 7 1 40)) (29 . #s(token name "x" x 7 2 41)) (30 . #s(token comma "," "," 7 3 42)) (31 . #s(token whitespace " " " " 7 4 43)) (32 . #s(token name "a" a 7 5 44)) (33 . #s(token lsqbrace "[" "[" 7 6 45)) (34 . #s(token name "a" a 7 7 46)) (35 . #s(token dot "." |.| 7 8 47)) (36 . #s(token name "x" x 7 9 48)) (37 . #s(token rsqbrace "]" "]" 7 10 49)) (38 . #s(token whitespace " " " " 7 11 50)) (39 . #s(token op "=" = 7 12 51)) (40 . #s(token whitespace " " " " 7 13 52)) (41 . #s(token number "1" 1 7 14 53)) (42 . #s(token comma "," "," 7 15 54)) (43 . #s(token whitespace " " " " 7 16 55)) (44 . #s(token number "2" 2 7 17 56)) (45 . #s(token whitespace "\n" "\n" 7 18 57)) (46 . #s(token name "print" print 8 0 58)) (47 . #s(token lparen "(" "(" 8 5 63)) (48 . #s(token name "a" a 8 6 64)) (49 . #s(token dot "." |.| 8 7 65)) (50 . #s(token name "x" x 8 8 66)) (51 . #s(token comma "," "," 8 9 67)) (52 . #s(token whitespace " " " " 8 10 68)) (53 . #s(token name "a" a 8 11 69)) (54 . #s(token lsqbrace "[" "[" 8 12 70)) (55 . #s(token number "5" 5 8 13 71)) (56 . #s(token rsqbrace "]" "]" 8 14 72)) (57 . #s(token rparen ")" ")" 8 15 73)) (58 . #s(token whitespace "\n" "\n" 8 16 74))) -------------------------------------------------------------------------------- /lua-test/lua/examples/assignment-02.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | a = {} 4 | 5 | function f() 6 | return a, 50 7 | end 8 | 9 | f().x, f()["y"], f()["z"] = 1, 2, {} 10 | print(a.x, a.y) 11 | 12 | f()["z"]["a"] = 42 13 | print(f().z.a) 14 | 15 | k = "x" 16 | k, f()[k] = "y", 42 17 | print(k, f().x, f().y) 18 | 19 | function g() 20 | return 1, 2 21 | end 22 | 23 | k = "x" 24 | k, f()[k] = g() 25 | print(k, f().x, f().y) 26 | -------------------------------------------------------------------------------- /lua-test/lua/examples/assignment-02.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\t2\n42\ny\t42\t2\n1\t2\t2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/bitops.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(~1) 4 | print(~.0) 5 | pcall(function() 6 | print(~.5) 7 | end) 8 | 9 | print(1 | 2) 10 | print(1 & 2) 11 | print(2 & 2) 12 | print(1 ~ 5) 13 | print(1 << 1) 14 | print(1 << 8) 15 | print(256 >> 1) 16 | print(256 >> 8) 17 | -------------------------------------------------------------------------------- /lua-test/lua/examples/bitops.stdout.rktd: -------------------------------------------------------------------------------- 1 | "-2\n-1\n3\n0\n2\n4\n2\n256\n128\n1\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/bitops.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "print" print 3 0 12)) (6 . #s(token lparen "(" "(" 3 5 17)) (7 . #s(token op "~" ~ 3 6 18)) (8 . #s(token number "1" 1 3 7 19)) (9 . #s(token rparen ")" ")" 3 8 20)) (10 . #s(token whitespace "\n" "\n" 3 9 21)) (11 . #s(token name "print" print 4 0 22)) (12 . #s(token lparen "(" "(" 4 5 27)) (13 . #s(token op "~" ~ 4 6 28)) (14 . #s(token number ".0" 0.0 4 7 29)) (15 . #s(token rparen ")" ")" 4 9 31)) (16 . #s(token whitespace "\n" "\n" 4 10 32)) (17 . #s(token name "pcall" pcall 5 0 33)) (18 . #s(token lparen "(" "(" 5 5 38)) (19 . #s(token keyword "function" function 5 6 39)) (20 . #s(token lparen "(" "(" 5 14 47)) (21 . #s(token rparen ")" ")" 5 15 48)) (22 . #s(token whitespace "\n " "\n " 5 16 49)) (23 . #s(token name "print" print 6 8 58)) (24 . #s(token lparen "(" "(" 6 13 63)) (25 . #s(token op "~" ~ 6 14 64)) (26 . #s(token number ".5" 0.5 6 15 65)) (27 . #s(token rparen ")" ")" 6 17 67)) (28 . #s(token whitespace "\n" "\n" 6 18 68)) (29 . #s(token keyword "end" end 7 0 69)) (30 . #s(token rparen ")" ")" 7 3 72)) (31 . #s(token whitespace "\n\n" "\n\n" 7 4 73)) (32 . #s(token name "print" print 9 0 75)) (33 . #s(token lparen "(" "(" 9 5 80)) (34 . #s(token number "1" 1 9 6 81)) (35 . #s(token whitespace " " " " 9 7 82)) (36 . #s(token op "|" \| 9 8 83)) (37 . #s(token whitespace " " " " 9 9 84)) (38 . #s(token number "2" 2 9 10 85)) (39 . #s(token rparen ")" ")" 9 11 86)) (40 . #s(token whitespace "\n" "\n" 9 12 87)) (41 . #s(token name "print" print 10 0 88)) (42 . #s(token lparen "(" "(" 10 5 93)) (43 . #s(token number "1" 1 10 6 94)) (44 . #s(token whitespace " " " " 10 7 95)) (45 . #s(token op "&" & 10 8 96)) (46 . #s(token whitespace " " " " 10 9 97)) (47 . #s(token number "2" 2 10 10 98)) (48 . #s(token rparen ")" ")" 10 11 99)) (49 . #s(token whitespace "\n" "\n" 10 12 100)) (50 . #s(token name "print" print 11 0 101)) (51 . #s(token lparen "(" "(" 11 5 106)) (52 . #s(token number "2" 2 11 6 107)) (53 . #s(token whitespace " " " " 11 7 108)) (54 . #s(token op "&" & 11 8 109)) (55 . #s(token whitespace " " " " 11 9 110)) (56 . #s(token number "2" 2 11 10 111)) (57 . #s(token rparen ")" ")" 11 11 112)) (58 . #s(token whitespace "\n" "\n" 11 12 113)) (59 . #s(token name "print" print 12 0 114)) (60 . #s(token lparen "(" "(" 12 5 119)) (61 . #s(token number "1" 1 12 6 120)) (62 . #s(token whitespace " " " " 12 7 121)) (63 . #s(token op "~" ~ 12 8 122)) (64 . #s(token whitespace " " " " 12 9 123)) (65 . #s(token number "5" 5 12 10 124)) (66 . #s(token rparen ")" ")" 12 11 125)) (67 . #s(token whitespace "\n" "\n" 12 12 126)) (68 . #s(token name "print" print 13 0 127)) (69 . #s(token lparen "(" "(" 13 5 132)) (70 . #s(token number "1" 1 13 6 133)) (71 . #s(token whitespace " " " " 13 7 134)) (72 . #s(token op "<<" << 13 8 135)) (73 . #s(token whitespace " " " " 13 10 137)) (74 . #s(token number "1" 1 13 11 138)) (75 . #s(token rparen ")" ")" 13 12 139)) (76 . #s(token whitespace "\n" "\n" 13 13 140)) (77 . #s(token name "print" print 14 0 141)) (78 . #s(token lparen "(" "(" 14 5 146)) (79 . #s(token number "1" 1 14 6 147)) (80 . #s(token whitespace " " " " 14 7 148)) (81 . #s(token op "<<" << 14 8 149)) (82 . #s(token whitespace " " " " 14 10 151)) (83 . #s(token number "8" 8 14 11 152)) (84 . #s(token rparen ")" ")" 14 12 153)) (85 . #s(token whitespace "\n" "\n" 14 13 154)) (86 . #s(token name "print" print 15 0 155)) (87 . #s(token lparen "(" "(" 15 5 160)) (88 . #s(token number "256" 256 15 6 161)) (89 . #s(token whitespace " " " " 15 9 164)) (90 . #s(token op ">>" >> 15 10 165)) (91 . #s(token whitespace " " " " 15 12 167)) (92 . #s(token number "1" 1 15 13 168)) (93 . #s(token rparen ")" ")" 15 14 169)) (94 . #s(token whitespace "\n" "\n" 15 15 170)) (95 . #s(token name "print" print 16 0 171)) (96 . #s(token lparen "(" "(" 16 5 176)) (97 . #s(token number "256" 256 16 6 177)) (98 . #s(token whitespace " " " " 16 9 180)) (99 . #s(token op ">>" >> 16 10 181)) (100 . #s(token whitespace " " " " 16 12 183)) (101 . #s(token number "8" 8 16 13 184)) (102 . #s(token rparen ")" ")" 16 14 185)) (103 . #s(token whitespace "\n" "\n" 16 15 186))) -------------------------------------------------------------------------------- /lua-test/lua/examples/break.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | a = 0 4 | while true do 5 | if a > 10 then 6 | break 7 | end 8 | print(a) 9 | a = a + 1 10 | end 11 | -------------------------------------------------------------------------------- /lua-test/lua/examples/break.stdout.rktd: -------------------------------------------------------------------------------- 1 | "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/break.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "a" a 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token number "0" 0 3 4 16)) (10 . #s(token whitespace "\n" "\n" 3 5 17)) (11 . #s(token keyword "while" while 4 0 18)) (12 . #s(token whitespace " " " " 4 5 23)) (13 . #s(token keyword "true" true 4 6 24)) (14 . #s(token whitespace " " " " 4 10 28)) (15 . #s(token keyword "do" do 4 11 29)) (16 . #s(token whitespace "\n " "\n " 4 13 31)) (17 . #s(token keyword "if" if 5 4 36)) (18 . #s(token whitespace " " " " 5 6 38)) (19 . #s(token name "a" a 5 7 39)) (20 . #s(token whitespace " " " " 5 8 40)) (21 . #s(token op ">" > 5 9 41)) (22 . #s(token whitespace " " " " 5 10 42)) (23 . #s(token number "10" 10 5 11 43)) (24 . #s(token whitespace " " " " 5 13 45)) (25 . #s(token keyword "then" then 5 14 46)) (26 . #s(token whitespace "\n " "\n " 5 18 50)) (27 . #s(token keyword "break" break 6 8 59)) (28 . #s(token whitespace "\n " "\n " 6 13 64)) (29 . #s(token keyword "end" end 7 4 69)) (30 . #s(token whitespace "\n " "\n " 7 7 72)) (31 . #s(token name "print" print 8 4 77)) (32 . #s(token lparen "(" "(" 8 9 82)) (33 . #s(token name "a" a 8 10 83)) (34 . #s(token rparen ")" ")" 8 11 84)) (35 . #s(token whitespace "\n " "\n " 8 12 85)) (36 . #s(token name "a" a 9 4 90)) (37 . #s(token whitespace " " " " 9 5 91)) (38 . #s(token op "=" = 9 6 92)) (39 . #s(token whitespace " " " " 9 7 93)) (40 . #s(token name "a" a 9 8 94)) (41 . #s(token whitespace " " " " 9 9 95)) (42 . #s(token op "+" + 9 10 96)) (43 . #s(token whitespace " " " " 9 11 97)) (44 . #s(token number "1" 1 9 12 98)) (45 . #s(token whitespace "\n" "\n" 9 13 99)) (46 . #s(token keyword "end" end 10 0 100)) (47 . #s(token whitespace "\n" "\n" 10 3 103))) -------------------------------------------------------------------------------- /lua-test/lua/examples/concat.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print("hello" .. " " .. "world!") 4 | 5 | local ConcatDebug = {} 6 | function ConcatDebug.new(str) 7 | o = { str = str } 8 | setmetatable(o, ConcatDebug) 9 | return o 10 | end 11 | function ConcatDebug:__concat(other) 12 | print("concat", self.str, "with", other.str) 13 | return ConcatDebug.new(self.str .. other.str) 14 | end 15 | 16 | local a = ConcatDebug.new('a') 17 | local b = ConcatDebug.new('b') 18 | local c = ConcatDebug.new('c') 19 | print(a .. b .. c) 20 | 21 | print(1 .. 2) 22 | print(1 .. "2" .. 3) 23 | -------------------------------------------------------------------------------- /lua-test/lua/examples/concat.stdout.rktd: -------------------------------------------------------------------------------- 1 | "hello world!\nconcat\tb\twith\tc\nconcat\ta\twith\tbc\n\n12\n123\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/coroutine.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(coroutine.running()) 4 | print(coroutine.isyieldable()) 5 | print(coroutine.status(coroutine.running())) 6 | print(coroutine.resume(coroutine.running())) 7 | print(pcall(function() coroutine.close(coroutine.running()) end)) 8 | 9 | local gen = coroutine.create(function() 10 | for i = 1, 3 do 11 | print("yield", coroutine.yield(i)) 12 | end 13 | return 4, 5 14 | end) 15 | 16 | print(gen) 17 | print("status", coroutine.status(gen)) 18 | print("resume", coroutine.resume(gen)) 19 | print("status", coroutine.status(gen)) 20 | 21 | print("resume", coroutine.resume(gen, 'a', 'b')) 22 | print("status", coroutine.status(gen)) 23 | 24 | print("resume", coroutine.resume(gen, 'b', 'c')) 25 | print("status", coroutine.status(gen)) 26 | 27 | print("resume", coroutine.resume(gen, 'c', 'd')) 28 | print("status", coroutine.status(gen)) 29 | 30 | print("resume", coroutine.resume(gen, 'c', 'd')) 31 | print("close", coroutine.close(gen)) 32 | 33 | local function make_incr(amt) 34 | local i = 0 35 | return coroutine.wrap(function() 36 | while true do 37 | print("yield incr", coroutine.yield(i)) 38 | i = i + amt 39 | end 40 | end) 41 | end 42 | local incr5 = make_incr(5) 43 | print(incr5()) 44 | print(incr5(1)) 45 | print(incr5(2)) 46 | -------------------------------------------------------------------------------- /lua-test/lua/examples/coroutine.stdout.rktd: -------------------------------------------------------------------------------- 1 | "\ttrue\nfalse\nrunning\nfalse\tcoroutine.resume: cannot resume non-suspended coroutine\ntrue\n\nstatus\tsuspended\nresume\ttrue\t1\nstatus\tsuspended\nyield\ta\tb\nresume\ttrue\t2\nstatus\tsuspended\nyield\tb\tc\nresume\ttrue\t3\nstatus\tsuspended\nyield\tc\td\nresume\ttrue\t4\t5\nstatus\tdead\nresume\tfalse\tcoroutine.resume: cannot resume non-suspended coroutine\nclose\ttrue\n0\nyield incr\t1\n5\nyield incr\t2\n10\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/do.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print("outside") 4 | do 5 | print("inside") 6 | end 7 | 8 | do 9 | end 10 | 11 | do 12 | do 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lua-test/lua/examples/do.stdout.rktd: -------------------------------------------------------------------------------- 1 | "outside\ninside\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/do.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "print" print 3 0 12)) (6 . #s(token lparen "(" "(" 3 5 17)) (7 . #s(token string "\"outside\"" #"outside" 3 6 18)) (8 . #s(token rparen ")" ")" 3 15 27)) (9 . #s(token whitespace "\n" "\n" 3 16 28)) (10 . #s(token keyword "do" do 4 0 29)) (11 . #s(token whitespace "\n " "\n " 4 2 31)) (12 . #s(token name "print" print 5 4 36)) (13 . #s(token lparen "(" "(" 5 9 41)) (14 . #s(token string "\"inside\"" #"inside" 5 10 42)) (15 . #s(token rparen ")" ")" 5 18 50)) (16 . #s(token whitespace "\n" "\n" 5 19 51)) (17 . #s(token keyword "end" end 6 0 52)) (18 . #s(token whitespace "\n\n" "\n\n" 6 3 55)) (19 . #s(token keyword "do" do 8 0 57)) (20 . #s(token whitespace "\n" "\n" 8 2 59)) (21 . #s(token keyword "end" end 9 0 60)) (22 . #s(token whitespace "\n\n" "\n\n" 9 3 63)) (23 . #s(token keyword "do" do 11 0 65)) (24 . #s(token whitespace "\n " "\n " 11 2 67)) (25 . #s(token keyword "do" do 12 4 72)) (26 . #s(token whitespace "\n " "\n " 12 6 74)) (27 . #s(token keyword "end" end 13 4 79)) (28 . #s(token whitespace "\n" "\n" 13 7 82)) (29 . #s(token keyword "end" end 14 0 83)) (30 . #s(token whitespace "\n" "\n" 14 3 86))) -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-call.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | Callable = {} 4 | function Callable:__call(...) 5 | print(self.name .. " called with:", ...) 6 | end 7 | 8 | SomeCallable = {name = "SomeCallable"} 9 | setmetatable(SomeCallable, Callable) 10 | SomeCallable(1, 2, "a", "b") 11 | -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-call.stdout.rktd: -------------------------------------------------------------------------------- 1 | "SomeCallable called with:\t1\t2\ta\tb\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-call.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "Callable" Callable 3 0 12)) (6 . #s(token whitespace " " " " 3 8 20)) (7 . #s(token op "=" = 3 9 21)) (8 . #s(token whitespace " " " " 3 10 22)) (9 . #s(token lcubrace "{" "{" 3 11 23)) (10 . #s(token rcubrace "}" "}" 3 12 24)) (11 . #s(token whitespace "\n" "\n" 3 13 25)) (12 . #s(token keyword "function" function 4 0 26)) (13 . #s(token whitespace " " " " 4 8 34)) (14 . #s(token name "Callable" Callable 4 9 35)) (15 . #s(token colon ":" ":" 4 17 43)) (16 . #s(token name "__call" __call 4 18 44)) (17 . #s(token lparen "(" "(" 4 24 50)) (18 . #s(token dotdotdot "..." ... 4 25 51)) (19 . #s(token rparen ")" ")" 4 28 54)) (20 . #s(token whitespace "\n " "\n " 4 29 55)) (21 . #s(token name "print" print 5 4 60)) (22 . #s(token lparen "(" "(" 5 9 65)) (23 . #s(token name "self" self 5 10 66)) (24 . #s(token dot "." |.| 5 14 70)) (25 . #s(token name "name" name 5 15 71)) (26 . #s(token whitespace " " " " 5 19 75)) (27 . #s(token op ".." .. 5 20 76)) (28 . #s(token whitespace " " " " 5 22 78)) (29 . #s(token string "\" called with:\"" #" called with:" 5 23 79)) (30 . #s(token comma "," "," 5 38 94)) (31 . #s(token whitespace " " " " 5 39 95)) (32 . #s(token dotdotdot "..." ... 5 40 96)) (33 . #s(token rparen ")" ")" 5 43 99)) (34 . #s(token whitespace "\n" "\n" 5 44 100)) (35 . #s(token keyword "end" end 6 0 101)) (36 . #s(token whitespace "\n\n" "\n\n" 6 3 104)) (37 . #s(token name "SomeCallable" SomeCallable 8 0 106)) (38 . #s(token whitespace " " " " 8 12 118)) (39 . #s(token op "=" = 8 13 119)) (40 . #s(token whitespace " " " " 8 14 120)) (41 . #s(token lcubrace "{" "{" 8 15 121)) (42 . #s(token name "name" name 8 16 122)) (43 . #s(token whitespace " " " " 8 20 126)) (44 . #s(token op "=" = 8 21 127)) (45 . #s(token whitespace " " " " 8 22 128)) (46 . #s(token string "\"SomeCallable\"" #"SomeCallable" 8 23 129)) (47 . #s(token rcubrace "}" "}" 8 37 143)) (48 . #s(token whitespace "\n" "\n" 8 38 144)) (49 . #s(token name "setmetatable" setmetatable 9 0 145)) (50 . #s(token lparen "(" "(" 9 12 157)) (51 . #s(token name "SomeCallable" SomeCallable 9 13 158)) (52 . #s(token comma "," "," 9 25 170)) (53 . #s(token whitespace " " " " 9 26 171)) (54 . #s(token name "Callable" Callable 9 27 172)) (55 . #s(token rparen ")" ")" 9 35 180)) (56 . #s(token whitespace "\n" "\n" 9 36 181)) (57 . #s(token name "SomeCallable" SomeCallable 10 0 182)) (58 . #s(token lparen "(" "(" 10 12 194)) (59 . #s(token number "1" 1 10 13 195)) (60 . #s(token comma "," "," 10 14 196)) (61 . #s(token whitespace " " " " 10 15 197)) (62 . #s(token number "2" 2 10 16 198)) (63 . #s(token comma "," "," 10 17 199)) (64 . #s(token whitespace " " " " 10 18 200)) (65 . #s(token string "\"a\"" #"a" 10 19 201)) (66 . #s(token comma "," "," 10 22 204)) (67 . #s(token whitespace " " " " 10 23 205)) (68 . #s(token string "\"b\"" #"b" 10 24 206)) (69 . #s(token rparen ")" ")" 10 27 209)) (70 . #s(token whitespace "\n" "\n" 10 28 210))) -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-eq.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | Record = {} 4 | Record.__index = Record 5 | function Record:new(id, o) 6 | o = o or {} 7 | o.id = id 8 | setmetatable(o, Record) 9 | return o 10 | end 11 | 12 | a = Record:new(1, {value = "a"}) 13 | b = Record:new(1, {value = "b"}) 14 | c = Record:new(2, {value = "c"}) 15 | print(a == b) 16 | print(b == a) 17 | print(a == c) 18 | print(c == a) 19 | 20 | function Record:__eq(other) 21 | print("eq called on:", self.value) 22 | return self.id == other.id 23 | end 24 | print(a == b) 25 | print(b == a) 26 | print(a == c) 27 | print(c == a) 28 | -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-eq.stdout.rktd: -------------------------------------------------------------------------------- 1 | "false\nfalse\nfalse\nfalse\neq called on:\ta\ntrue\neq called on:\tb\ntrue\neq called on:\ta\nfalse\neq called on:\tc\nfalse\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-index.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | meta = {} 4 | function meta:__index(k) 5 | return k..self.suffix 6 | end 7 | 8 | loud = {suffix = "!"} 9 | louder = {suffix = "!!"} 10 | loudest = {suffix = "!!!"} 11 | for idx, t in ipairs({ loud, louder, loudest }) do 12 | setmetatable(t, meta) 13 | print(t.a) 14 | end 15 | -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-index.stdout.rktd: -------------------------------------------------------------------------------- 1 | "a!\na!!\na!!!\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-index.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "meta" meta 3 0 12)) (6 . #s(token whitespace " " " " 3 4 16)) (7 . #s(token op "=" = 3 5 17)) (8 . #s(token whitespace " " " " 3 6 18)) (9 . #s(token lcubrace "{" "{" 3 7 19)) (10 . #s(token rcubrace "}" "}" 3 8 20)) (11 . #s(token whitespace "\n" "\n" 3 9 21)) (12 . #s(token keyword "function" function 4 0 22)) (13 . #s(token whitespace " " " " 4 8 30)) (14 . #s(token name "meta" meta 4 9 31)) (15 . #s(token colon ":" ":" 4 13 35)) (16 . #s(token name "__index" __index 4 14 36)) (17 . #s(token lparen "(" "(" 4 21 43)) (18 . #s(token name "k" k 4 22 44)) (19 . #s(token rparen ")" ")" 4 23 45)) (20 . #s(token whitespace "\n " "\n " 4 24 46)) (21 . #s(token keyword "return" return 5 4 51)) (22 . #s(token whitespace " " " " 5 10 57)) (23 . #s(token name "k" k 5 11 58)) (24 . #s(token op ".." .. 5 12 59)) (25 . #s(token name "self" self 5 14 61)) (26 . #s(token dot "." |.| 5 18 65)) (27 . #s(token name "suffix" suffix 5 19 66)) (28 . #s(token whitespace "\n" "\n" 5 25 72)) (29 . #s(token keyword "end" end 6 0 73)) (30 . #s(token whitespace "\n\n" "\n\n" 6 3 76)) (31 . #s(token name "loud" loud 8 0 78)) (32 . #s(token whitespace " " " " 8 4 82)) (33 . #s(token op "=" = 8 5 83)) (34 . #s(token whitespace " " " " 8 6 84)) (35 . #s(token lcubrace "{" "{" 8 7 85)) (36 . #s(token name "suffix" suffix 8 8 86)) (37 . #s(token whitespace " " " " 8 14 92)) (38 . #s(token op "=" = 8 15 93)) (39 . #s(token whitespace " " " " 8 16 94)) (40 . #s(token string "\"!\"" #"!" 8 17 95)) (41 . #s(token rcubrace "}" "}" 8 20 98)) (42 . #s(token whitespace "\n" "\n" 8 21 99)) (43 . #s(token name "louder" louder 9 0 100)) (44 . #s(token whitespace " " " " 9 6 106)) (45 . #s(token op "=" = 9 7 107)) (46 . #s(token whitespace " " " " 9 8 108)) (47 . #s(token lcubrace "{" "{" 9 9 109)) (48 . #s(token name "suffix" suffix 9 10 110)) (49 . #s(token whitespace " " " " 9 16 116)) (50 . #s(token op "=" = 9 17 117)) (51 . #s(token whitespace " " " " 9 18 118)) (52 . #s(token string "\"!!\"" #"!!" 9 19 119)) (53 . #s(token rcubrace "}" "}" 9 23 123)) (54 . #s(token whitespace "\n" "\n" 9 24 124)) (55 . #s(token name "loudest" loudest 10 0 125)) (56 . #s(token whitespace " " " " 10 7 132)) (57 . #s(token op "=" = 10 8 133)) (58 . #s(token whitespace " " " " 10 9 134)) (59 . #s(token lcubrace "{" "{" 10 10 135)) (60 . #s(token name "suffix" suffix 10 11 136)) (61 . #s(token whitespace " " " " 10 17 142)) (62 . #s(token op "=" = 10 18 143)) (63 . #s(token whitespace " " " " 10 19 144)) (64 . #s(token string "\"!!!\"" #"!!!" 10 20 145)) (65 . #s(token rcubrace "}" "}" 10 25 150)) (66 . #s(token whitespace "\n" "\n" 10 26 151)) (67 . #s(token keyword "for" for 11 0 152)) (68 . #s(token whitespace " " " " 11 3 155)) (69 . #s(token name "idx" idx 11 4 156)) (70 . #s(token comma "," "," 11 7 159)) (71 . #s(token whitespace " " " " 11 8 160)) (72 . #s(token name "t" t 11 9 161)) (73 . #s(token whitespace " " " " 11 10 162)) (74 . #s(token keyword "in" in 11 11 163)) (75 . #s(token whitespace " " " " 11 13 165)) (76 . #s(token name "ipairs" ipairs 11 14 166)) (77 . #s(token lparen "(" "(" 11 20 172)) (78 . #s(token lcubrace "{" "{" 11 21 173)) (79 . #s(token whitespace " " " " 11 22 174)) (80 . #s(token name "loud" loud 11 23 175)) (81 . #s(token comma "," "," 11 27 179)) (82 . #s(token whitespace " " " " 11 28 180)) (83 . #s(token name "louder" louder 11 29 181)) (84 . #s(token comma "," "," 11 35 187)) (85 . #s(token whitespace " " " " 11 36 188)) (86 . #s(token name "loudest" loudest 11 37 189)) (87 . #s(token whitespace " " " " 11 44 196)) (88 . #s(token rcubrace "}" "}" 11 45 197)) (89 . #s(token rparen ")" ")" 11 46 198)) (90 . #s(token whitespace " " " " 11 47 199)) (91 . #s(token keyword "do" do 11 48 200)) (92 . #s(token whitespace "\n " "\n " 11 50 202)) (93 . #s(token name "setmetatable" setmetatable 12 4 207)) (94 . #s(token lparen "(" "(" 12 16 219)) (95 . #s(token name "t" t 12 17 220)) (96 . #s(token comma "," "," 12 18 221)) (97 . #s(token whitespace " " " " 12 19 222)) (98 . #s(token name "meta" meta 12 20 223)) (99 . #s(token rparen ")" ")" 12 24 227)) (100 . #s(token whitespace "\n " "\n " 12 25 228)) (101 . #s(token name "print" print 13 4 233)) (102 . #s(token lparen "(" "(" 13 9 238)) (103 . #s(token name "t" t 13 10 239)) (104 . #s(token dot "." |.| 13 11 240)) (105 . #s(token name "a" a 13 12 241)) (106 . #s(token rparen ")" ")" 13 13 242)) (107 . #s(token whitespace "\n" "\n" 13 14 243)) (108 . #s(token keyword "end" end 14 0 244)) (109 . #s(token whitespace "\n" "\n" 14 3 247))) -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-len.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | CustomLength = {} 4 | function CustomLength:__len() 5 | return self.len 6 | end 7 | 8 | o = {len = 5} 9 | setmetatable(o, CustomLength) 10 | print(#o) 11 | -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-len.stdout.rktd: -------------------------------------------------------------------------------- 1 | "5\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-len.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "CustomLength" CustomLength 3 0 12)) (6 . #s(token whitespace " " " " 3 12 24)) (7 . #s(token op "=" = 3 13 25)) (8 . #s(token whitespace " " " " 3 14 26)) (9 . #s(token lcubrace "{" "{" 3 15 27)) (10 . #s(token rcubrace "}" "}" 3 16 28)) (11 . #s(token whitespace "\n" "\n" 3 17 29)) (12 . #s(token keyword "function" function 4 0 30)) (13 . #s(token whitespace " " " " 4 8 38)) (14 . #s(token name "CustomLength" CustomLength 4 9 39)) (15 . #s(token colon ":" ":" 4 21 51)) (16 . #s(token name "__len" __len 4 22 52)) (17 . #s(token lparen "(" "(" 4 27 57)) (18 . #s(token rparen ")" ")" 4 28 58)) (19 . #s(token whitespace "\n " "\n " 4 29 59)) (20 . #s(token keyword "return" return 5 4 64)) (21 . #s(token whitespace " " " " 5 10 70)) (22 . #s(token name "self" self 5 11 71)) (23 . #s(token dot "." |.| 5 15 75)) (24 . #s(token name "len" len 5 16 76)) (25 . #s(token whitespace "\n" "\n" 5 19 79)) (26 . #s(token keyword "end" end 6 0 80)) (27 . #s(token whitespace "\n\n" "\n\n" 6 3 83)) (28 . #s(token name "o" o 8 0 85)) (29 . #s(token whitespace " " " " 8 1 86)) (30 . #s(token op "=" = 8 2 87)) (31 . #s(token whitespace " " " " 8 3 88)) (32 . #s(token lcubrace "{" "{" 8 4 89)) (33 . #s(token name "len" len 8 5 90)) (34 . #s(token whitespace " " " " 8 8 93)) (35 . #s(token op "=" = 8 9 94)) (36 . #s(token whitespace " " " " 8 10 95)) (37 . #s(token number "5" 5 8 11 96)) (38 . #s(token rcubrace "}" "}" 8 12 97)) (39 . #s(token whitespace "\n" "\n" 8 13 98)) (40 . #s(token name "setmetatable" setmetatable 9 0 99)) (41 . #s(token lparen "(" "(" 9 12 111)) (42 . #s(token name "o" o 9 13 112)) (43 . #s(token comma "," "," 9 14 113)) (44 . #s(token whitespace " " " " 9 15 114)) (45 . #s(token name "CustomLength" CustomLength 9 16 115)) (46 . #s(token rparen ")" ")" 9 28 127)) (47 . #s(token whitespace "\n" "\n" 9 29 128)) (48 . #s(token name "print" print 10 0 129)) (49 . #s(token lparen "(" "(" 10 5 134)) (50 . #s(token op "#" |#| 10 6 135)) (51 . #s(token name "o" o 10 7 136)) (52 . #s(token rparen ")" ")" 10 8 137)) (53 . #s(token whitespace "\n" "\n" 10 9 138))) -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-metatable.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | a = {id = "a"} 4 | b = {id = "b"} 5 | c = {id = "c"} 6 | setmetatable(b, a) 7 | setmetatable(c, b) 8 | print(getmetatable(c) == b) 9 | print(getmetatable(b) == a) 10 | b.__metatable = a 11 | print(getmetatable(c) == a) 12 | print(getmetatable(b) == a) 13 | -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-metatable.stdout.rktd: -------------------------------------------------------------------------------- 1 | "true\ntrue\ntrue\ntrue\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-metatable.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "a" a 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token lcubrace "{" "{" 3 4 16)) (10 . #s(token name "id" id 3 5 17)) (11 . #s(token whitespace " " " " 3 7 19)) (12 . #s(token op "=" = 3 8 20)) (13 . #s(token whitespace " " " " 3 9 21)) (14 . #s(token string "\"a\"" #"a" 3 10 22)) (15 . #s(token rcubrace "}" "}" 3 13 25)) (16 . #s(token whitespace "\n" "\n" 3 14 26)) (17 . #s(token name "b" b 4 0 27)) (18 . #s(token whitespace " " " " 4 1 28)) (19 . #s(token op "=" = 4 2 29)) (20 . #s(token whitespace " " " " 4 3 30)) (21 . #s(token lcubrace "{" "{" 4 4 31)) (22 . #s(token name "id" id 4 5 32)) (23 . #s(token whitespace " " " " 4 7 34)) (24 . #s(token op "=" = 4 8 35)) (25 . #s(token whitespace " " " " 4 9 36)) (26 . #s(token string "\"b\"" #"b" 4 10 37)) (27 . #s(token rcubrace "}" "}" 4 13 40)) (28 . #s(token whitespace "\n" "\n" 4 14 41)) (29 . #s(token name "c" c 5 0 42)) (30 . #s(token whitespace " " " " 5 1 43)) (31 . #s(token op "=" = 5 2 44)) (32 . #s(token whitespace " " " " 5 3 45)) (33 . #s(token lcubrace "{" "{" 5 4 46)) (34 . #s(token name "id" id 5 5 47)) (35 . #s(token whitespace " " " " 5 7 49)) (36 . #s(token op "=" = 5 8 50)) (37 . #s(token whitespace " " " " 5 9 51)) (38 . #s(token string "\"c\"" #"c" 5 10 52)) (39 . #s(token rcubrace "}" "}" 5 13 55)) (40 . #s(token whitespace "\n" "\n" 5 14 56)) (41 . #s(token name "setmetatable" setmetatable 6 0 57)) (42 . #s(token lparen "(" "(" 6 12 69)) (43 . #s(token name "b" b 6 13 70)) (44 . #s(token comma "," "," 6 14 71)) (45 . #s(token whitespace " " " " 6 15 72)) (46 . #s(token name "a" a 6 16 73)) (47 . #s(token rparen ")" ")" 6 17 74)) (48 . #s(token whitespace "\n" "\n" 6 18 75)) (49 . #s(token name "setmetatable" setmetatable 7 0 76)) (50 . #s(token lparen "(" "(" 7 12 88)) (51 . #s(token name "c" c 7 13 89)) (52 . #s(token comma "," "," 7 14 90)) (53 . #s(token whitespace " " " " 7 15 91)) (54 . #s(token name "b" b 7 16 92)) (55 . #s(token rparen ")" ")" 7 17 93)) (56 . #s(token whitespace "\n" "\n" 7 18 94)) (57 . #s(token name "print" print 8 0 95)) (58 . #s(token lparen "(" "(" 8 5 100)) (59 . #s(token name "getmetatable" getmetatable 8 6 101)) (60 . #s(token lparen "(" "(" 8 18 113)) (61 . #s(token name "c" c 8 19 114)) (62 . #s(token rparen ")" ")" 8 20 115)) (63 . #s(token whitespace " " " " 8 21 116)) (64 . #s(token op "==" == 8 22 117)) (65 . #s(token whitespace " " " " 8 24 119)) (66 . #s(token name "b" b 8 25 120)) (67 . #s(token rparen ")" ")" 8 26 121)) (68 . #s(token whitespace "\n" "\n" 8 27 122)) (69 . #s(token name "print" print 9 0 123)) (70 . #s(token lparen "(" "(" 9 5 128)) (71 . #s(token name "getmetatable" getmetatable 9 6 129)) (72 . #s(token lparen "(" "(" 9 18 141)) (73 . #s(token name "b" b 9 19 142)) (74 . #s(token rparen ")" ")" 9 20 143)) (75 . #s(token whitespace " " " " 9 21 144)) (76 . #s(token op "==" == 9 22 145)) (77 . #s(token whitespace " " " " 9 24 147)) (78 . #s(token name "a" a 9 25 148)) (79 . #s(token rparen ")" ")" 9 26 149)) (80 . #s(token whitespace "\n" "\n" 9 27 150)) (81 . #s(token name "b" b 10 0 151)) (82 . #s(token dot "." |.| 10 1 152)) (83 . #s(token name "__metatable" __metatable 10 2 153)) (84 . #s(token whitespace " " " " 10 13 164)) (85 . #s(token op "=" = 10 14 165)) (86 . #s(token whitespace " " " " 10 15 166)) (87 . #s(token name "a" a 10 16 167)) (88 . #s(token whitespace "\n" "\n" 10 17 168)) (89 . #s(token name "print" print 11 0 169)) (90 . #s(token lparen "(" "(" 11 5 174)) (91 . #s(token name "getmetatable" getmetatable 11 6 175)) (92 . #s(token lparen "(" "(" 11 18 187)) (93 . #s(token name "c" c 11 19 188)) (94 . #s(token rparen ")" ")" 11 20 189)) (95 . #s(token whitespace " " " " 11 21 190)) (96 . #s(token op "==" == 11 22 191)) (97 . #s(token whitespace " " " " 11 24 193)) (98 . #s(token name "a" a 11 25 194)) (99 . #s(token rparen ")" ")" 11 26 195)) (100 . #s(token whitespace "\n" "\n" 11 27 196)) (101 . #s(token name "print" print 12 0 197)) (102 . #s(token lparen "(" "(" 12 5 202)) (103 . #s(token name "getmetatable" getmetatable 12 6 203)) (104 . #s(token lparen "(" "(" 12 18 215)) (105 . #s(token name "b" b 12 19 216)) (106 . #s(token rparen ")" ")" 12 20 217)) (107 . #s(token whitespace " " " " 12 21 218)) (108 . #s(token op "==" == 12 22 219)) (109 . #s(token whitespace " " " " 12 24 221)) (110 . #s(token name "a" a 12 25 222)) (111 . #s(token rparen ")" ")" 12 26 223)) (112 . #s(token whitespace "\n" "\n" 12 27 224))) -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-newindex.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local Target = {} 4 | local MetaWithTable = {} 5 | MetaWithTable.__newindex = Target 6 | 7 | local t0 = {} 8 | setmetatable(t0, MetaWithTable) 9 | t0.x = 42 10 | print(t0.x) 11 | print(Target.x) 12 | 13 | local MetaWithProc = {} 14 | function MetaWithProc:__newindex(k, v) 15 | print(k, "<-", v) 16 | rawset(self, k, v) 17 | end 18 | 19 | local t1 = {} 20 | setmetatable(t1, MetaWithProc) 21 | t1.foo = 42 22 | print(t1.foo) 23 | -------------------------------------------------------------------------------- /lua-test/lua/examples/dunder-newindex.stdout.rktd: -------------------------------------------------------------------------------- 1 | "nil\n42\nfoo\t<-\t42\n42\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/env.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(a) 4 | print(_ENV) 5 | _ENV["a"] = 42 6 | _ENV["b"] = 123 7 | local a = 1 8 | print(a) 9 | print(b) 10 | 11 | local old_print = print 12 | function my_print(a) 13 | old_print("my_print:" .. a) 14 | end 15 | _ENV["print"] = my_print 16 | print(a) 17 | -------------------------------------------------------------------------------- /lua-test/lua/examples/env.stdout.rktd: -------------------------------------------------------------------------------- 1 | "nil\n
\n1\n123\nmy_print:1\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/env.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "print" print 3 0 12)) (6 . #s(token lparen "(" "(" 3 5 17)) (7 . #s(token name "a" a 3 6 18)) (8 . #s(token rparen ")" ")" 3 7 19)) (9 . #s(token whitespace "\n" "\n" 3 8 20)) (10 . #s(token name "print" print 4 0 21)) (11 . #s(token lparen "(" "(" 4 5 26)) (12 . #s(token name "_ENV" _ENV 4 6 27)) (13 . #s(token rparen ")" ")" 4 10 31)) (14 . #s(token whitespace "\n" "\n" 4 11 32)) (15 . #s(token name "_ENV" _ENV 5 0 33)) (16 . #s(token lsqbrace "[" "[" 5 4 37)) (17 . #s(token string "\"a\"" #"a" 5 5 38)) (18 . #s(token rsqbrace "]" "]" 5 8 41)) (19 . #s(token whitespace " " " " 5 9 42)) (20 . #s(token op "=" = 5 10 43)) (21 . #s(token whitespace " " " " 5 11 44)) (22 . #s(token number "42" 42 5 12 45)) (23 . #s(token whitespace "\n" "\n" 5 14 47)) (24 . #s(token name "_ENV" _ENV 6 0 48)) (25 . #s(token lsqbrace "[" "[" 6 4 52)) (26 . #s(token string "\"b\"" #"b" 6 5 53)) (27 . #s(token rsqbrace "]" "]" 6 8 56)) (28 . #s(token whitespace " " " " 6 9 57)) (29 . #s(token op "=" = 6 10 58)) (30 . #s(token whitespace " " " " 6 11 59)) (31 . #s(token number "123" 123 6 12 60)) (32 . #s(token whitespace "\n" "\n" 6 15 63)) (33 . #s(token keyword "local" local 7 0 64)) (34 . #s(token whitespace " " " " 7 5 69)) (35 . #s(token name "a" a 7 6 70)) (36 . #s(token whitespace " " " " 7 7 71)) (37 . #s(token op "=" = 7 8 72)) (38 . #s(token whitespace " " " " 7 9 73)) (39 . #s(token number "1" 1 7 10 74)) (40 . #s(token whitespace "\n" "\n" 7 11 75)) (41 . #s(token name "print" print 8 0 76)) (42 . #s(token lparen "(" "(" 8 5 81)) (43 . #s(token name "a" a 8 6 82)) (44 . #s(token rparen ")" ")" 8 7 83)) (45 . #s(token whitespace "\n" "\n" 8 8 84)) (46 . #s(token name "print" print 9 0 85)) (47 . #s(token lparen "(" "(" 9 5 90)) (48 . #s(token name "b" b 9 6 91)) (49 . #s(token rparen ")" ")" 9 7 92)) (50 . #s(token whitespace "\n\n" "\n\n" 9 8 93)) (51 . #s(token keyword "local" local 11 0 95)) (52 . #s(token whitespace " " " " 11 5 100)) (53 . #s(token name "old_print" old_print 11 6 101)) (54 . #s(token whitespace " " " " 11 15 110)) (55 . #s(token op "=" = 11 16 111)) (56 . #s(token whitespace " " " " 11 17 112)) (57 . #s(token name "print" print 11 18 113)) (58 . #s(token whitespace "\n" "\n" 11 23 118)) (59 . #s(token keyword "function" function 12 0 119)) (60 . #s(token whitespace " " " " 12 8 127)) (61 . #s(token name "my_print" my_print 12 9 128)) (62 . #s(token lparen "(" "(" 12 17 136)) (63 . #s(token name "a" a 12 18 137)) (64 . #s(token rparen ")" ")" 12 19 138)) (65 . #s(token whitespace "\n " "\n " 12 20 139)) (66 . #s(token name "old_print" old_print 13 4 144)) (67 . #s(token lparen "(" "(" 13 13 153)) (68 . #s(token string "\"my_print:\"" #"my_print:" 13 14 154)) (69 . #s(token whitespace " " " " 13 25 165)) (70 . #s(token op ".." .. 13 26 166)) (71 . #s(token whitespace " " " " 13 28 168)) (72 . #s(token name "a" a 13 29 169)) (73 . #s(token rparen ")" ")" 13 30 170)) (74 . #s(token whitespace "\n" "\n" 13 31 171)) (75 . #s(token keyword "end" end 14 0 172)) (76 . #s(token whitespace "\n" "\n" 14 3 175)) (77 . #s(token name "_ENV" _ENV 15 0 176)) (78 . #s(token lsqbrace "[" "[" 15 4 180)) (79 . #s(token string "\"print\"" #"print" 15 5 181)) (80 . #s(token rsqbrace "]" "]" 15 12 188)) (81 . #s(token whitespace " " " " 15 13 189)) (82 . #s(token op "=" = 15 14 190)) (83 . #s(token whitespace " " " " 15 15 191)) (84 . #s(token name "my_print" my_print 15 16 192)) (85 . #s(token whitespace "\n" "\n" 15 24 200)) (86 . #s(token name "print" print 16 0 201)) (87 . #s(token lparen "(" "(" 16 5 206)) (88 . #s(token name "a" a 16 6 207)) (89 . #s(token rparen ")" ")" 16 7 208)) (90 . #s(token whitespace "\n" "\n" 16 8 209))) -------------------------------------------------------------------------------- /lua-test/lua/examples/error.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | if pcall(function() return 1 end) then 4 | print("ok") 5 | end 6 | 7 | if pcall(function() error(404) end) then 8 | print("fail") 9 | else 10 | print("ok") 11 | end 12 | 13 | local status, err = pcall(function() a = 'a' + 1 end) 14 | print(status) 15 | print(err) 16 | 17 | local status, err = pcall(function() error({code = 1}) end) 18 | print(status) 19 | print(err.code) 20 | 21 | local status, err = pcall(function() error({code = 1}, 2) end) 22 | print(status) 23 | print(err.code) 24 | 25 | local status, err = pcall(function(x) error(x) end, 5) 26 | print(status) 27 | print(err) 28 | -------------------------------------------------------------------------------- /lua-test/lua/examples/error.stdout.rktd: -------------------------------------------------------------------------------- 1 | "ok\nok\nfalse\nstring.__add: bad lhs; expected a numeric string\nfalse\n1\nfalse\n1\nfalse\n5\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/fib.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | function fib(n) 4 | if n < 2 then 5 | return 1 6 | else 7 | return fib(n - 2) + fib(n - 1) 8 | end 9 | end 10 | 11 | print(fib(8)) 12 | -------------------------------------------------------------------------------- /lua-test/lua/examples/fib.stdout.rktd: -------------------------------------------------------------------------------- 1 | "34\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/fib.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token keyword "function" function 3 0 12)) (6 . #s(token whitespace " " " " 3 8 20)) (7 . #s(token name "fib" fib 3 9 21)) (8 . #s(token lparen "(" "(" 3 12 24)) (9 . #s(token name "n" n 3 13 25)) (10 . #s(token rparen ")" ")" 3 14 26)) (11 . #s(token whitespace "\n " "\n " 3 15 27)) (12 . #s(token keyword "if" if 4 4 32)) (13 . #s(token whitespace " " " " 4 6 34)) (14 . #s(token name "n" n 4 7 35)) (15 . #s(token whitespace " " " " 4 8 36)) (16 . #s(token op "<" < 4 9 37)) (17 . #s(token whitespace " " " " 4 10 38)) (18 . #s(token number "2" 2 4 11 39)) (19 . #s(token whitespace " " " " 4 12 40)) (20 . #s(token keyword "then" then 4 13 41)) (21 . #s(token whitespace "\n " "\n " 4 17 45)) (22 . #s(token keyword "return" return 5 8 54)) (23 . #s(token whitespace " " " " 5 14 60)) (24 . #s(token number "1" 1 5 15 61)) (25 . #s(token whitespace "\n " "\n " 5 16 62)) (26 . #s(token keyword "else" else 6 4 67)) (27 . #s(token whitespace "\n " "\n " 6 8 71)) (28 . #s(token keyword "return" return 7 8 80)) (29 . #s(token whitespace " " " " 7 14 86)) (30 . #s(token name "fib" fib 7 15 87)) (31 . #s(token lparen "(" "(" 7 18 90)) (32 . #s(token name "n" n 7 19 91)) (33 . #s(token whitespace " " " " 7 20 92)) (34 . #s(token op "-" - 7 21 93)) (35 . #s(token whitespace " " " " 7 22 94)) (36 . #s(token number "2" 2 7 23 95)) (37 . #s(token rparen ")" ")" 7 24 96)) (38 . #s(token whitespace " " " " 7 25 97)) (39 . #s(token op "+" + 7 26 98)) (40 . #s(token whitespace " " " " 7 27 99)) (41 . #s(token name "fib" fib 7 28 100)) (42 . #s(token lparen "(" "(" 7 31 103)) (43 . #s(token name "n" n 7 32 104)) (44 . #s(token whitespace " " " " 7 33 105)) (45 . #s(token op "-" - 7 34 106)) (46 . #s(token whitespace " " " " 7 35 107)) (47 . #s(token number "1" 1 7 36 108)) (48 . #s(token rparen ")" ")" 7 37 109)) (49 . #s(token whitespace "\n " "\n " 7 38 110)) (50 . #s(token keyword "end" end 8 4 115)) (51 . #s(token whitespace "\n" "\n" 8 7 118)) (52 . #s(token keyword "end" end 9 0 119)) (53 . #s(token whitespace "\n\n" "\n\n" 9 3 122)) (54 . #s(token name "print" print 11 0 124)) (55 . #s(token lparen "(" "(" 11 5 129)) (56 . #s(token name "fib" fib 11 6 130)) (57 . #s(token lparen "(" "(" 11 9 133)) (58 . #s(token number "8" 8 11 10 134)) (59 . #s(token rparen ")" ")" 11 11 135)) (60 . #s(token rparen ")" ")" 11 12 136)) (61 . #s(token whitespace "\n" "\n" 11 13 137))) -------------------------------------------------------------------------------- /lua-test/lua/examples/file.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local path = os.tmpname() 4 | local file = io.output(path) 5 | file:write("hello\n") 6 | file:write("world!") 7 | file:close() 8 | 9 | local file = io.input(path) 10 | for line in file:lines() do 11 | print(line) 12 | end 13 | file:close() 14 | 15 | local file = io.input(path) 16 | for c in file:lines(1) do 17 | print(c) 18 | end 19 | file:close() 20 | 21 | local file = io.input(path) 22 | print(file:seek()) 23 | print(file:seek("set", 6)) 24 | print(file:read("l")) 25 | print(file:seek("end", 6)) 26 | print(file:read("l")) 27 | file:close() 28 | -------------------------------------------------------------------------------- /lua-test/lua/examples/file.stdout.rktd: -------------------------------------------------------------------------------- 1 | "hello\nworld!\nh\ne\nl\nl\no\n\n\nw\no\nr\nl\nd\n!\n0\n6\nworld!\n6\nworld!\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/for-in-closing.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | function closing_iter(proc) 4 | return function(s, c) 5 | if c < 1 then 6 | return 1 7 | end 8 | end, {}, 0, proc 9 | end 10 | 11 | for v in closing_iter(function() print("closed") end) do 12 | print(v) 13 | end 14 | 15 | pcall(function() 16 | for v in closing_iter(function() print("closed") end) do 17 | error("fail") 18 | end 19 | end) 20 | -------------------------------------------------------------------------------- /lua-test/lua/examples/for-in-closing.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\nclosed\nclosed\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/for-in.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | function range_iter(n) 4 | return function(s) 5 | local i = s.current 6 | if i <= n then 7 | s.current = i + 1 8 | return i 9 | end 10 | end, {current = 1} 11 | end 12 | 13 | for i in range_iter(5) do 14 | print(i) 15 | end 16 | 17 | -- https://www.lua.org/pil/7.1.html 18 | function list_iter(t) 19 | local i = 0 20 | local n = #t 21 | return function() 22 | i = i + 1 23 | if i <= n then 24 | return t[i] 25 | end 26 | end 27 | end 28 | 29 | for e in list_iter({}) do 30 | print(e) 31 | end 32 | 33 | for e in list_iter({1, 2, 3, 4}) do 34 | print(e) 35 | end 36 | 37 | function ipairs(t) 38 | return function(t, c) 39 | c = c + 1 40 | if t[c] ~= nil then 41 | return c, t[c] 42 | end 43 | end, t, 0 44 | end 45 | 46 | for i, v in ipairs({"a", "b", "c", "d"}) do 47 | print(i, v) 48 | end 49 | -------------------------------------------------------------------------------- /lua-test/lua/examples/for-in.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\n2\n3\n4\n5\n1\n2\n3\n4\n1\ta\n2\tb\n3\tc\n4\td\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/for.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | for i = 0, 5 do 4 | print(i) 5 | end 6 | 7 | for i = 0, -10, -1 do 8 | print(i) 9 | end 10 | 11 | for i = 0, 10 do 12 | break 13 | print("fail") 14 | end 15 | -------------------------------------------------------------------------------- /lua-test/lua/examples/for.stdout.rktd: -------------------------------------------------------------------------------- 1 | "0\n1\n2\n3\n4\n5\n0\n-1\n-2\n-3\n-4\n-5\n-6\n-7\n-8\n-9\n-10\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/for.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token keyword "for" for 3 0 12)) (6 . #s(token whitespace " " " " 3 3 15)) (7 . #s(token name "i" i 3 4 16)) (8 . #s(token whitespace " " " " 3 5 17)) (9 . #s(token op "=" = 3 6 18)) (10 . #s(token whitespace " " " " 3 7 19)) (11 . #s(token number "0" 0 3 8 20)) (12 . #s(token comma "," "," 3 9 21)) (13 . #s(token whitespace " " " " 3 10 22)) (14 . #s(token number "5" 5 3 11 23)) (15 . #s(token whitespace " " " " 3 12 24)) (16 . #s(token keyword "do" do 3 13 25)) (17 . #s(token whitespace "\n " "\n " 3 15 27)) (18 . #s(token name "print" print 4 4 32)) (19 . #s(token lparen "(" "(" 4 9 37)) (20 . #s(token name "i" i 4 10 38)) (21 . #s(token rparen ")" ")" 4 11 39)) (22 . #s(token whitespace "\n" "\n" 4 12 40)) (23 . #s(token keyword "end" end 5 0 41)) (24 . #s(token whitespace "\n\n" "\n\n" 5 3 44)) (25 . #s(token keyword "for" for 7 0 46)) (26 . #s(token whitespace " " " " 7 3 49)) (27 . #s(token name "i" i 7 4 50)) (28 . #s(token whitespace " " " " 7 5 51)) (29 . #s(token op "=" = 7 6 52)) (30 . #s(token whitespace " " " " 7 7 53)) (31 . #s(token number "0" 0 7 8 54)) (32 . #s(token comma "," "," 7 9 55)) (33 . #s(token whitespace " " " " 7 10 56)) (34 . #s(token op "-" - 7 11 57)) (35 . #s(token number "10" 10 7 12 58)) (36 . #s(token comma "," "," 7 14 60)) (37 . #s(token whitespace " " " " 7 15 61)) (38 . #s(token op "-" - 7 16 62)) (39 . #s(token number "1" 1 7 17 63)) (40 . #s(token whitespace " " " " 7 18 64)) (41 . #s(token keyword "do" do 7 19 65)) (42 . #s(token whitespace "\n " "\n " 7 21 67)) (43 . #s(token name "print" print 8 4 72)) (44 . #s(token lparen "(" "(" 8 9 77)) (45 . #s(token name "i" i 8 10 78)) (46 . #s(token rparen ")" ")" 8 11 79)) (47 . #s(token whitespace "\n" "\n" 8 12 80)) (48 . #s(token keyword "end" end 9 0 81)) (49 . #s(token whitespace "\n\n" "\n\n" 9 3 84)) (50 . #s(token keyword "for" for 11 0 86)) (51 . #s(token whitespace " " " " 11 3 89)) (52 . #s(token name "i" i 11 4 90)) (53 . #s(token whitespace " " " " 11 5 91)) (54 . #s(token op "=" = 11 6 92)) (55 . #s(token whitespace " " " " 11 7 93)) (56 . #s(token number "0" 0 11 8 94)) (57 . #s(token comma "," "," 11 9 95)) (58 . #s(token whitespace " " " " 11 10 96)) (59 . #s(token number "10" 10 11 11 97)) (60 . #s(token whitespace " " " " 11 13 99)) (61 . #s(token keyword "do" do 11 14 100)) (62 . #s(token whitespace "\n " "\n " 11 16 102)) (63 . #s(token keyword "break" break 12 4 107)) (64 . #s(token whitespace "\n " "\n " 12 9 112)) (65 . #s(token name "print" print 13 4 117)) (66 . #s(token lparen "(" "(" 13 9 122)) (67 . #s(token string "\"fail\"" #"fail" 13 10 123)) (68 . #s(token rparen ")" ")" 13 16 129)) (69 . #s(token whitespace "\n" "\n" 13 17 130)) (70 . #s(token keyword "end" end 14 0 131)) (71 . #s(token whitespace "\n" "\n" 14 3 134))) -------------------------------------------------------------------------------- /lua-test/lua/examples/function.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | function add(x, y) 4 | return x + y 5 | end 6 | 7 | print(add(1, 2)) 8 | 9 | function variadic(...) 10 | local a, b = ... 11 | print(a) 12 | print(b) 13 | end 14 | 15 | print("no args:") 16 | variadic() 17 | print("one arg:") 18 | variadic(1) 19 | print("two args:") 20 | variadic(1, 2) 21 | 22 | function variadic_add(...) 23 | local args = {...} 24 | local accum = 0 25 | for i = 1, #args do 26 | accum = accum + args[i] 27 | end 28 | return accum 29 | end 30 | 31 | print(variadic_add(1, 2, 3, 4)) 32 | print(_ENV["variadic_add"]) 33 | 34 | function f() 35 | return 1 36 | end 37 | 38 | function f() 39 | return 2 40 | end 41 | 42 | print(f()) 43 | -------------------------------------------------------------------------------- /lua-test/lua/examples/function.stdout.rktd: -------------------------------------------------------------------------------- 1 | "3\nno args:\nnil\nnil\none arg:\n1\nnil\ntwo args:\n1\n2\n10\n\n2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/functioncall.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print"hello" 4 | 5 | local o = {} 6 | function o.show(v) 7 | print(v) 8 | end 9 | 10 | o.show"hi" 11 | o.show{} 12 | o["show"]"hi" 13 | 14 | local metao = {} 15 | metao.__index = metao 16 | function metao:show(v) 17 | print(self, v) 18 | end 19 | 20 | local w = { inner = o } 21 | setmetatable(w, metao) 22 | 23 | w.inner["show"]"hi" 24 | w:show"meta hi" 25 | w:show{1, 2, 3, w} 26 | -------------------------------------------------------------------------------- /lua-test/lua/examples/functioncall.stdout.rktd: -------------------------------------------------------------------------------- 1 | "hello\nhi\n
\nhi\nhi\n
\tmeta hi\n
\t
\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/goto-01.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | a = 0 4 | 5 | ::a:: 6 | print(a) 7 | a = a + 1 8 | if a < 10 then 9 | goto a 10 | end 11 | -------------------------------------------------------------------------------- /lua-test/lua/examples/goto-01.stdout.rktd: -------------------------------------------------------------------------------- 1 | "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/goto-01.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "a" a 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token number "0" 0 3 4 16)) (10 . #s(token whitespace "\n\n" "\n\n" 3 5 17)) (11 . #s(token coloncolon "::" "::" 5 0 19)) (12 . #s(token name "a" a 5 2 21)) (13 . #s(token coloncolon "::" "::" 5 3 22)) (14 . #s(token whitespace "\n" "\n" 5 5 24)) (15 . #s(token name "print" print 6 0 25)) (16 . #s(token lparen "(" "(" 6 5 30)) (17 . #s(token name "a" a 6 6 31)) (18 . #s(token rparen ")" ")" 6 7 32)) (19 . #s(token whitespace "\n" "\n" 6 8 33)) (20 . #s(token name "a" a 7 0 34)) (21 . #s(token whitespace " " " " 7 1 35)) (22 . #s(token op "=" = 7 2 36)) (23 . #s(token whitespace " " " " 7 3 37)) (24 . #s(token name "a" a 7 4 38)) (25 . #s(token whitespace " " " " 7 5 39)) (26 . #s(token op "+" + 7 6 40)) (27 . #s(token whitespace " " " " 7 7 41)) (28 . #s(token number "1" 1 7 8 42)) (29 . #s(token whitespace "\n" "\n" 7 9 43)) (30 . #s(token keyword "if" if 8 0 44)) (31 . #s(token whitespace " " " " 8 2 46)) (32 . #s(token name "a" a 8 3 47)) (33 . #s(token whitespace " " " " 8 4 48)) (34 . #s(token op "<" < 8 5 49)) (35 . #s(token whitespace " " " " 8 6 50)) (36 . #s(token number "10" 10 8 7 51)) (37 . #s(token whitespace " " " " 8 9 53)) (38 . #s(token keyword "then" then 8 10 54)) (39 . #s(token whitespace "\n " "\n " 8 14 58)) (40 . #s(token keyword "goto" goto 9 4 63)) (41 . #s(token whitespace " " " " 9 8 67)) (42 . #s(token name "a" a 9 9 68)) (43 . #s(token whitespace "\n" "\n" 9 10 69)) (44 . #s(token keyword "end" end 10 0 70)) (45 . #s(token whitespace "\n" "\n" 10 3 73))) -------------------------------------------------------------------------------- /lua-test/lua/examples/if.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | if true then 4 | print("ok") 5 | end 6 | 7 | if false then 8 | print("fail") 9 | end 10 | 11 | if nil then 12 | print("fail") 13 | end 14 | 15 | if nil then 16 | print("fail") 17 | else 18 | print("ok") 19 | end 20 | 21 | if nil or true then 22 | print("ok") 23 | end 24 | 25 | if nil and true then 26 | print("fail") 27 | end 28 | 29 | if true and nil then 30 | print("fail") 31 | end 32 | 33 | if nil then 34 | print("fail") 35 | elseif false then 36 | print("fail") 37 | elseif true then 38 | print("ok") 39 | else 40 | print("fail") 41 | end 42 | -------------------------------------------------------------------------------- /lua-test/lua/examples/if.stdout.rktd: -------------------------------------------------------------------------------- 1 | "ok\nok\nok\nok\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/io-file.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local path = os.tmpname() 4 | local f = io.open(path, "w") 5 | print(f) 6 | f:write("hello, world!") 7 | f:close() 8 | 9 | local inp = io.open(path) 10 | print(inp:read(5)) 11 | print(inp:read(5)) 12 | print(inp:read(5)) 13 | print(inp:read(5)) 14 | inp:close() 15 | 16 | local inp = io.open(path) 17 | print(inp:read("a")) 18 | inp:close() 19 | 20 | local inp = io.open(path) 21 | print(inp:read(100, "a")) 22 | inp:close() 23 | 24 | local inp = io.open(path) 25 | print(inp:read(5, "l", 5)) 26 | inp:close() 27 | 28 | print(os.remove(path)) 29 | -------------------------------------------------------------------------------- /lua-test/lua/examples/io-file.stdout.rktd: -------------------------------------------------------------------------------- 1 | "\nhello\n, wor\nld!\nnil\nhello, world!\nhello, world!\t\nhello\t, world!\tnil\ntrue\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/io.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | io.write("hello!\n") 4 | 5 | local stdout = io.output() 6 | stdout:write("hello", 42, "world!", "\n") 7 | 8 | local stderr = io.output(io.stderr) 9 | stderr:write("hello, errors!\n") 10 | -------------------------------------------------------------------------------- /lua-test/lua/examples/io.stdout.rktd: -------------------------------------------------------------------------------- 1 | "hello!\nhello42world!\nhello, errors!\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/io.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "io" io 3 0 12)) (6 . #s(token dot "." |.| 3 2 14)) (7 . #s(token name "write" write 3 3 15)) (8 . #s(token lparen "(" "(" 3 8 20)) (9 . #s(token string "\"hello!\\n\"" #"hello!\n" 3 9 21)) (10 . #s(token rparen ")" ")" 3 19 31)) (11 . #s(token whitespace "\n\n" "\n\n" 3 20 32)) (12 . #s(token keyword "local" local 5 0 34)) (13 . #s(token whitespace " " " " 5 5 39)) (14 . #s(token name "stdout" stdout 5 6 40)) (15 . #s(token whitespace " " " " 5 12 46)) (16 . #s(token op "=" = 5 13 47)) (17 . #s(token whitespace " " " " 5 14 48)) (18 . #s(token name "io" io 5 15 49)) (19 . #s(token dot "." |.| 5 17 51)) (20 . #s(token name "output" output 5 18 52)) (21 . #s(token lparen "(" "(" 5 24 58)) (22 . #s(token rparen ")" ")" 5 25 59)) (23 . #s(token whitespace "\n" "\n" 5 26 60)) (24 . #s(token name "stdout" stdout 6 0 61)) (25 . #s(token colon ":" ":" 6 6 67)) (26 . #s(token name "write" write 6 7 68)) (27 . #s(token lparen "(" "(" 6 12 73)) (28 . #s(token string "\"hello\"" #"hello" 6 13 74)) (29 . #s(token comma "," "," 6 20 81)) (30 . #s(token whitespace " " " " 6 21 82)) (31 . #s(token number "42" 42 6 22 83)) (32 . #s(token comma "," "," 6 24 85)) (33 . #s(token whitespace " " " " 6 25 86)) (34 . #s(token string "\"world!\"" #"world!" 6 26 87)) (35 . #s(token comma "," "," 6 34 95)) (36 . #s(token whitespace " " " " 6 35 96)) (37 . #s(token string "\"\\n\"" #"\n" 6 36 97)) (38 . #s(token rparen ")" ")" 6 40 101)) (39 . #s(token whitespace "\n\n" "\n\n" 6 41 102)) (40 . #s(token keyword "local" local 8 0 104)) (41 . #s(token whitespace " " " " 8 5 109)) (42 . #s(token name "stderr" stderr 8 6 110)) (43 . #s(token whitespace " " " " 8 12 116)) (44 . #s(token op "=" = 8 13 117)) (45 . #s(token whitespace " " " " 8 14 118)) (46 . #s(token name "io" io 8 15 119)) (47 . #s(token dot "." |.| 8 17 121)) (48 . #s(token name "output" output 8 18 122)) (49 . #s(token lparen "(" "(" 8 24 128)) (50 . #s(token name "io" io 8 25 129)) (51 . #s(token dot "." |.| 8 27 131)) (52 . #s(token name "stderr" stderr 8 28 132)) (53 . #s(token rparen ")" ")" 8 34 138)) (54 . #s(token whitespace "\n" "\n" 8 35 139)) (55 . #s(token name "stderr" stderr 9 0 140)) (56 . #s(token colon ":" ":" 9 6 146)) (57 . #s(token name "write" write 9 7 147)) (58 . #s(token lparen "(" "(" 9 12 152)) (59 . #s(token string "\"hello, errors!\\n\"" #"hello, errors!\n" 9 13 153)) (60 . #s(token rparen ")" ")" 9 31 171)) (61 . #s(token whitespace "\n" "\n" 9 32 172))) -------------------------------------------------------------------------------- /lua-test/lua/examples/ipairs.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | for idx, v in ipairs({1, 2, 3, 4}) do 4 | print(idx, v) 5 | end 6 | 7 | for idx, v in ipairs({a = 1, b = 2}) do 8 | print("fail") 9 | end 10 | 11 | for idx, v in ipairs({1, 2, 3, [5] = 5, a = 6}) do 12 | print(idx, v) 13 | end 14 | 15 | s = {1, 2, 3} 16 | t = {} 17 | setmetatable(t, s) 18 | s.__index = s 19 | for idx, v in ipairs(t) do 20 | print(idx, v) 21 | end 22 | -------------------------------------------------------------------------------- /lua-test/lua/examples/ipairs.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\t1\n2\t2\n3\t3\n4\t4\n1\t1\n2\t2\n3\t3\n1\t1\n2\t2\n3\t3\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/json.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local t = json.decode'{"o": {"a": [1, 2, 3], "b": 42, "c": {}, "d": false}}' 4 | print(t) 5 | print(t.o) 6 | print(t.o.a, t.o.b, t.o.c, t.o.d) 7 | print(json.encode(t)) 8 | -------------------------------------------------------------------------------- /lua-test/lua/examples/json.stdout.rktd: -------------------------------------------------------------------------------- 1 | "
\n
\n
\t42\t
\tfalse\n{\"o\":{\"a\":[1,2,3],\"b\":42,\"c\":{},\"d\":false}}\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/json.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token keyword "local" local 3 0 12)) (6 . #s(token whitespace " " " " 3 5 17)) (7 . #s(token name "t" t 3 6 18)) (8 . #s(token whitespace " " " " 3 7 19)) (9 . #s(token op "=" = 3 8 20)) (10 . #s(token whitespace " " " " 3 9 21)) (11 . #s(token name "json" json 3 10 22)) (12 . #s(token dot "." |.| 3 14 26)) (13 . #s(token name "decode" decode 3 15 27)) (14 . #s(token string "'{\"o\": {\"a\": [1, 2, 3], \"b\": 42, \"c\": {}, \"d\": false}}'" #"{\"o\": {\"a\": [1, 2, 3], \"b\": 42, \"c\": {}, \"d\": false}}" 3 21 33)) (15 . #s(token whitespace "\n" "\n" 3 76 88)) (16 . #s(token name "print" print 4 0 89)) (17 . #s(token lparen "(" "(" 4 5 94)) (18 . #s(token name "t" t 4 6 95)) (19 . #s(token rparen ")" ")" 4 7 96)) (20 . #s(token whitespace "\n" "\n" 4 8 97)) (21 . #s(token name "print" print 5 0 98)) (22 . #s(token lparen "(" "(" 5 5 103)) (23 . #s(token name "t" t 5 6 104)) (24 . #s(token dot "." |.| 5 7 105)) (25 . #s(token name "o" o 5 8 106)) (26 . #s(token rparen ")" ")" 5 9 107)) (27 . #s(token whitespace "\n" "\n" 5 10 108)) (28 . #s(token name "print" print 6 0 109)) (29 . #s(token lparen "(" "(" 6 5 114)) (30 . #s(token name "t" t 6 6 115)) (31 . #s(token dot "." |.| 6 7 116)) (32 . #s(token name "o" o 6 8 117)) (33 . #s(token dot "." |.| 6 9 118)) (34 . #s(token name "a" a 6 10 119)) (35 . #s(token comma "," "," 6 11 120)) (36 . #s(token whitespace " " " " 6 12 121)) (37 . #s(token name "t" t 6 13 122)) (38 . #s(token dot "." |.| 6 14 123)) (39 . #s(token name "o" o 6 15 124)) (40 . #s(token dot "." |.| 6 16 125)) (41 . #s(token name "b" b 6 17 126)) (42 . #s(token comma "," "," 6 18 127)) (43 . #s(token whitespace " " " " 6 19 128)) (44 . #s(token name "t" t 6 20 129)) (45 . #s(token dot "." |.| 6 21 130)) (46 . #s(token name "o" o 6 22 131)) (47 . #s(token dot "." |.| 6 23 132)) (48 . #s(token name "c" c 6 24 133)) (49 . #s(token comma "," "," 6 25 134)) (50 . #s(token whitespace " " " " 6 26 135)) (51 . #s(token name "t" t 6 27 136)) (52 . #s(token dot "." |.| 6 28 137)) (53 . #s(token name "o" o 6 29 138)) (54 . #s(token dot "." |.| 6 30 139)) (55 . #s(token name "d" d 6 31 140)) (56 . #s(token rparen ")" ")" 6 32 141)) (57 . #s(token whitespace "\n" "\n" 6 33 142)) (58 . #s(token name "print" print 7 0 143)) (59 . #s(token lparen "(" "(" 7 5 148)) (60 . #s(token name "json" json 7 6 149)) (61 . #s(token dot "." |.| 7 10 153)) (62 . #s(token name "encode" encode 7 11 154)) (63 . #s(token lparen "(" "(" 7 17 160)) (64 . #s(token name "t" t 7 18 161)) (65 . #s(token rparen ")" ")" 7 19 162)) (66 . #s(token rparen ")" ")" 7 20 163)) (67 . #s(token whitespace "\n" "\n" 7 21 164))) -------------------------------------------------------------------------------- /lua-test/lua/examples/kitchen-sink.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | -- comment 4 | ()[]{} 5 | 010 50 0.5 .1 example 6 | = == + - * / // ^ % & | ~ ~= > < <= >= 7 | 125%2==5 'a'..'b' 8 | 'hi' "hello" "\\" "\"a" 9 | function do for while until end if elseif else then 10 | iffy funny forty whiley 11 | -5 +2.5 +.5 print(.5) 12 | 1+25-3.0/4 13 | 14 | "string with \n escapes \t \r\n" 15 | 16 | 1024.523 5e10 5e-20 .5e10 .5E-5 0.5e00 0.123E1 12.5e+10 17 | 18 | 0x0 0x7F 0x123 0Xdeadbeef 0xDeAdBeEF 19 | -------------------------------------------------------------------------------- /lua-test/lua/examples/local-env.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local old_print = print 4 | local function my_print(...) 5 | print("my_print:" .. ...) 6 | end 7 | 8 | local _ENV = {print = my_print} 9 | print(1, 2) 10 | -------------------------------------------------------------------------------- /lua-test/lua/examples/local-env.stdout.rktd: -------------------------------------------------------------------------------- 1 | "my_print:1\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/local-env.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token keyword "local" local 3 0 12)) (6 . #s(token whitespace " " " " 3 5 17)) (7 . #s(token name "old_print" old_print 3 6 18)) (8 . #s(token whitespace " " " " 3 15 27)) (9 . #s(token op "=" = 3 16 28)) (10 . #s(token whitespace " " " " 3 17 29)) (11 . #s(token name "print" print 3 18 30)) (12 . #s(token whitespace "\n" "\n" 3 23 35)) (13 . #s(token keyword "local" local 4 0 36)) (14 . #s(token whitespace " " " " 4 5 41)) (15 . #s(token keyword "function" function 4 6 42)) (16 . #s(token whitespace " " " " 4 14 50)) (17 . #s(token name "my_print" my_print 4 15 51)) (18 . #s(token lparen "(" "(" 4 23 59)) (19 . #s(token dotdotdot "..." ... 4 24 60)) (20 . #s(token rparen ")" ")" 4 27 63)) (21 . #s(token whitespace "\n " "\n " 4 28 64)) (22 . #s(token name "print" print 5 4 69)) (23 . #s(token lparen "(" "(" 5 9 74)) (24 . #s(token string "\"my_print:\"" #"my_print:" 5 10 75)) (25 . #s(token whitespace " " " " 5 21 86)) (26 . #s(token op ".." .. 5 22 87)) (27 . #s(token whitespace " " " " 5 24 89)) (28 . #s(token dotdotdot "..." ... 5 25 90)) (29 . #s(token rparen ")" ")" 5 28 93)) (30 . #s(token whitespace "\n" "\n" 5 29 94)) (31 . #s(token keyword "end" end 6 0 95)) (32 . #s(token whitespace "\n\n" "\n\n" 6 3 98)) (33 . #s(token keyword "local" local 8 0 100)) (34 . #s(token whitespace " " " " 8 5 105)) (35 . #s(token name "_ENV" _ENV 8 6 106)) (36 . #s(token whitespace " " " " 8 10 110)) (37 . #s(token op "=" = 8 11 111)) (38 . #s(token whitespace " " " " 8 12 112)) (39 . #s(token lcubrace "{" "{" 8 13 113)) (40 . #s(token name "print" print 8 14 114)) (41 . #s(token whitespace " " " " 8 19 119)) (42 . #s(token op "=" = 8 20 120)) (43 . #s(token whitespace " " " " 8 21 121)) (44 . #s(token name "my_print" my_print 8 22 122)) (45 . #s(token rcubrace "}" "}" 8 30 130)) (46 . #s(token whitespace "\n" "\n" 8 31 131)) (47 . #s(token name "print" print 9 0 132)) (48 . #s(token lparen "(" "(" 9 5 137)) (49 . #s(token number "1" 1 9 6 138)) (50 . #s(token comma "," "," 9 7 139)) (51 . #s(token whitespace " " " " 9 8 140)) (52 . #s(token number "2" 2 9 9 141)) (53 . #s(token rparen ")" ")" 9 10 142)) (54 . #s(token whitespace "\n" "\n" 9 11 143))) -------------------------------------------------------------------------------- /lua-test/lua/examples/local-function-name.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local function Foo() 4 | return 1+true 5 | end 6 | 7 | print(pcall(function() Foo() end)) 8 | -------------------------------------------------------------------------------- /lua-test/lua/examples/local-function-name.stdout.rktd: -------------------------------------------------------------------------------- 1 | "false\t+: expected two numbers, received 1 and true\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/local-function-name.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) 2 | (1 . #s(token name "lang" lang 1 1 2)) 3 | (2 . #s(token whitespace " " " " 1 5 6)) 4 | (3 . #s(token name "lua" lua 1 6 7)) 5 | (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) 6 | (5 . #s(token keyword "local" local 3 0 12)) 7 | (6 . #s(token whitespace " " " " 3 5 17)) 8 | (7 . #s(token keyword "function" function 3 6 18)) 9 | (8 . #s(token whitespace " " " " 3 14 26)) 10 | (9 . #s(token name "Foo" Foo 3 15 27)) 11 | (10 . #s(token lparen "(" "(" 3 18 30)) 12 | (11 . #s(token rparen ")" ")" 3 19 31)) 13 | (12 . #s(token whitespace "\n " "\n " 3 20 32)) 14 | (13 . #s(token keyword "return" return 4 4 37)) 15 | (14 . #s(token whitespace " " " " 4 10 43)) 16 | (15 . #s(token number "1" 1 4 11 44)) 17 | (16 . #s(token op "+" + 4 12 45)) 18 | (17 . #s(token keyword "true" true 4 13 46)) 19 | (18 . #s(token whitespace "\n" "\n" 4 17 50)) 20 | (19 . #s(token keyword "end" end 5 0 51)) 21 | (20 . #s(token whitespace "\n\n" "\n\n" 5 3 54)) 22 | (21 . #s(token name "print" print 7 0 56)) 23 | (22 . #s(token lparen "(" "(" 7 5 61)) 24 | (23 . #s(token name "pcall" pcall 7 6 62)) 25 | (24 . #s(token lparen "(" "(" 7 11 67)) 26 | (25 . #s(token keyword "function" function 7 12 68)) 27 | (26 . #s(token lparen "(" "(" 7 20 76)) 28 | (27 . #s(token rparen ")" ")" 7 21 77)) 29 | (28 . #s(token whitespace " " " " 7 22 78)) 30 | (29 . #s(token name "Foo" Foo 7 23 79)) 31 | (30 . #s(token lparen "(" "(" 7 26 82)) 32 | (31 . #s(token rparen ")" ")" 7 27 83)) 33 | (32 . #s(token whitespace " " " " 7 28 84)) 34 | (33 . #s(token keyword "end" end 7 29 85)) 35 | (34 . #s(token rparen ")" ")" 7 32 88)) 36 | (35 . #s(token rparen ")" ")" 7 33 89)) 37 | (36 . #s(token whitespace "\n" "\n" 7 34 90))) 38 | -------------------------------------------------------------------------------- /lua-test/lua/examples/local-function.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local function add(x, y) 4 | return x + y 5 | end 6 | print(add(1, 2)) 7 | 8 | local function fib(n) 9 | if n < 2 then 10 | return 1 11 | else 12 | return fib(n - 2) + fib(n - 1) 13 | end 14 | end 15 | print(fib(8)) 16 | -------------------------------------------------------------------------------- /lua-test/lua/examples/local-function.stdout.rktd: -------------------------------------------------------------------------------- 1 | "3\n34\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/local-function.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token keyword "local" local 3 0 12)) (6 . #s(token whitespace " " " " 3 5 17)) (7 . #s(token keyword "function" function 3 6 18)) (8 . #s(token whitespace " " " " 3 14 26)) (9 . #s(token name "add" add 3 15 27)) (10 . #s(token lparen "(" "(" 3 18 30)) (11 . #s(token name "x" x 3 19 31)) (12 . #s(token comma "," "," 3 20 32)) (13 . #s(token whitespace " " " " 3 21 33)) (14 . #s(token name "y" y 3 22 34)) (15 . #s(token rparen ")" ")" 3 23 35)) (16 . #s(token whitespace "\n " "\n " 3 24 36)) (17 . #s(token keyword "return" return 4 4 41)) (18 . #s(token whitespace " " " " 4 10 47)) (19 . #s(token name "x" x 4 11 48)) (20 . #s(token whitespace " " " " 4 12 49)) (21 . #s(token op "+" + 4 13 50)) (22 . #s(token whitespace " " " " 4 14 51)) (23 . #s(token name "y" y 4 15 52)) (24 . #s(token whitespace "\n" "\n" 4 16 53)) (25 . #s(token keyword "end" end 5 0 54)) (26 . #s(token whitespace "\n" "\n" 5 3 57)) (27 . #s(token name "print" print 6 0 58)) (28 . #s(token lparen "(" "(" 6 5 63)) (29 . #s(token name "add" add 6 6 64)) (30 . #s(token lparen "(" "(" 6 9 67)) (31 . #s(token number "1" 1 6 10 68)) (32 . #s(token comma "," "," 6 11 69)) (33 . #s(token whitespace " " " " 6 12 70)) (34 . #s(token number "2" 2 6 13 71)) (35 . #s(token rparen ")" ")" 6 14 72)) (36 . #s(token rparen ")" ")" 6 15 73)) (37 . #s(token whitespace "\n\n" "\n\n" 6 16 74)) (38 . #s(token keyword "local" local 8 0 76)) (39 . #s(token whitespace " " " " 8 5 81)) (40 . #s(token keyword "function" function 8 6 82)) (41 . #s(token whitespace " " " " 8 14 90)) (42 . #s(token name "fib" fib 8 15 91)) (43 . #s(token lparen "(" "(" 8 18 94)) (44 . #s(token name "n" n 8 19 95)) (45 . #s(token rparen ")" ")" 8 20 96)) (46 . #s(token whitespace "\n " "\n " 8 21 97)) (47 . #s(token keyword "if" if 9 4 102)) (48 . #s(token whitespace " " " " 9 6 104)) (49 . #s(token name "n" n 9 7 105)) (50 . #s(token whitespace " " " " 9 8 106)) (51 . #s(token op "<" < 9 9 107)) (52 . #s(token whitespace " " " " 9 10 108)) (53 . #s(token number "2" 2 9 11 109)) (54 . #s(token whitespace " " " " 9 12 110)) (55 . #s(token keyword "then" then 9 13 111)) (56 . #s(token whitespace "\n " "\n " 9 17 115)) (57 . #s(token keyword "return" return 10 8 124)) (58 . #s(token whitespace " " " " 10 14 130)) (59 . #s(token number "1" 1 10 15 131)) (60 . #s(token whitespace "\n " "\n " 10 16 132)) (61 . #s(token keyword "else" else 11 4 137)) (62 . #s(token whitespace "\n " "\n " 11 8 141)) (63 . #s(token keyword "return" return 12 8 150)) (64 . #s(token whitespace " " " " 12 14 156)) (65 . #s(token name "fib" fib 12 15 157)) (66 . #s(token lparen "(" "(" 12 18 160)) (67 . #s(token name "n" n 12 19 161)) (68 . #s(token whitespace " " " " 12 20 162)) (69 . #s(token op "-" - 12 21 163)) (70 . #s(token whitespace " " " " 12 22 164)) (71 . #s(token number "2" 2 12 23 165)) (72 . #s(token rparen ")" ")" 12 24 166)) (73 . #s(token whitespace " " " " 12 25 167)) (74 . #s(token op "+" + 12 26 168)) (75 . #s(token whitespace " " " " 12 27 169)) (76 . #s(token name "fib" fib 12 28 170)) (77 . #s(token lparen "(" "(" 12 31 173)) (78 . #s(token name "n" n 12 32 174)) (79 . #s(token whitespace " " " " 12 33 175)) (80 . #s(token op "-" - 12 34 176)) (81 . #s(token whitespace " " " " 12 35 177)) (82 . #s(token number "1" 1 12 36 178)) (83 . #s(token rparen ")" ")" 12 37 179)) (84 . #s(token whitespace "\n " "\n " 12 38 180)) (85 . #s(token keyword "end" end 13 4 185)) (86 . #s(token whitespace "\n" "\n" 13 7 188)) (87 . #s(token keyword "end" end 14 0 189)) (88 . #s(token whitespace "\n" "\n" 14 3 192)) (89 . #s(token name "print" print 15 0 193)) (90 . #s(token lparen "(" "(" 15 5 198)) (91 . #s(token name "fib" fib 15 6 199)) (92 . #s(token lparen "(" "(" 15 9 202)) (93 . #s(token number "8" 8 15 10 203)) (94 . #s(token rparen ")" ")" 15 11 204)) (95 . #s(token rparen ")" ")" 15 12 205)) (96 . #s(token whitespace "\n" "\n" 15 13 206))) -------------------------------------------------------------------------------- /lua-test/lua/examples/local.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local x, y 4 | print(x, y) 5 | 6 | local a, b = 0 7 | function b() 8 | print(a) 9 | end 10 | 11 | local a = 5 12 | function c() 13 | print(a) 14 | end 15 | 16 | b() 17 | c() 18 | -------------------------------------------------------------------------------- /lua-test/lua/examples/local.stdout.rktd: -------------------------------------------------------------------------------- 1 | "nil\tnil\n0\n5\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/local.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token keyword "local" local 3 0 12)) (6 . #s(token whitespace " " " " 3 5 17)) (7 . #s(token name "x" x 3 6 18)) (8 . #s(token comma "," "," 3 7 19)) (9 . #s(token whitespace " " " " 3 8 20)) (10 . #s(token name "y" y 3 9 21)) (11 . #s(token whitespace "\n" "\n" 3 10 22)) (12 . #s(token name "print" print 4 0 23)) (13 . #s(token lparen "(" "(" 4 5 28)) (14 . #s(token name "x" x 4 6 29)) (15 . #s(token comma "," "," 4 7 30)) (16 . #s(token whitespace " " " " 4 8 31)) (17 . #s(token name "y" y 4 9 32)) (18 . #s(token rparen ")" ")" 4 10 33)) (19 . #s(token whitespace "\n\n" "\n\n" 4 11 34)) (20 . #s(token keyword "local" local 6 0 36)) (21 . #s(token whitespace " " " " 6 5 41)) (22 . #s(token name "a" a 6 6 42)) (23 . #s(token comma "," "," 6 7 43)) (24 . #s(token whitespace " " " " 6 8 44)) (25 . #s(token name "b" b 6 9 45)) (26 . #s(token whitespace " " " " 6 10 46)) (27 . #s(token op "=" = 6 11 47)) (28 . #s(token whitespace " " " " 6 12 48)) (29 . #s(token number "0" 0 6 13 49)) (30 . #s(token whitespace "\n" "\n" 6 14 50)) (31 . #s(token keyword "function" function 7 0 51)) (32 . #s(token whitespace " " " " 7 8 59)) (33 . #s(token name "b" b 7 9 60)) (34 . #s(token lparen "(" "(" 7 10 61)) (35 . #s(token rparen ")" ")" 7 11 62)) (36 . #s(token whitespace "\n " "\n " 7 12 63)) (37 . #s(token name "print" print 8 2 66)) (38 . #s(token lparen "(" "(" 8 7 71)) (39 . #s(token name "a" a 8 8 72)) (40 . #s(token rparen ")" ")" 8 9 73)) (41 . #s(token whitespace "\n" "\n" 8 10 74)) (42 . #s(token keyword "end" end 9 0 75)) (43 . #s(token whitespace "\n\n" "\n\n" 9 3 78)) (44 . #s(token keyword "local" local 11 0 80)) (45 . #s(token whitespace " " " " 11 5 85)) (46 . #s(token name "a" a 11 6 86)) (47 . #s(token whitespace " " " " 11 7 87)) (48 . #s(token op "=" = 11 8 88)) (49 . #s(token whitespace " " " " 11 9 89)) (50 . #s(token number "5" 5 11 10 90)) (51 . #s(token whitespace "\n" "\n" 11 11 91)) (52 . #s(token keyword "function" function 12 0 92)) (53 . #s(token whitespace " " " " 12 8 100)) (54 . #s(token name "c" c 12 9 101)) (55 . #s(token lparen "(" "(" 12 10 102)) (56 . #s(token rparen ")" ")" 12 11 103)) (57 . #s(token whitespace "\n " "\n " 12 12 104)) (58 . #s(token name "print" print 13 2 107)) (59 . #s(token lparen "(" "(" 13 7 112)) (60 . #s(token name "a" a 13 8 113)) (61 . #s(token rparen ")" ")" 13 9 114)) (62 . #s(token whitespace "\n" "\n" 13 10 115)) (63 . #s(token keyword "end" end 14 0 116)) (64 . #s(token whitespace "\n\n" "\n\n" 14 3 119)) (65 . #s(token name "b" b 16 0 121)) (66 . #s(token lparen "(" "(" 16 1 122)) (67 . #s(token rparen ")" ")" 16 2 123)) (68 . #s(token whitespace "\n" "\n" 16 3 124)) (69 . #s(token name "c" c 17 0 125)) (70 . #s(token lparen "(" "(" 17 1 126)) (71 . #s(token rparen ")" ")" 17 2 127)) (72 . #s(token whitespace "\n" "\n" 17 3 128))) -------------------------------------------------------------------------------- /lua-test/lua/examples/longbrakets.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | --[ not a long bracket comment 4 | 5 | --[[ 6 | long brackets comments 7 | have multiline support 8 | --]] 9 | 10 | --[=[ 11 | [[ nesting ]] 12 | --]=] 13 | 14 | print("after comments") 15 | 16 | print([[test]]) 17 | print([[ 18 | multi-line 19 | test 20 | ]]) 21 | print([==[ 22 | [[nesting]] 23 | ]==]) 24 | print([===[;; test 25 | λ 26 | ]===]) 27 | -------------------------------------------------------------------------------- /lua-test/lua/examples/longbrakets.stdout.rktd: -------------------------------------------------------------------------------- 1 | "after comments\ntest\n multi-line\n test\n\n [[nesting]]\n\n;; test\n λ\n \n" -------------------------------------------------------------------------------- /lua-test/lua/examples/longbrakets.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) 2 | (1 . #s(token name "lang" lang 1 1 2)) 3 | (2 . #s(token whitespace " " " " 1 5 6)) 4 | (3 . #s(token name "lua" lua 1 6 7)) 5 | (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) 6 | (5 7 | . 8 | #s(token 9 | comment 10 | "--[ not a long bracket comment" 11 | "--[ not a long bracket comment" 12 | 3 13 | 0 14 | 12)) 15 | (6 . #s(token whitespace "\n" "\n" 4 0 43)) 16 | (7 17 | . 18 | #s(token 19 | comment 20 | "--[[\n long brackets comments\n have multiline support\n--]]" 21 | "--[[\n long brackets comments\n have multiline support\n--]]" 22 | 5 23 | 0 24 | 44)) 25 | (8 . #s(token whitespace "\n\n" "\n\n" 8 4 107)) 26 | (9 27 | . 28 | #s(token 29 | comment 30 | "--[=[\n [[ nesting ]]\n--]=]" 31 | "--[=[\n [[ nesting ]]\n--]=]" 32 | 10 33 | 0 34 | 109)) 35 | (10 . #s(token whitespace "\n\n" "\n\n" 12 5 138)) 36 | (11 . #s(token name "print" print 14 0 140)) 37 | (12 . #s(token lparen "(" "(" 14 5 145)) 38 | (13 39 | . 40 | #s(token string "\"after comments\"" #"after comments" 14 6 146)) 41 | (14 . #s(token rparen ")" ")" 14 22 162)) 42 | (15 . #s(token whitespace "\n\n" "\n\n" 14 23 163)) 43 | (16 . #s(token name "print" print 16 0 165)) 44 | (17 . #s(token lparen "(" "(" 16 5 170)) 45 | (18 . #s(token string "[[test]]" #"test" 16 6 171)) 46 | (19 . #s(token rparen ")" ")" 16 14 179)) 47 | (20 . #s(token whitespace "\n" "\n" 16 15 180)) 48 | (21 . #s(token name "print" print 17 0 181)) 49 | (22 . #s(token lparen "(" "(" 17 5 186)) 50 | (23 51 | . 52 | #s(token 53 | string 54 | "[[\n multi-line\n test\n]]" 55 | #" multi-line\n test\n" 56 | 17 57 | 6 58 | 187)) 59 | (24 . #s(token rparen ")" ")" 20 2 212)) 60 | (25 . #s(token whitespace "\n" "\n" 20 3 213)) 61 | (26 . #s(token name "print" print 21 0 214)) 62 | (27 . #s(token lparen "(" "(" 21 5 219)) 63 | (28 64 | . 65 | #s(token 66 | string 67 | "[==[\n [[nesting]]\n]==]" 68 | #" [[nesting]]\n" 69 | 21 70 | 6 71 | 220)) 72 | (29 . #s(token rparen ")" ")" 23 4 243)) 73 | (30 . #s(token whitespace "\n" "\n" 23 5 244)) 74 | (31 . #s(token name "print" print 24 0 245)) 75 | (32 . #s(token lparen "(" "(" 24 5 250)) 76 | (33 77 | . 78 | #s(token 79 | string 80 | "[===[;; test\n λ\n ]===]" 81 | #";; test\n \316\273\n " 82 | 24 83 | 6 84 | 251)) 85 | (34 . #s(token rparen ")" ")" 26 7 275)) 86 | (35 . #s(token whitespace "\n" "\n" 26 8 276))) 87 | -------------------------------------------------------------------------------- /lua-test/lua/examples/math.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(math.abs(5)) 4 | print(math.abs(-5)) 5 | print(pcall(function() math.abs("hello") end)) 6 | 7 | print(math.ceil(1.2)) 8 | print(math.floor(1.2)) 9 | 10 | print(pcall(function() math.max() end)) 11 | print(math.max(1)) 12 | print(math.max(1, 2, 5)) 13 | 14 | local Comparable = {} 15 | function Comparable.new(id, value) 16 | local o = { id = id, value = value } 17 | setmetatable(o, Comparable) 18 | return o 19 | end 20 | function Comparable:__lt(other) 21 | return self.value < other.value 22 | end 23 | 24 | local a = Comparable.new('a', 1) 25 | local b = Comparable.new('b', 1) 26 | local c = Comparable.new('c', 5) 27 | print(math.max(a, b, c).id) 28 | 29 | print(math.min(5, 2, -25)) 30 | print(math.min(a, b, c).id) 31 | 32 | print(math.sqrt(4)) 33 | print(math.sqrt(-1)) 34 | 35 | print(math.type(1)) 36 | print(math.type(1.0)) 37 | print(math.type(1.5)) 38 | print(math.type(false)) 39 | -------------------------------------------------------------------------------- /lua-test/lua/examples/math.stdout.rktd: -------------------------------------------------------------------------------- 1 | "5\n5\nfalse\tmath.abs: bad argument #1; not a number\n2.0\n1.0\nfalse\tmath.max: bad argument #1 to max (value expected)\n1\n5\nc\n-25\na\n2\n0+1i\ninteger\ninteger\nfloat\nnil\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/methods.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | -- https://www.lua.org/pil/16.html 4 | Account = {balance = 0} 5 | function Account:withdraw(amount) 6 | self.balance = self.balance - amount 7 | end 8 | function Account:deposit(amount) 9 | self.balance = self.balance + amount 10 | end 11 | 12 | local a = Account 13 | a:withdraw(5) 14 | print(a.balance) 15 | a:deposit(25) 16 | print(a.balance) 17 | 18 | -- https://www.lua.org/pil/16.1.html 19 | function Account:new(o) 20 | o = o or {} 21 | setmetatable(o, self) 22 | self.__index = self 23 | return o 24 | end 25 | 26 | local a = Account:new({balance = 0}) 27 | print(a) 28 | a:deposit(50) 29 | a:withdraw(10) 30 | print(a.balance) 31 | 32 | local b = Account:new({balance = 10}) 33 | b:withdraw(10) 34 | print("b:", b.balance) 35 | print("a:", a.balance) 36 | 37 | -- https://www.lua.org/pil/16.2.html 38 | SpecialAccount = Account:new() 39 | function SpecialAccount:withdraw(amount) 40 | if amount - self.balance >= self:getLimit() then 41 | error("insufficient funds") 42 | end 43 | self.balance = self.balance - amount 44 | end 45 | function SpecialAccount:getLimit() 46 | return self.limit or 0 47 | end 48 | 49 | local c = SpecialAccount:new({ balance = 50, limit = 100}) 50 | print("c:", c) 51 | print(c:getLimit()) 52 | c:withdraw(50) 53 | print(c.balance) 54 | local status, err = pcall(function() c:withdraw(150) end) 55 | print(status, err) 56 | -------------------------------------------------------------------------------- /lua-test/lua/examples/methods.stdout.rktd: -------------------------------------------------------------------------------- 1 | "-5\n20\n
\n40\nb:\t0\na:\t40\nc:\t
\n100\n0\nfalse\tinsufficient funds\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/next.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | idx, v = next({}) 4 | print(idx, v) 5 | 6 | t = {1, 2, 3} 7 | idx, v = next(t) 8 | print(idx, v) 9 | 10 | idx, v = next(t, idx) 11 | print(idx, v) 12 | 13 | idx, v = next(t, idx) 14 | print(idx, v) 15 | 16 | idx, v = next(t, idx) 17 | print(idx, v) 18 | 19 | local _, err = pcall(function() 20 | idx, v = next(t, 50) 21 | print(idx, v) 22 | end) 23 | print(err) 24 | 25 | -- NOTE: Order not guaranteed so test may be flaky. 26 | local t = {a = 1, b = 2, c = 3} 27 | local idx, k = next(t) 28 | repeat 29 | print(idx, k) 30 | idx, k = next(t, idx) 31 | until not idx 32 | -------------------------------------------------------------------------------- /lua-test/lua/examples/next.stdout.rktd: -------------------------------------------------------------------------------- 1 | "nil\tnil\n1\t1\n2\t2\n3\t3\nnil\tnil\nnext: invalid key 50\na\t1\nc\t3\nb\t2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/npe.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(pcall(function() idontexist(1, 2, 3) end)) 4 | 5 | local t = {} 6 | function t.c() 7 | return d(42) 8 | end 9 | 10 | function a() 11 | return b() 12 | end 13 | 14 | function b() 15 | return t.c() 16 | end 17 | 18 | print(pcall(function() a() end)) 19 | 20 | function loop() 21 | local function go(n) 22 | if n > 1000 then 23 | t.c() 24 | else 25 | go(n + 1) 26 | end 27 | end 28 | go(1) 29 | end 30 | 31 | print(pcall(function() loop() end)) 32 | 33 | print(pcall(function() t:d() end)) 34 | -------------------------------------------------------------------------------- /lua-test/lua/examples/npe.stdout.rktd: -------------------------------------------------------------------------------- 1 | "false\tattempt to call a nil value\n call args: \n 1\n 2\n 3\n call stack:\n .../examples/npe.lua:3:12: function\nfalse\tattempt to call a nil value\n call args: \n 42\n call stack:\n .../examples/npe.lua:6:0: t.c\n .../examples/npe.lua:14:0: b\n .../examples/npe.lua:10:0: a\n .../examples/npe.lua:18:12: function\nfalse\tattempt to call a nil value\n call args: \n 42\n call stack:\n .../examples/npe.lua:6:0: t.c\n .../examples/npe.lua:21:4: go\n [repeated 1001 times]\n .../examples/npe.lua:20:0: loop\n .../examples/npe.lua:31:12: function\nfalse\tattempt to call a nil value\n call args: \n
\n call stack:\n .../examples/npe.lua:33:12: function\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/os.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(math.type(os.clock())) 4 | print(math.type(os.time())) 5 | print(os.time({ year = 2023, month = 5, day = 29, isutc = true })) 6 | print(os.time({ year = 2023, month = 5, day = 29, hour = 5, minute = 10, second = 25, isutc = true })) 7 | print(pcall(function() os.time({ year = 2023, month = 5, day = 29, minute = -15 }) end)) 8 | 9 | local timestamp = os.time({ year = 2023, month = 5, day = 29, hour = 5, minute = 10, second = 25, isutc = true}) 10 | local date = os.date("!*t", timestamp) 11 | for k, v in pairs(date) do 12 | print(k, v) 13 | end 14 | print(os.date("!", timestamp)) 15 | -------------------------------------------------------------------------------- /lua-test/lua/examples/os.stdout.rktd: -------------------------------------------------------------------------------- 1 | "integer\ninteger\n1685361600\n1685336400\nfalse\tos.time: negative fields are not supported\nyear\t2023\nmonth\t5\nday\t29\nsec\t0\nyday\t148\nisdst\tfalse\nmin\t0\nhour\t5\nwday\t1\n2023-05-29T05:00:00\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/pairs-meta.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | a = {} 4 | a.__index = a 5 | function a:__pairs() 6 | print("__pairs") 7 | return next, self, nil 8 | end 9 | 10 | t = {1, 2, 3} 11 | setmetatable(t, a) 12 | for k, v in pairs(t) do 13 | print(k, v) 14 | end 15 | -------------------------------------------------------------------------------- /lua-test/lua/examples/pairs-meta.stdout.rktd: -------------------------------------------------------------------------------- 1 | "__pairs\n1\t1\n2\t2\n3\t3\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/pairs-meta.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "a" a 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token lcubrace "{" "{" 3 4 16)) (10 . #s(token rcubrace "}" "}" 3 5 17)) (11 . #s(token whitespace "\n" "\n" 3 6 18)) (12 . #s(token name "a" a 4 0 19)) (13 . #s(token dot "." |.| 4 1 20)) (14 . #s(token name "__index" __index 4 2 21)) (15 . #s(token whitespace " " " " 4 9 28)) (16 . #s(token op "=" = 4 10 29)) (17 . #s(token whitespace " " " " 4 11 30)) (18 . #s(token name "a" a 4 12 31)) (19 . #s(token whitespace "\n" "\n" 4 13 32)) (20 . #s(token keyword "function" function 5 0 33)) (21 . #s(token whitespace " " " " 5 8 41)) (22 . #s(token name "a" a 5 9 42)) (23 . #s(token colon ":" ":" 5 10 43)) (24 . #s(token name "__pairs" __pairs 5 11 44)) (25 . #s(token lparen "(" "(" 5 18 51)) (26 . #s(token rparen ")" ")" 5 19 52)) (27 . #s(token whitespace "\n " "\n " 5 20 53)) (28 . #s(token name "print" print 6 4 58)) (29 . #s(token lparen "(" "(" 6 9 63)) (30 . #s(token string "\"__pairs\"" #"__pairs" 6 10 64)) (31 . #s(token rparen ")" ")" 6 19 73)) (32 . #s(token whitespace "\n " "\n " 6 20 74)) (33 . #s(token keyword "return" return 7 4 79)) (34 . #s(token whitespace " " " " 7 10 85)) (35 . #s(token name "next" next 7 11 86)) (36 . #s(token comma "," "," 7 15 90)) (37 . #s(token whitespace " " " " 7 16 91)) (38 . #s(token name "self" self 7 17 92)) (39 . #s(token comma "," "," 7 21 96)) (40 . #s(token whitespace " " " " 7 22 97)) (41 . #s(token keyword "nil" nil 7 23 98)) (42 . #s(token whitespace "\n" "\n" 7 26 101)) (43 . #s(token keyword "end" end 8 0 102)) (44 . #s(token whitespace "\n\n" "\n\n" 8 3 105)) (45 . #s(token name "t" t 10 0 107)) (46 . #s(token whitespace " " " " 10 1 108)) (47 . #s(token op "=" = 10 2 109)) (48 . #s(token whitespace " " " " 10 3 110)) (49 . #s(token lcubrace "{" "{" 10 4 111)) (50 . #s(token number "1" 1 10 5 112)) (51 . #s(token comma "," "," 10 6 113)) (52 . #s(token whitespace " " " " 10 7 114)) (53 . #s(token number "2" 2 10 8 115)) (54 . #s(token comma "," "," 10 9 116)) (55 . #s(token whitespace " " " " 10 10 117)) (56 . #s(token number "3" 3 10 11 118)) (57 . #s(token rcubrace "}" "}" 10 12 119)) (58 . #s(token whitespace "\n" "\n" 10 13 120)) (59 . #s(token name "setmetatable" setmetatable 11 0 121)) (60 . #s(token lparen "(" "(" 11 12 133)) (61 . #s(token name "t" t 11 13 134)) (62 . #s(token comma "," "," 11 14 135)) (63 . #s(token whitespace " " " " 11 15 136)) (64 . #s(token name "a" a 11 16 137)) (65 . #s(token rparen ")" ")" 11 17 138)) (66 . #s(token whitespace "\n" "\n" 11 18 139)) (67 . #s(token keyword "for" for 12 0 140)) (68 . #s(token whitespace " " " " 12 3 143)) (69 . #s(token name "k" k 12 4 144)) (70 . #s(token comma "," "," 12 5 145)) (71 . #s(token whitespace " " " " 12 6 146)) (72 . #s(token name "v" v 12 7 147)) (73 . #s(token whitespace " " " " 12 8 148)) (74 . #s(token keyword "in" in 12 9 149)) (75 . #s(token whitespace " " " " 12 11 151)) (76 . #s(token name "pairs" pairs 12 12 152)) (77 . #s(token lparen "(" "(" 12 17 157)) (78 . #s(token name "t" t 12 18 158)) (79 . #s(token rparen ")" ")" 12 19 159)) (80 . #s(token whitespace " " " " 12 20 160)) (81 . #s(token keyword "do" do 12 21 161)) (82 . #s(token whitespace "\n " "\n " 12 23 163)) (83 . #s(token name "print" print 13 4 168)) (84 . #s(token lparen "(" "(" 13 9 173)) (85 . #s(token name "k" k 13 10 174)) (86 . #s(token comma "," "," 13 11 175)) (87 . #s(token whitespace " " " " 13 12 176)) (88 . #s(token name "v" v 13 13 177)) (89 . #s(token rparen ")" ")" 13 14 178)) (90 . #s(token whitespace "\n" "\n" 13 15 179)) (91 . #s(token keyword "end" end 14 0 180)) (92 . #s(token whitespace "\n" "\n" 14 3 183))) -------------------------------------------------------------------------------- /lua-test/lua/examples/pairs.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | p, t, idx = pairs({1, 2, 3}) 4 | print(p, t, idx) 5 | idx, v = p(t, idx) 6 | print(p, t, idx) 7 | idx, v = p(t, idx) 8 | print(p, t, idx) 9 | idx, v = p(t, idx) 10 | print(p, t, idx) 11 | idx, v = p(t, idx) 12 | print(p, t, idx) 13 | 14 | for k, v in pairs({a = 1, b = 2, c = 3}) do 15 | print(k, v) 16 | end 17 | -------------------------------------------------------------------------------- /lua-test/lua/examples/pairs.stdout.rktd: -------------------------------------------------------------------------------- 1 | "\t
\tnil\n\t
\t1\n\t
\t2\n\t
\t3\n\t
\tnil\na\t1\nc\t3\nb\t2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/parens.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local function f(x) 4 | return x ^ 2 5 | end 6 | 7 | print((nil or f)(4)) 8 | 9 | local o = {} 10 | function o:ok(v) 11 | return v == "abc" 12 | end 13 | print((true and (nil or o):ok("abc"))) 14 | print((true and (nil or o):ok("abc")) and false) 15 | 16 | local o = {} 17 | function o:id(v, ...) 18 | if v ~= nil then 19 | return v, o:id(...) 20 | end 21 | end 22 | print(o:id(1, ((o.id)(o) or true))) 23 | print((function() return false end)(1) or (function(x) return x == 2 end)(2)) 24 | -------------------------------------------------------------------------------- /lua-test/lua/examples/parens.stdout.rktd: -------------------------------------------------------------------------------- 1 | "16\ntrue\nfalse\n1\ttrue\ntrue\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/repeat.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | i = 0 4 | repeat 5 | print(i) 6 | i = i + 1 7 | until i >= 10 8 | 9 | i = 0 10 | repeat 11 | print(i) 12 | break 13 | until false 14 | 15 | ok = true 16 | repeat 17 | if not ok then 18 | print("fail") 19 | end 20 | ok = nil 21 | until not ok 22 | -------------------------------------------------------------------------------- /lua-test/lua/examples/repeat.stdout.rktd: -------------------------------------------------------------------------------- 1 | "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/repeat.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "i" i 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token number "0" 0 3 4 16)) (10 . #s(token whitespace "\n" "\n" 3 5 17)) (11 . #s(token keyword "repeat" repeat 4 0 18)) (12 . #s(token whitespace "\n " "\n " 4 6 24)) (13 . #s(token name "print" print 5 4 29)) (14 . #s(token lparen "(" "(" 5 9 34)) (15 . #s(token name "i" i 5 10 35)) (16 . #s(token rparen ")" ")" 5 11 36)) (17 . #s(token whitespace "\n " "\n " 5 12 37)) (18 . #s(token name "i" i 6 4 42)) (19 . #s(token whitespace " " " " 6 5 43)) (20 . #s(token op "=" = 6 6 44)) (21 . #s(token whitespace " " " " 6 7 45)) (22 . #s(token name "i" i 6 8 46)) (23 . #s(token whitespace " " " " 6 9 47)) (24 . #s(token op "+" + 6 10 48)) (25 . #s(token whitespace " " " " 6 11 49)) (26 . #s(token number "1" 1 6 12 50)) (27 . #s(token whitespace "\n" "\n" 6 13 51)) (28 . #s(token keyword "until" until 7 0 52)) (29 . #s(token whitespace " " " " 7 5 57)) (30 . #s(token name "i" i 7 6 58)) (31 . #s(token whitespace " " " " 7 7 59)) (32 . #s(token op ">=" >= 7 8 60)) (33 . #s(token whitespace " " " " 7 10 62)) (34 . #s(token number "10" 10 7 11 63)) (35 . #s(token whitespace "\n\n" "\n\n" 7 13 65)) (36 . #s(token name "i" i 9 0 67)) (37 . #s(token whitespace " " " " 9 1 68)) (38 . #s(token op "=" = 9 2 69)) (39 . #s(token whitespace " " " " 9 3 70)) (40 . #s(token number "0" 0 9 4 71)) (41 . #s(token whitespace "\n" "\n" 9 5 72)) (42 . #s(token keyword "repeat" repeat 10 0 73)) (43 . #s(token whitespace "\n " "\n " 10 6 79)) (44 . #s(token name "print" print 11 4 84)) (45 . #s(token lparen "(" "(" 11 9 89)) (46 . #s(token name "i" i 11 10 90)) (47 . #s(token rparen ")" ")" 11 11 91)) (48 . #s(token whitespace "\n " "\n " 11 12 92)) (49 . #s(token keyword "break" break 12 4 97)) (50 . #s(token whitespace "\n" "\n" 12 9 102)) (51 . #s(token keyword "until" until 13 0 103)) (52 . #s(token whitespace " " " " 13 5 108)) (53 . #s(token keyword "false" false 13 6 109)) (54 . #s(token whitespace "\n\n" "\n\n" 13 11 114)) (55 . #s(token name "ok" ok 15 0 116)) (56 . #s(token whitespace " " " " 15 2 118)) (57 . #s(token op "=" = 15 3 119)) (58 . #s(token whitespace " " " " 15 4 120)) (59 . #s(token keyword "true" true 15 5 121)) (60 . #s(token whitespace "\n" "\n" 15 9 125)) (61 . #s(token keyword "repeat" repeat 16 0 126)) (62 . #s(token whitespace "\n " "\n " 16 6 132)) (63 . #s(token keyword "if" if 17 4 137)) (64 . #s(token whitespace " " " " 17 6 139)) (65 . #s(token op "not" not 17 7 140)) (66 . #s(token whitespace " " " " 17 10 143)) (67 . #s(token name "ok" ok 17 11 144)) (68 . #s(token whitespace " " " " 17 13 146)) (69 . #s(token keyword "then" then 17 14 147)) (70 . #s(token whitespace "\n " "\n " 17 18 151)) (71 . #s(token name "print" print 18 8 160)) (72 . #s(token lparen "(" "(" 18 13 165)) (73 . #s(token string "\"fail\"" #"fail" 18 14 166)) (74 . #s(token rparen ")" ")" 18 20 172)) (75 . #s(token whitespace "\n " "\n " 18 21 173)) (76 . #s(token keyword "end" end 19 4 178)) (77 . #s(token whitespace "\n " "\n " 19 7 181)) (78 . #s(token name "ok" ok 20 4 186)) (79 . #s(token whitespace " " " " 20 6 188)) (80 . #s(token op "=" = 20 7 189)) (81 . #s(token whitespace " " " " 20 8 190)) (82 . #s(token keyword "nil" nil 20 9 191)) (83 . #s(token whitespace "\n" "\n" 20 12 194)) (84 . #s(token keyword "until" until 21 0 195)) (85 . #s(token whitespace " " " " 21 5 200)) (86 . #s(token op "not" not 21 6 201)) (87 . #s(token whitespace " " " " 21 9 204)) (88 . #s(token name "ok" ok 21 10 205)) (89 . #s(token whitespace "\n" "\n" 21 12 207))) -------------------------------------------------------------------------------- /lua-test/lua/examples/string-format.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(string.format("%d", 123456)) 4 | print(string.format("%q", true)) 5 | print(string.format("%q", false)) 6 | print(string.format("%q", nil)) 7 | print(pcall(function() string.format("%q", {}) end)) 8 | print(string.format("%q %q", 1, 1.23)) 9 | print(string.format("%q", "hello, world!")) 10 | print(string.format("%q", [==[ 11 | hello, "world!" 12 | ]==])) 13 | print(string.format("%80q", 1.23)) 14 | -------------------------------------------------------------------------------- /lua-test/lua/examples/string-format.stdout.rktd: -------------------------------------------------------------------------------- 1 | "123456\ntrue\nfalse\nnil\nfalse\tstring.format: value has no literal form\n call stack:\n ...string-format.lua:7:12: function\n1 0x1.3ae148p+00\n\"hello, world!\"\n\" hello, \\\"world!\\\"\\\n\"\n 0x1.3ae148p+00\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/string.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local s = "hello, world!" 4 | print(string.sub(s, 1)) 5 | print(string.sub(s, 8)) 6 | print(string.sub(s, -1)) 7 | print(string.sub(s, -1, -3)) 8 | print(string.sub(s, 1, -3)) 9 | 10 | print(string.byte("hello")) 11 | print(string.byte("hello", 1, -1)) 12 | 13 | print(pcall(function() string.dump(function() end) end)) 14 | print(string.upper("hello!! 123")) 15 | print(string.upper("HELLO!! 123")) 16 | print(string.lower("HELLO!! 123")) 17 | 18 | print(string.reverse("")) 19 | print(string.reverse("1")) 20 | print(string.reverse("12345")) 21 | 22 | print(string.rep("a", 0)) 23 | print(string.rep("a", 1)) 24 | print(string.rep("a", 4)) 25 | print(string.rep("a", 4, " ")) 26 | print(string.rep("abc", 4, ", ")) 27 | 28 | print(getmetatable("")) 29 | print("1" + 2 + "3") 30 | print(1 + "2" + "3") 31 | print("1" * 5) 32 | 33 | local s = "abc" 34 | print(s:reverse()) 35 | 36 | print(string.format("%08.02f", 1.2345)) 37 | -------------------------------------------------------------------------------- /lua-test/lua/examples/string.stdout.rktd: -------------------------------------------------------------------------------- 1 | "hello, world!\nworld!\n!\n\nhello, worl\n104\n104\t101\t108\t108\t111\nfalse\tstring.dump: not implemented\nHELLO!! 123\nHELLO!! 123\nhello!! 123\n\n1\n54321\n\na\naaaa\na a a a\nabc, abc, abc, abc\n
\n6\n6\n5\ncba\n00001.23\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/subscript.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | t = {"hello"} 4 | print(t[1]) 5 | t[1] = "goodbye" 6 | print(t[1]) 7 | 8 | local k = "hello" 9 | local t = {[k] = "goodbye"} 10 | print(t["hello"]) 11 | print(t["goodbye"]) 12 | -------------------------------------------------------------------------------- /lua-test/lua/examples/subscript.stdout.rktd: -------------------------------------------------------------------------------- 1 | "hello\ngoodbye\ngoodbye\nnil\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/subscript.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "t" t 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token lcubrace "{" "{" 3 4 16)) (10 . #s(token string "\"hello\"" #"hello" 3 5 17)) (11 . #s(token rcubrace "}" "}" 3 12 24)) (12 . #s(token whitespace "\n" "\n" 3 13 25)) (13 . #s(token name "print" print 4 0 26)) (14 . #s(token lparen "(" "(" 4 5 31)) (15 . #s(token name "t" t 4 6 32)) (16 . #s(token lsqbrace "[" "[" 4 7 33)) (17 . #s(token number "1" 1 4 8 34)) (18 . #s(token rsqbrace "]" "]" 4 9 35)) (19 . #s(token rparen ")" ")" 4 10 36)) (20 . #s(token whitespace "\n" "\n" 4 11 37)) (21 . #s(token name "t" t 5 0 38)) (22 . #s(token lsqbrace "[" "[" 5 1 39)) (23 . #s(token number "1" 1 5 2 40)) (24 . #s(token rsqbrace "]" "]" 5 3 41)) (25 . #s(token whitespace " " " " 5 4 42)) (26 . #s(token op "=" = 5 5 43)) (27 . #s(token whitespace " " " " 5 6 44)) (28 . #s(token string "\"goodbye\"" #"goodbye" 5 7 45)) (29 . #s(token whitespace "\n" "\n" 5 16 54)) (30 . #s(token name "print" print 6 0 55)) (31 . #s(token lparen "(" "(" 6 5 60)) (32 . #s(token name "t" t 6 6 61)) (33 . #s(token lsqbrace "[" "[" 6 7 62)) (34 . #s(token number "1" 1 6 8 63)) (35 . #s(token rsqbrace "]" "]" 6 9 64)) (36 . #s(token rparen ")" ")" 6 10 65)) (37 . #s(token whitespace "\n\n" "\n\n" 6 11 66)) (38 . #s(token keyword "local" local 8 0 68)) (39 . #s(token whitespace " " " " 8 5 73)) (40 . #s(token name "k" k 8 6 74)) (41 . #s(token whitespace " " " " 8 7 75)) (42 . #s(token op "=" = 8 8 76)) (43 . #s(token whitespace " " " " 8 9 77)) (44 . #s(token string "\"hello\"" #"hello" 8 10 78)) (45 . #s(token whitespace "\n" "\n" 8 17 85)) (46 . #s(token keyword "local" local 9 0 86)) (47 . #s(token whitespace " " " " 9 5 91)) (48 . #s(token name "t" t 9 6 92)) (49 . #s(token whitespace " " " " 9 7 93)) (50 . #s(token op "=" = 9 8 94)) (51 . #s(token whitespace " " " " 9 9 95)) (52 . #s(token lcubrace "{" "{" 9 10 96)) (53 . #s(token lsqbrace "[" "[" 9 11 97)) (54 . #s(token name "k" k 9 12 98)) (55 . #s(token rsqbrace "]" "]" 9 13 99)) (56 . #s(token whitespace " " " " 9 14 100)) (57 . #s(token op "=" = 9 15 101)) (58 . #s(token whitespace " " " " 9 16 102)) (59 . #s(token string "\"goodbye\"" #"goodbye" 9 17 103)) (60 . #s(token rcubrace "}" "}" 9 26 112)) (61 . #s(token whitespace "\n" "\n" 9 27 113)) (62 . #s(token name "print" print 10 0 114)) (63 . #s(token lparen "(" "(" 10 5 119)) (64 . #s(token name "t" t 10 6 120)) (65 . #s(token lsqbrace "[" "[" 10 7 121)) (66 . #s(token string "\"hello\"" #"hello" 10 8 122)) (67 . #s(token rsqbrace "]" "]" 10 15 129)) (68 . #s(token rparen ")" ")" 10 16 130)) (69 . #s(token whitespace "\n" "\n" 10 17 131)) (70 . #s(token name "print" print 11 0 132)) (71 . #s(token lparen "(" "(" 11 5 137)) (72 . #s(token name "t" t 11 6 138)) (73 . #s(token lsqbrace "[" "[" 11 7 139)) (74 . #s(token string "\"goodbye\"" #"goodbye" 11 8 140)) (75 . #s(token rsqbrace "]" "]" 11 17 149)) (76 . #s(token rparen ")" ")" 11 18 150)) (77 . #s(token whitespace "\n" "\n" 11 19 151))) -------------------------------------------------------------------------------- /lua-test/lua/examples/table-borders.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(#{}) 4 | print(#{a = 1, b = 2}) 5 | print(#{10, 20, 30, 40, 50}) 6 | print(#{10, 20, 30, nil, 50}) 7 | print(#{nil, 20, 30, nil, nil, 60, nil}) 8 | print(#{nil, 20, 30, 40, 50, a = 1, b = 2}) 9 | print(#{[100] = 1}) 10 | print(#{1, [100] = 2}) 11 | 12 | local t = {nil, 10, 20, nil} 13 | print(#t) 14 | table.insert(t, 20) 15 | table.insert(t, 30) 16 | print(#t) 17 | -------------------------------------------------------------------------------- /lua-test/lua/examples/table-borders.stdout.rktd: -------------------------------------------------------------------------------- 1 | "0\n0\n5\n5\n6\n5\n100\n100\n3\n5\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/table-operator.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local n = 42 4 | local t0 = { math.min(1, 2, 3) } 5 | print(t0[1]) 6 | local t1 = { n / n } 7 | print(t1[1]) 8 | local t2 = { x = n / n, 42 } 9 | print(t2.x, t2[1]) 10 | local t3 = { [n / n] = 42 } 11 | print(t3[1.0]) 12 | -------------------------------------------------------------------------------- /lua-test/lua/examples/table-operator.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\n1.0\n1.0\t42\n42\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/table-separator.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local function print_table(t) 4 | print(string.format("== start %s ==", tostring(t))) 5 | for k, v in pairs(t) do 6 | print(k, v) 7 | end 8 | print("== end ==") 9 | end 10 | 11 | print_table({}) 12 | print_table({1,}) 13 | print_table({1,2,}) 14 | print_table({1;}) 15 | print_table({1;2;}) 16 | print_table({a = 1; b = 2;}) 17 | -------------------------------------------------------------------------------- /lua-test/lua/examples/table-separator.stdout.rktd: -------------------------------------------------------------------------------- 1 | "== start
==\n== end ==\n== start
==\n1\t1\n== end ==\n== start
==\n1\t1\n2\t2\n== end ==\n== start
==\n1\t1\n== end ==\n== start
==\n1\t1\n2\t2\n== end ==\n== start
==\na\t1\nb\t2\n== end ==\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/table-sort.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | t = {} 4 | table.sort(t) 5 | for k, v in pairs(t) do 6 | print(k, v) 7 | end 8 | 9 | t = {5, 2, 3, 0} 10 | table.sort(t) 11 | for k, v in pairs(t) do 12 | print(k, v) 13 | end 14 | 15 | table.sort(t, function(a, b) return a > b end) 16 | for k, v in pairs(t) do 17 | print(k, v) 18 | end 19 | 20 | t = {"B", "a", "C"} 21 | table.sort(t) 22 | for k, v in pairs(t) do 23 | print(k, v) 24 | end 25 | 26 | t = { a = 1; b = 2 } 27 | table.sort(t) 28 | for k, v in pairs(t) do 29 | print(k, v) 30 | end 31 | -------------------------------------------------------------------------------- /lua-test/lua/examples/table-sort.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\t0\n2\t2\n3\t3\n4\t5\n1\t5\n2\t3\n3\t2\n4\t0\n1\tB\n2\tC\n3\ta\na\t1\nb\t2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/table-with-nil.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | print(pcall(function() print({ [nil] = 1 }) end)) 4 | -------------------------------------------------------------------------------- /lua-test/lua/examples/table-with-nil.stdout.rktd: -------------------------------------------------------------------------------- 1 | "false\ttable index is nil\n call stack:\n ...able-with-nil.lua:3:12: function\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/table-with-nil.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) 2 | (1 . #s(token name "lang" lang 1 1 2)) 3 | (2 . #s(token whitespace " " " " 1 5 6)) 4 | (3 . #s(token name "lua" lua 1 6 7)) 5 | (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) 6 | (5 . #s(token name "print" print 3 0 12)) 7 | (6 . #s(token lparen "(" "(" 3 5 17)) 8 | (7 . #s(token name "pcall" pcall 3 6 18)) 9 | (8 . #s(token lparen "(" "(" 3 11 23)) 10 | (9 . #s(token keyword "function" function 3 12 24)) 11 | (10 . #s(token lparen "(" "(" 3 20 32)) 12 | (11 . #s(token rparen ")" ")" 3 21 33)) 13 | (12 . #s(token whitespace " " " " 3 22 34)) 14 | (13 . #s(token name "print" print 3 23 35)) 15 | (14 . #s(token lparen "(" "(" 3 28 40)) 16 | (15 . #s(token lcubrace "{" "{" 3 29 41)) 17 | (16 . #s(token whitespace " " " " 3 30 42)) 18 | (17 . #s(token lsqbrace "[" "[" 3 31 43)) 19 | (18 . #s(token keyword "nil" nil 3 32 44)) 20 | (19 . #s(token rsqbrace "]" "]" 3 35 47)) 21 | (20 . #s(token whitespace " " " " 3 36 48)) 22 | (21 . #s(token op "=" = 3 37 49)) 23 | (22 . #s(token whitespace " " " " 3 38 50)) 24 | (23 . #s(token number "1" 1 3 39 51)) 25 | (24 . #s(token whitespace " " " " 3 40 52)) 26 | (25 . #s(token rcubrace "}" "}" 3 41 53)) 27 | (26 . #s(token rparen ")" ")" 3 42 54)) 28 | (27 . #s(token whitespace " " " " 3 43 55)) 29 | (28 . #s(token keyword "end" end 3 44 56)) 30 | (29 . #s(token rparen ")" ")" 3 47 59)) 31 | (30 . #s(token rparen ")" ")" 3 48 60)) 32 | (31 . #s(token whitespace "\n" "\n" 3 49 61))) 33 | -------------------------------------------------------------------------------- /lua-test/lua/examples/table.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | for k, v in pairs({1, 2, nil, 3}) do 4 | print(k,v ) 5 | end 6 | 7 | print(table.pack()) 8 | print(table.pack(1, 2, 3)) 9 | for k, v in pairs(table.pack(1, 2, 3)) do 10 | print(k, v) 11 | end 12 | for k, v in pairs(table.pack(1, 2, nil, 3)) do 13 | print(k, v) 14 | end 15 | 16 | print(select("#")) 17 | print(select("#", 1, 2, 3)) 18 | 19 | print("select 2") 20 | for _, v in pairs(table.pack(select(2, 1, 2, 3))) do 21 | print(v) 22 | end 23 | 24 | print("select -2") 25 | for _, v in pairs(table.pack(select(-2, 1, 2, 3))) do 26 | print(v) 27 | end 28 | 29 | print("select -1") 30 | for _, v in pairs(table.pack(select(-1, 1, 2, 3))) do 31 | print(v) 32 | end 33 | 34 | print("select 0", pcall(function() select(0, 1, 2, 3) end)) 35 | for i = -1, -9, -1 do 36 | print("select", i) 37 | for _, v in pairs(table.pack(select(i, 1, 2, 3))) do 38 | print(v) 39 | end 40 | end 41 | 42 | local t = {} 43 | table.insert(t, 'a') 44 | table.insert(t, 'b') 45 | table.insert(t, 'c') 46 | for k, v in pairs(t) do 47 | print(k, v) 48 | end 49 | 50 | table.insert(t, 2, 'd') 51 | for k, v in pairs(t) do 52 | print(k, v) 53 | end 54 | 55 | print(pcall(function() table.insert(t, 0, 'e') end)) 56 | print(pcall(function() table.insert(t, 6, 'e') end)) 57 | 58 | table.remove(t) 59 | for k, v in pairs(t) do 60 | print(k, v) 61 | end 62 | 63 | table.remove(t, 2) 64 | for k, v in pairs(t) do 65 | print(k, v) 66 | end 67 | 68 | print(pcall(function() table.remove(t, 0) end)) 69 | print(pcall(function() table.remove(t, 3) end)) 70 | 71 | table.insert(t, 'c') 72 | print(table.concat(t)) 73 | print(table.concat(t, ' ')) 74 | print(table.concat(t, ' ', 2)) 75 | print(table.concat(t, ' ', 5)) 76 | 77 | print(pcall(function() t[nil] = 5 end)) 78 | -------------------------------------------------------------------------------- /lua-test/lua/examples/table.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\t1\n2\t2\n4\t3\n
\n
\nn\t3\n1\t1\n2\t2\n3\t3\nn\t4\n1\t1\n2\t2\n4\t3\n0\n3\nselect 2\n2\n2\n3\nselect -2\n2\n2\n3\nselect -1\n1\n3\nselect 0\tfalse\tselect: 0 index\nselect\t-1\n1\n3\nselect\t-2\n2\n2\n3\nselect\t-3\n3\n1\n2\n3\nselect\t-4\n1\n3\nselect\t-5\n2\n2\n3\nselect\t-6\n3\n1\n2\n3\nselect\t-7\n1\n3\nselect\t-8\n2\n2\n3\nselect\t-9\n3\n1\n2\n3\n1\ta\n2\tb\n3\tc\n1\ta\n2\td\n3\tb\n4\tc\nfalse\ttable.insert: position out of bounds\nfalse\ttable.insert: position out of bounds\n1\ta\n2\td\n3\tb\n1\ta\n2\tb\nfalse\ttable.remove: position out of bounds\nfalse\ttable.remove: position out of bounds\nabc\na b c\nb c\n\nfalse\ttable index is nil\n call stack:\n ...xamples/table.lua:77:12: function\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/tostring.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | Wrapper = {} 4 | function Wrapper:new(o) 5 | o = o or {n = 0} 6 | setmetatable(o, self) 7 | return o 8 | end 9 | function Wrapper:__tostring() 10 | return "Wrapper:" .. self.n 11 | end 12 | 13 | print(Wrapper) 14 | print(Wrapper:new({n = 5})) 15 | 16 | Named = {__name = "Named"} 17 | foo = {} 18 | setmetatable(foo, Named) 19 | print(Named) 20 | print(foo) 21 | -------------------------------------------------------------------------------- /lua-test/lua/examples/tostring.stdout.rktd: -------------------------------------------------------------------------------- 1 | "
\nWrapper:5\n
\n\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/values-00.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | function f(a, b) 4 | print(a, b) 5 | end 6 | 7 | f(1) 8 | f(1, 2) 9 | f(1, 2, 3) 10 | 11 | function g() 12 | return 1, 2 13 | end 14 | 15 | f(g()) 16 | f(g(), g()) 17 | f(g(), g(), g()) 18 | 19 | function h() 20 | end 21 | 22 | print(h()) 23 | 24 | local x, y = g(), g(), nil 25 | print(x, y) 26 | 27 | local x, y = g() 28 | print(x, y) 29 | 30 | function i() 31 | return function() 32 | return 1 33 | end, function() 34 | return 2 35 | end 36 | end 37 | 38 | print(i()) 39 | local fa = i() 40 | print(fa()) 41 | 42 | local fa, fb, fc = i() 43 | print(fa()) 44 | print(fb()) 45 | print(fc) 46 | 47 | local function j(x, y, ...) 48 | local z = ... 49 | print(x, y, z, ...) 50 | end 51 | 52 | j(1, 2) 53 | j(1, 2, 3) 54 | j(1, 2, 3, 4) 55 | j(1, 2, 3, 4, 5) 56 | 57 | local function k(...) 58 | local t = {...} 59 | print("k:", #t) 60 | print(t) 61 | print(t[1]) 62 | print(t[2]) 63 | end 64 | 65 | k() 66 | k(1, 2) 67 | k(1, g()) 68 | k(g(), g()) 69 | k(g()) 70 | -------------------------------------------------------------------------------- /lua-test/lua/examples/values-00.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\tnil\n1\t2\n1\t2\n1\t2\n1\t1\n1\t1\n\n1\t1\n1\t2\n\t\n1\n1\n2\nnil\n1\t2\tnil\n1\t2\t3\t3\n1\t2\t3\t3\t4\n1\t2\t3\t3\t4\t5\nk:\t0\n
\nnil\nnil\nk:\t2\n
\n1\n2\nk:\t3\n
\n1\n1\nk:\t3\n
\n1\n1\nk:\t2\n
\n1\n2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/values-01.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | function f() 4 | return 1, 2 5 | end 6 | 7 | local x, y = f() 8 | print(x, y) 9 | print(x + f() + f()) 10 | 11 | function g() 12 | return 0, f() 13 | end 14 | print(g()) 15 | 16 | local a, b, c, d = g() 17 | print(a, b, c, d) 18 | 19 | local a, b, c, d = g(), nil 20 | print(a, b, c, d) 21 | -------------------------------------------------------------------------------- /lua-test/lua/examples/values-01.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\t2\n3\n0\t1\t2\n0\t1\t2\tnil\n0\tnil\tnil\tnil\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/values-02.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local t = {1, 2, 3} 4 | print(t[1]) 5 | print(t[2]) 6 | print(t[3]) 7 | 8 | local function f() 9 | return 1, 2 10 | end 11 | 12 | local t = {x = f()} 13 | print(t.x) 14 | 15 | local t2 = {a = t} 16 | print(t2.a.x) 17 | 18 | local t3 = {f()} 19 | print(t3[1], t3[2], t3[3]) 20 | 21 | local t4 = {f(), nil} 22 | print(t4[1], t4[2], t4[3]) 23 | -------------------------------------------------------------------------------- /lua-test/lua/examples/values-02.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\n2\n3\n1\n1\n1\t2\tnil\n1\tnil\tnil\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/values-03.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | local function f() 4 | return 1, 2 5 | end 6 | 7 | a, b = 1, 2, f() 8 | print(a, b) 9 | -------------------------------------------------------------------------------- /lua-test/lua/examples/values-03.stdout.rktd: -------------------------------------------------------------------------------- 1 | "1\t2\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/values-03.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token keyword "local" local 3 0 12)) (6 . #s(token whitespace " " " " 3 5 17)) (7 . #s(token keyword "function" function 3 6 18)) (8 . #s(token whitespace " " " " 3 14 26)) (9 . #s(token name "f" f 3 15 27)) (10 . #s(token lparen "(" "(" 3 16 28)) (11 . #s(token rparen ")" ")" 3 17 29)) (12 . #s(token whitespace "\n " "\n " 3 18 30)) (13 . #s(token keyword "return" return 4 4 35)) (14 . #s(token whitespace " " " " 4 10 41)) (15 . #s(token number "1" 1 4 11 42)) (16 . #s(token comma "," "," 4 12 43)) (17 . #s(token whitespace " " " " 4 13 44)) (18 . #s(token number "2" 2 4 14 45)) (19 . #s(token whitespace "\n" "\n" 4 15 46)) (20 . #s(token keyword "end" end 5 0 47)) (21 . #s(token whitespace "\n\n" "\n\n" 5 3 50)) (22 . #s(token name "a" a 7 0 52)) (23 . #s(token comma "," "," 7 1 53)) (24 . #s(token whitespace " " " " 7 2 54)) (25 . #s(token name "b" b 7 3 55)) (26 . #s(token whitespace " " " " 7 4 56)) (27 . #s(token op "=" = 7 5 57)) (28 . #s(token whitespace " " " " 7 6 58)) (29 . #s(token number "1" 1 7 7 59)) (30 . #s(token comma "," "," 7 8 60)) (31 . #s(token whitespace " " " " 7 9 61)) (32 . #s(token number "2" 2 7 10 62)) (33 . #s(token comma "," "," 7 11 63)) (34 . #s(token whitespace " " " " 7 12 64)) (35 . #s(token name "f" f 7 13 65)) (36 . #s(token lparen "(" "(" 7 14 66)) (37 . #s(token rparen ")" ")" 7 15 67)) (38 . #s(token whitespace "\n" "\n" 7 16 68)) (39 . #s(token name "print" print 8 0 69)) (40 . #s(token lparen "(" "(" 8 5 74)) (41 . #s(token name "a" a 8 6 75)) (42 . #s(token comma "," "," 8 7 76)) (43 . #s(token whitespace " " " " 8 8 77)) (44 . #s(token name "b" b 8 9 78)) (45 . #s(token rparen ")" ")" 8 10 79)) (46 . #s(token whitespace "\n" "\n" 8 11 80))) -------------------------------------------------------------------------------- /lua-test/lua/examples/while.lua: -------------------------------------------------------------------------------- 1 | #lang lua 2 | 3 | i = 0 4 | while i < 10 do 5 | print(i) 6 | i = i + 1 7 | end 8 | 9 | i = 0 10 | while i < 10 do 11 | print(i) 12 | break 13 | end 14 | 15 | while nil do 16 | print("fail") 17 | end 18 | -------------------------------------------------------------------------------- /lua-test/lua/examples/while.stdout.rktd: -------------------------------------------------------------------------------- 1 | "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n" -------------------------------------------------------------------------------- /lua-test/lua/examples/while.tokens.rktd: -------------------------------------------------------------------------------- 1 | #hasheqv((0 . #s(token op "#" |#| 1 0 1)) (1 . #s(token name "lang" lang 1 1 2)) (2 . #s(token whitespace " " " " 1 5 6)) (3 . #s(token name "lua" lua 1 6 7)) (4 . #s(token whitespace "\n\n" "\n\n" 1 9 10)) (5 . #s(token name "i" i 3 0 12)) (6 . #s(token whitespace " " " " 3 1 13)) (7 . #s(token op "=" = 3 2 14)) (8 . #s(token whitespace " " " " 3 3 15)) (9 . #s(token number "0" 0 3 4 16)) (10 . #s(token whitespace "\n" "\n" 3 5 17)) (11 . #s(token keyword "while" while 4 0 18)) (12 . #s(token whitespace " " " " 4 5 23)) (13 . #s(token name "i" i 4 6 24)) (14 . #s(token whitespace " " " " 4 7 25)) (15 . #s(token op "<" < 4 8 26)) (16 . #s(token whitespace " " " " 4 9 27)) (17 . #s(token number "10" 10 4 10 28)) (18 . #s(token whitespace " " " " 4 12 30)) (19 . #s(token keyword "do" do 4 13 31)) (20 . #s(token whitespace "\n " "\n " 4 15 33)) (21 . #s(token name "print" print 5 4 38)) (22 . #s(token lparen "(" "(" 5 9 43)) (23 . #s(token name "i" i 5 10 44)) (24 . #s(token rparen ")" ")" 5 11 45)) (25 . #s(token whitespace "\n " "\n " 5 12 46)) (26 . #s(token name "i" i 6 4 51)) (27 . #s(token whitespace " " " " 6 5 52)) (28 . #s(token op "=" = 6 6 53)) (29 . #s(token whitespace " " " " 6 7 54)) (30 . #s(token name "i" i 6 8 55)) (31 . #s(token whitespace " " " " 6 9 56)) (32 . #s(token op "+" + 6 10 57)) (33 . #s(token whitespace " " " " 6 11 58)) (34 . #s(token number "1" 1 6 12 59)) (35 . #s(token whitespace "\n" "\n" 6 13 60)) (36 . #s(token keyword "end" end 7 0 61)) (37 . #s(token whitespace "\n\n" "\n\n" 7 3 64)) (38 . #s(token name "i" i 9 0 66)) (39 . #s(token whitespace " " " " 9 1 67)) (40 . #s(token op "=" = 9 2 68)) (41 . #s(token whitespace " " " " 9 3 69)) (42 . #s(token number "0" 0 9 4 70)) (43 . #s(token whitespace "\n" "\n" 9 5 71)) (44 . #s(token keyword "while" while 10 0 72)) (45 . #s(token whitespace " " " " 10 5 77)) (46 . #s(token name "i" i 10 6 78)) (47 . #s(token whitespace " " " " 10 7 79)) (48 . #s(token op "<" < 10 8 80)) (49 . #s(token whitespace " " " " 10 9 81)) (50 . #s(token number "10" 10 10 10 82)) (51 . #s(token whitespace " " " " 10 12 84)) (52 . #s(token keyword "do" do 10 13 85)) (53 . #s(token whitespace "\n " "\n " 10 15 87)) (54 . #s(token name "print" print 11 4 92)) (55 . #s(token lparen "(" "(" 11 9 97)) (56 . #s(token name "i" i 11 10 98)) (57 . #s(token rparen ")" ")" 11 11 99)) (58 . #s(token whitespace "\n " "\n " 11 12 100)) (59 . #s(token keyword "break" break 12 4 105)) (60 . #s(token whitespace "\n" "\n" 12 9 110)) (61 . #s(token keyword "end" end 13 0 111)) (62 . #s(token whitespace "\n\n" "\n\n" 13 3 114)) (63 . #s(token keyword "while" while 15 0 116)) (64 . #s(token whitespace " " " " 15 5 121)) (65 . #s(token keyword "nil" nil 15 6 122)) (66 . #s(token whitespace " " " " 15 9 125)) (67 . #s(token keyword "do" do 15 10 126)) (68 . #s(token whitespace "\n " "\n " 15 12 128)) (69 . #s(token name "print" print 16 4 133)) (70 . #s(token lparen "(" "(" 16 9 138)) (71 . #s(token string "\"fail\"" #"fail" 16 10 139)) (72 . #s(token rparen ")" ")" 16 16 145)) (73 . #s(token whitespace "\n" "\n" 16 17 146)) (74 . #s(token keyword "end" end 17 0 147)) (75 . #s(token whitespace "\n" "\n" 17 3 150))) -------------------------------------------------------------------------------- /lua-test/lua/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define test-omit-paths '("examples")) 4 | (define compile-omit-paths '("examples")) 5 | -------------------------------------------------------------------------------- /lua-test/lua/lexer.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require lua/lang/lexer 4 | racket/path 5 | racket/pretty 6 | racket/runtime-path 7 | rackunit) 8 | 9 | (define-runtime-path examples 10 | "examples") 11 | 12 | (define (lexer->tokens l) 13 | (let loop ([tokens null]) 14 | (define token (lexer-take l)) 15 | (case (token-type token) 16 | [(eof) (reverse tokens)] 17 | [else (loop (cons token tokens))]))) 18 | 19 | (define lexer-tests 20 | (test-suite 21 | "lexer" 22 | 23 | (test-suite 24 | "lexer-peek" 25 | 26 | (test-case "gives at most one token of lookahead" 27 | (define l (make-lexer (open-input-string "fib(1 + 2)"))) 28 | (check-equal? (lexer-peek l) (token 'name "fib" 'fib 1 0 1)) 29 | (check-equal? (lexer-peek l) (token 'name "fib" 'fib 1 0 1)) 30 | (check-equal? (lexer-take l) (token 'name "fib" 'fib 1 0 1)) 31 | (check-equal? (lexer-peek l) (token 'lparen "(" "(" 1 3 4)) 32 | (check-equal? (lexer-peek l) (token 'lparen "(" "(" 1 3 4)))) 33 | 34 | (test-suite 35 | "examples" 36 | 37 | (for ([path (in-list (directory-list examples #:build? #t))] 38 | #:when (path-has-extension? path #".lua")) 39 | (define tokens-path 40 | (path-replace-extension path #".tokens.rktd")) 41 | (define tokens 42 | (call-with-input-file path 43 | (lambda (in) 44 | (for/hasheqv ([idx (in-naturals)] 45 | [tok (in-list (lexer->tokens (make-lexer in #:skip-comments? #f)))]) 46 | (values idx tok))))) 47 | (with-handlers ([exn:fail:filesystem? 48 | (λ (_) 49 | (call-with-output-file tokens-path 50 | #:exists 'replace 51 | (lambda (out) 52 | (pretty-write tokens out))))]) 53 | (define expected-tokens 54 | (call-with-input-file tokens-path read)) 55 | (define n 56 | (max (hash-count tokens) 57 | (hash-count expected-tokens))) 58 | (for ([idx (in-range n)]) 59 | (define a (hash-ref tokens idx #f)) 60 | (define b (hash-ref expected-tokens idx #f)) 61 | (check-equal? a b (format "~a#~a" path idx)))))))) 62 | 63 | (module+ test 64 | (require rackunit/text-ui) 65 | (parameterize ([port-count-lines-enabled #t]) 66 | (run-tests lexer-tests))) 67 | -------------------------------------------------------------------------------- /lua-test/lua/table.rkt: -------------------------------------------------------------------------------- 1 | #lang racket/base 2 | 3 | (require lua/value 4 | rackcheck 5 | racket/list 6 | racket/match 7 | rackunit) 8 | 9 | (define table-tests 10 | (test-suite 11 | "table" 12 | 13 | (test-suite 14 | "in-table" 15 | 16 | (check-equal? (for/list ([v (in-table (make-table))]) v) null) 17 | (check-equal? 18 | (for/list ([v (in-table (make-table 1 2 3))]) v) 19 | '(1 2 3)) 20 | (check-equal? 21 | (for/list ([v (in-table (make-table nil 1 nil 2 nil nil 3))]) 22 | v) 23 | (list nil 1 nil 2 nil nil 3))) 24 | 25 | (test-suite 26 | "table-length" 27 | 28 | (test-case "sequences" 29 | (check-property 30 | (property ([n (gen:integer-in 0 1000)]) 31 | (check-equal? 32 | (table-length 33 | (apply make-table (make-list n #t))) 34 | n)))) 35 | 36 | ;; Generate random sequences with gaps in them and ensure valid 37 | ;; borders are returned. Remove a random number of inserted elements 38 | ;; to avoid always hitting the (not (nil? (table-ref t table-hi))) 39 | ;; case. 40 | (test-case "sequences with gaps" 41 | (check-property 42 | (property ([groups (gen:integer-in 0 16)] 43 | [instrs (apply 44 | gen:tuple 45 | (for/list ([n (in-range groups)]) 46 | (define gen:len (gen:integer-in n (add1 (* n 1000)))) 47 | (gen:frequency 48 | `((3 . ,(gen:let ([m gen:len]) `(gap ,m))) 49 | (2 . ,(gen:let ([m gen:len]) `(rem ,m))) 50 | (7 . ,gen:len)))))]) 51 | (define t (make-table)) 52 | (define last-idx 53 | (for/fold ([idx 1]) 54 | ([instr (in-list instrs)]) 55 | (match instr 56 | [`(gap ,len) (+ idx len)] 57 | [`(rem ,_) idx] 58 | [len 59 | (for ([i (in-range len)]) 60 | (table-set! t (+ idx i) #t)) 61 | (+ idx len)]))) 62 | (for/fold ([idx last-idx]) 63 | ([instr (in-list instrs)]) 64 | (match instr 65 | [`(rem ,len) 66 | (for ([i (in-range len)]) 67 | (table-set! t (- idx i) nil)) 68 | (- idx len)] 69 | [_ idx])) 70 | (define border 71 | (table-length t)) 72 | (label! 73 | (if (zero? border) 74 | "zero" 75 | "non-zero")) 76 | (cond 77 | [(zero? border) 78 | (check-true 79 | (nil? (table-ref t 1)))] 80 | [else 81 | (check-false 82 | (nil? (table-ref t border))) 83 | (check-true 84 | (nil? (table-ref t (add1 border))))]))))))) 85 | 86 | (module+ test 87 | (require rackunit/text-ui) 88 | (run-tests table-tests)) 89 | -------------------------------------------------------------------------------- /lua/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 Bogdan Popa 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 14 | 3. Neither the name of the copyright holder nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 26 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | -------------------------------------------------------------------------------- /lua/info.rkt: -------------------------------------------------------------------------------- 1 | #lang info 2 | 3 | (define license 'BSD-3-Clause) 4 | (define collection "lua") 5 | (define deps '("base" 6 | "lua-lib")) 7 | (define build-deps '("base" 8 | "racket-doc" 9 | "sandbox-lib" 10 | "scribble-lib")) 11 | (define implies '("lua-lib")) 12 | (define scribblings '(("lua-manual.scrbl" () (language)))) 13 | --------------------------------------------------------------------------------