summaryrefslogtreecommitdiff
path: root/tests/llex.js
blob: 8b69accd8f32cf21646d478011c2a95144aac70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"use strict";

const test     = require('tape');
const beautify = require('js-beautify').js_beautify;

const tests    = require("./tests.js");

const lapi     = require("../src/lapi.js");
const lauxlib  = require("../src/lauxlib.js");
const llex     = require("../src/llex.js");
const lua      = require('../src/lua.js');
const R        = llex.RESERVED;


test('basic lexing: TK_RETURN, TK_STRING', function (t) {
    let luaCode = `
        return "hello lex !"
    `, L;
    
    t.plan(2);

    let readTokens = [];

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        let ls = new llex.LexState();
        llex.luaX_setinput(L, ls, new llex.MBuffer(luaCode), luaCode, luaCode.charAt(0));

        llex.luaX_next(ls);

        while (ls.t.token !== R.TK_EOS) {
            // console.log(llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED]);

            readTokens.push(ls.t.token);
            llex.luaX_next(ls);
        }


    }, "JS Lua program ran without error");

    t.deepEqual(
        readTokens,
        [R.TK_RETURN, R.TK_STRING],
        "Correct tokens found"
    );

});


test('TK_LOCAL, TK_NAME, TK_INT', function (t) {
    let luaCode = `
        local f = testing(aBunch)
            return "of"
        end

        return f("things") + 12
    `, L;
    
    t.plan(2);

    let readTokens = [];

    t.doesNotThrow(function () {

        L = lauxlib.luaL_newstate();

        let ls = new llex.LexState();
        llex.luaX_setinput(L, ls, new llex.MBuffer(luaCode), luaCode, luaCode.charAt(0));

        llex.luaX_next(ls);

        while (ls.t.token !== R.TK_EOS) {
            // console.log(ls.t.token >= llex.FIRST_RESERVED ?
            //         llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED]
            //         : ls.t.token);

            readTokens.push(ls.t.token);
            llex.luaX_next(ls);
        }


    }, "JS Lua program ran without error");

    t.deepEqual(
        readTokens,
        [
            R.TK_LOCAL, R.TK_NAME, '=', R.TK_NAME, '(', R.TK_NAME, ')',
                R.TK_RETURN, R.TK_STRING,
            R.TK_END,
            R.TK_RETURN, R.TK_NAME, '(', R.TK_STRING, ')', '+', R.TK_INT
        ],
        "Correct tokens found"
    );

});