[1650] | 1 | -- tolua: code class |
---|
| 2 | -- Written by Waldemar Celes |
---|
| 3 | -- TeCGraf/PUC-Rio |
---|
| 4 | -- Jul 1999 |
---|
| 5 | -- $Id: $ |
---|
| 6 | |
---|
| 7 | -- This code is free software; you can redistribute it and/or modify it. |
---|
| 8 | -- The software provided hereunder is on an "as is" basis, and |
---|
| 9 | -- the author has no obligation to provide maintenance, support, updates, |
---|
| 10 | -- enhancements, or modifications. |
---|
| 11 | |
---|
| 12 | -- global |
---|
| 13 | code_n = 1 |
---|
| 14 | |
---|
| 15 | -- Code class |
---|
| 16 | -- Represents Lua code to be compiled and included |
---|
| 17 | -- in the initialization function. |
---|
| 18 | -- The following fields are stored: |
---|
| 19 | -- text = text code |
---|
| 20 | classCode = { |
---|
[2229] | 21 | text = '', |
---|
[1650] | 22 | } |
---|
| 23 | classCode.__index = classCode |
---|
| 24 | setmetatable(classCode,classFeature) |
---|
| 25 | |
---|
| 26 | -- register code |
---|
| 27 | function classCode:register (pre) |
---|
[2229] | 28 | pre = pre or '' |
---|
| 29 | -- clean Lua code |
---|
| 30 | local s = clean(self.text) |
---|
| 31 | if not s then |
---|
| 32 | --print(self.text) |
---|
| 33 | error("parser error in embedded code") |
---|
| 34 | end |
---|
[1650] | 35 | |
---|
[2229] | 36 | -- get first line |
---|
| 37 | local _, _, first_line=string.find(self.text, "^([^\n\r]*)") |
---|
| 38 | if string.find(first_line, "^%s*%-%-") then |
---|
| 39 | if string.find(first_line, "^%-%-##") then |
---|
| 40 | first_line = string.gsub(first_line, "^%-%-##", "") |
---|
| 41 | if flags['C'] then |
---|
| 42 | s = string.gsub(s, "^%-%-##[^\n\r]*\n", "") |
---|
| 43 | end |
---|
| 44 | end |
---|
| 45 | else |
---|
| 46 | first_line = "" |
---|
| 47 | end |
---|
[1650] | 48 | |
---|
[2229] | 49 | -- convert to C |
---|
| 50 | output('\n'..pre..'{ /* begin embedded lua code */\n') |
---|
| 51 | output(pre..' int top = lua_gettop(tolua_S);') |
---|
| 52 | output(pre..' static unsigned char B[] = {\n ') |
---|
| 53 | local t={n=0} |
---|
| 54 | local b = gsub(s, '(.)', |
---|
| 55 | function (c) |
---|
| 56 | local e = '' |
---|
| 57 | t.n=t.n+1 |
---|
| 58 | if t.n==15 then |
---|
| 59 | t.n=0 e='\n'..pre..' ' |
---|
| 60 | end |
---|
| 61 | return format('%3u,%s',strbyte(c),e) |
---|
| 62 | end |
---|
| 63 | ) |
---|
| 64 | output(b..strbyte(" ")) |
---|
| 65 | output('\n'..pre..' };\n') |
---|
| 66 | if first_line and first_line ~= "" then |
---|
| 67 | output(pre..' tolua_dobuffer(tolua_S,(char*)B,sizeof(B),"tolua embedded: '..first_line..'");') |
---|
| 68 | else |
---|
| 69 | output(pre..' tolua_dobuffer(tolua_S,(char*)B,sizeof(B),"tolua: embedded Lua code '..code_n..'");') |
---|
| 70 | end |
---|
| 71 | output(pre..' lua_settop(tolua_S, top);') |
---|
| 72 | output(pre..'} /* end of embedded lua code */\n\n') |
---|
| 73 | code_n = code_n +1 |
---|
[1650] | 74 | end |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | -- Print method |
---|
| 78 | function classCode:print (ident,close) |
---|
[2229] | 79 | print(ident.."Code{") |
---|
| 80 | print(ident.." text = [["..self.text.."]],") |
---|
| 81 | print(ident.."}"..close) |
---|
[1650] | 82 | end |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | -- Internal constructor |
---|
| 86 | function _Code (t) |
---|
[2229] | 87 | setmetatable(t,classCode) |
---|
| 88 | append(t) |
---|
| 89 | return t |
---|
[1650] | 90 | end |
---|
| 91 | |
---|
| 92 | -- Constructor |
---|
| 93 | -- Expects a string representing the code text |
---|
| 94 | function Code (l) |
---|
[2229] | 95 | return _Code { |
---|
| 96 | text = l |
---|
| 97 | } |
---|
[1650] | 98 | end |
---|
| 99 | |
---|
| 100 | |
---|