aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/src/LuaSL_compile.c
diff options
context:
space:
mode:
authorDavid Walter Seikel2012-02-03 15:05:50 +1000
committerDavid Walter Seikel2012-02-03 15:05:50 +1000
commitefaf675e44725830f4d77f5905543b232eedb131 (patch)
tree74896969a2d105010eaefa513fb1ceb600b03831 /LuaSL/src/LuaSL_compile.c
parentSwitch to using LuaJIT for compiling. (diff)
downloadSledjHamr-efaf675e44725830f4d77f5905543b232eedb131.zip
SledjHamr-efaf675e44725830f4d77f5905543b232eedb131.tar.gz
SledjHamr-efaf675e44725830f4d77f5905543b232eedb131.tar.bz2
SledjHamr-efaf675e44725830f4d77f5905543b232eedb131.tar.xz
Half arsed bit ops, does not even result in compilable Lua sometimes.
Diffstat (limited to 'LuaSL/src/LuaSL_compile.c')
-rw-r--r--LuaSL/src/LuaSL_compile.c51
1 files changed, 43 insertions, 8 deletions
diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c
index d68161a..d13df68 100644
--- a/LuaSL/src/LuaSL_compile.c
+++ b/LuaSL/src/LuaSL_compile.c
@@ -7,6 +7,7 @@ static LSL_Leaf *evaluateNoToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *ri
7static LSL_Leaf *evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 7static LSL_Leaf *evaluateOperationToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
8static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 8static LSL_Leaf *eveluateParenthesisToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
9static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right); 9static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_Leaf *right);
10static void outputBitOp(FILE *file, outputMode mode, LSL_Leaf *leaf);
10static void outputBlockToken(FILE *file, outputMode mode, LSL_Leaf *content); 11static void outputBlockToken(FILE *file, outputMode mode, LSL_Leaf *content);
11static void outputCrementsToken(FILE *file, outputMode mode, LSL_Leaf *content); 12static void outputCrementsToken(FILE *file, outputMode mode, LSL_Leaf *content);
12static void outputFloatToken(FILE *file, outputMode mode, LSL_Leaf *content); 13static void outputFloatToken(FILE *file, outputMode mode, LSL_Leaf *content);
@@ -39,9 +40,9 @@ LSL_Token LSL_Tokens[] =
39// LUA - "and" returns its first argument if it is false, otherwise, it returns its second argument. "or" returns its first argument if it is not false, otherwise it returns its second argument. 40// LUA - "and" returns its first argument if it is false, otherwise, it returns its second argument. "or" returns its first argument if it is not false, otherwise it returns its second argument.
40// Note that the above means that "and/or" can return any type. 41// Note that the above means that "and/or" can return any type.
41 {LSL_BOOL_OR, ST_BOOLEAN, "||", LSL_RIGHT2LEFT, NULL, evaluateOperationToken}, 42 {LSL_BOOL_OR, ST_BOOLEAN, "||", LSL_RIGHT2LEFT, NULL, evaluateOperationToken},
42 {LSL_BIT_OR, ST_BITWISE, "|", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 43 {LSL_BIT_OR, ST_BITWISE, "|", LSL_LEFT2RIGHT, outputBitOp, evaluateOperationToken},
43 {LSL_BIT_XOR, ST_BITWISE, "^", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 44 {LSL_BIT_XOR, ST_BITWISE, "^", LSL_LEFT2RIGHT, outputBitOp, evaluateOperationToken},
44 {LSL_BIT_AND, ST_BITWISE, "&", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 45 {LSL_BIT_AND, ST_BITWISE, "&", LSL_LEFT2RIGHT, outputBitOp, evaluateOperationToken},
45// QUIRK - Booleans and conditionals are executed right to left. Or maybe not, depending on who you believe. 46// QUIRK - Booleans and conditionals are executed right to left. Or maybe not, depending on who you believe.
46 {LSL_NOT_EQUAL, ST_EQUALITY, "!=", LSL_RIGHT2LEFT, NULL, evaluateOperationToken}, 47 {LSL_NOT_EQUAL, ST_EQUALITY, "!=", LSL_RIGHT2LEFT, NULL, evaluateOperationToken},
47 {LSL_EQUAL, ST_EQUALITY, "==", LSL_RIGHT2LEFT, NULL, evaluateOperationToken}, 48 {LSL_EQUAL, ST_EQUALITY, "==", LSL_RIGHT2LEFT, NULL, evaluateOperationToken},
@@ -51,8 +52,8 @@ LSL_Token LSL_Tokens[] =
51 {LSL_LESS_THAN, ST_COMPARISON, "<", LSL_RIGHT2LEFT, NULL, evaluateOperationToken}, 52 {LSL_LESS_THAN, ST_COMPARISON, "<", LSL_RIGHT2LEFT, NULL, evaluateOperationToken},
52// LUA - comparisons are always false if they are different types. Tables, userdata, and functions are compared by reference. Strings campare in alphabetical order, depending on current locale. 53// LUA - comparisons are always false if they are different types. Tables, userdata, and functions are compared by reference. Strings campare in alphabetical order, depending on current locale.
53// LUA - really only has three conditionals, as it translates a ~= b to not (a == b), a > b to b < a, and a >= b to b <= a. 54// LUA - really only has three conditionals, as it translates a ~= b to not (a == b), a > b to b < a, and a >= b to b <= a.
54 {LSL_RIGHT_SHIFT, ST_BITWISE, ">>", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 55 {LSL_RIGHT_SHIFT, ST_BITWISE, ">>", LSL_LEFT2RIGHT, outputBitOp, evaluateOperationToken},
55 {LSL_LEFT_SHIFT, ST_BITWISE, "<<", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 56 {LSL_LEFT_SHIFT, ST_BITWISE, "<<", LSL_LEFT2RIGHT, outputBitOp, evaluateOperationToken},
56 {LSL_CONCATENATE, ST_ADD, "+", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 57 {LSL_CONCATENATE, ST_ADD, "+", LSL_LEFT2RIGHT, NULL, evaluateOperationToken},
57 {LSL_ADD, ST_ADD, "+", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 58 {LSL_ADD, ST_ADD, "+", LSL_LEFT2RIGHT, NULL, evaluateOperationToken},
58 {LSL_SUBTRACT, ST_SUBTRACT, "-", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 59 {LSL_SUBTRACT, ST_SUBTRACT, "-", LSL_LEFT2RIGHT, NULL, evaluateOperationToken},
@@ -63,7 +64,7 @@ LSL_Token LSL_Tokens[] =
63 {LSL_DIVIDE, ST_MULTIPLY, "/", LSL_LEFT2RIGHT, NULL, evaluateOperationToken}, 64 {LSL_DIVIDE, ST_MULTIPLY, "/", LSL_LEFT2RIGHT, NULL, evaluateOperationToken},
64 {LSL_NEGATION, ST_NEGATE, "-", LSL_RIGHT2LEFT | LSL_UNARY, NULL, evaluateOperationToken}, 65 {LSL_NEGATION, ST_NEGATE, "-", LSL_RIGHT2LEFT | LSL_UNARY, NULL, evaluateOperationToken},
65 {LSL_BOOL_NOT, ST_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, NULL, evaluateOperationToken}, 66 {LSL_BOOL_NOT, ST_BOOL_NOT, "!", LSL_RIGHT2LEFT | LSL_UNARY, NULL, evaluateOperationToken},
66 {LSL_BIT_NOT, ST_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, NULL, evaluateOperationToken}, 67 {LSL_BIT_NOT, ST_BIT_NOT, "~", LSL_RIGHT2LEFT | LSL_UNARY, outputBitOp, evaluateOperationToken},
67 68
68// LUA precedence - (it has no bit operators, at least not until 5.2, but LuaJIT has them.) 69// LUA precedence - (it has no bit operators, at least not until 5.2, but LuaJIT has them.)
69// or 70// or
@@ -1515,7 +1516,8 @@ static void outputLeaf(FILE *file, outputMode mode, LSL_Leaf *leaf)
1515{ 1516{
1516 if (leaf) 1517 if (leaf)
1517 { 1518 {
1518 outputLeaf(file, mode, leaf->left); 1519 if ((OM_LUA == mode) &&(ST_BITWISE != leaf->toKen->subType))
1520 outputLeaf(file, mode, leaf->left);
1519#if LUASL_DIFF_CHECK 1521#if LUASL_DIFF_CHECK
1520 if ((!(LSL_NOIGNORE & leaf->toKen->flags)) && (leaf->ignorable)) 1522 if ((!(LSL_NOIGNORE & leaf->toKen->flags)) && (leaf->ignorable))
1521 fwrite(eina_strbuf_string_get(leaf->ignorable), 1, eina_strbuf_length_get(leaf->ignorable), file); 1523 fwrite(eina_strbuf_string_get(leaf->ignorable), 1, eina_strbuf_length_get(leaf->ignorable), file);
@@ -1552,7 +1554,8 @@ static void outputLeaf(FILE *file, outputMode mode, LSL_Leaf *leaf)
1552 else 1554 else
1553 fprintf(file, "%s", leaf->toKen->toKen); 1555 fprintf(file, "%s", leaf->toKen->toKen);
1554 } 1556 }
1555 outputLeaf(file, mode, leaf->right); 1557 if ((OM_LUA == mode) &&(ST_BITWISE != leaf->toKen->subType))
1558 outputLeaf(file, mode, leaf->right);
1556 } 1559 }
1557} 1560}
1558 1561
@@ -1857,6 +1860,37 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state
1857 } 1860 }
1858} 1861}
1859 1862
1863static void outputBitOp(FILE *file, outputMode mode, LSL_Leaf *leaf)
1864{
1865 if (OM_LSL == mode)
1866 outputLeaf(file, mode, leaf);
1867 else if (OM_LUA == mode)
1868 {
1869/*
1870swap = (integer)((string)pkey) & 1;
1871bit.band(swap= --[[(integer)]] ( --[[(string)]] pkey), 1) ;
1872*/
1873
1874 switch (leaf->toKen->type)
1875 {
1876 case LSL_BIT_AND : fprintf(file, " bit.band("); break;
1877 case LSL_BIT_OR : fprintf(file, " bit.bor("); break;
1878 case LSL_BIT_XOR : fprintf(file, " bit.xor("); break;
1879 case LSL_BIT_NOT : fprintf(file, " bit.bnot("); break;
1880 case LSL_LEFT_SHIFT : fprintf(file, " bit.lshift("); break;
1881 case LSL_RIGHT_SHIFT : fprintf(file, " bit.rshift("); break;
1882 default : break;
1883 }
1884 outputLeaf(file, mode, leaf->left);
1885 if (LSL_BIT_NOT != leaf->toKen->type)
1886 {
1887 fprintf(file, ", ");
1888 outputLeaf(file, mode, leaf->right);
1889 }
1890 fprintf(file, ") ");
1891 }
1892}
1893
1860static void outputBlockToken(FILE *file, outputMode mode, LSL_Leaf *content) 1894static void outputBlockToken(FILE *file, outputMode mode, LSL_Leaf *content)
1861{ 1895{
1862 if (content) 1896 if (content)
@@ -2223,6 +2257,7 @@ static boolean doneParsing(LuaSL_compiler *compiler)
2223 if (out) 2257 if (out)
2224 { 2258 {
2225 fprintf(out, "--// Pre declared helper stuff.\n"); 2259 fprintf(out, "--// Pre declared helper stuff.\n");
2260 fprintf(out, "local bit = require(\"bit\")\n");
2226 fprintf(out, "function preDecrement(x) x = x - 1; return x; end;\n"); 2261 fprintf(out, "function preDecrement(x) x = x - 1; return x; end;\n");
2227 fprintf(out, "function preIncrement(x) x = x + 1; return x; end;\n"); 2262 fprintf(out, "function preIncrement(x) x = x + 1; return x; end;\n");
2228 fprintf(out, "function postDecrement(x) x = x - 1; return x; end;\n"); 2263 fprintf(out, "function postDecrement(x) x = x - 1; return x; end;\n");