Skip to content

Commit

Permalink
fix:super+class
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangnanscu committed Jul 9, 2024
1 parent 1b7dfc5 commit a144ef8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 104 deletions.
6 changes: 0 additions & 6 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ module.exports = {
amd: true,
},
rules: {
"prettier/prettier": [
"warn",
{
printWidth: 120,
},
],
"max-len": ["warn", { code: 120, ignoreComments: true, ignoreStrings: true }],
"prefer-const": [
"error",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xiangnanscu/lua2js",
"version": "0.29.0",
"version": "0.30.0",
"main": "src/lua2js.mjs",
"type": "module",
"bin": {
Expand Down
92 changes: 15 additions & 77 deletions src/components/MainPage.vue
Original file line number Diff line number Diff line change
@@ -1,94 +1,28 @@
<script setup>
import { ref, computed, watch } from "vue";
import { lua2js, lua2ast, defaultOptions } from "../lua2js.mjs";
import luastring from "./code.lua?raw"
import fs from "file-saver";
const parseOptions = {};
const showLuacode = ref(null);
const showLuaAst = ref(false);
const quoteS = 'local x = [[`\\`\\\\`]]\n'
const luacode = ref(quoteS + `\
for i, e in ipairs(t) do
print(i, e)
end
for i, e in ipairs(t) do
print(e)
end
local a = dict({a=1}, b)
local a = list({1,2}, b)
print(a[1])
local a = {unpack(t)}
local function snake_case_name(x, y)
if x > 0 or y > 0then
return nil, string.format('error: x is %s and y is %s', x, y)
else
return x + y, x - y
end
end
console.log(string.format("hello %s", world))
console.log(string.format([[hello:
you are multiple line %s]], world))
type(x+1)
table.concat(t, ",")
table_concat(t, ",")
table_concat({1,2,3}, ",")
t[#t+1] = 1
table_insert(t,1,a)
table.insert(t,1,a)
table_insert(t, 1)
table.insert(t, 1)
local array = {1,2}
local dict = {a=1, b=2}
Test = class {
a = {1, 2}
}
local function foo(x, y)
return x + y
end
local c = {a=1}
function c.foo(x, y)
return x + y
end
function c.foo(self, x, y)
return x + y + self.n
end
function c:foo(x, y)
return x + y + self.n
end
local TestClass = class {
p1 = 'Hi class property p1',
p2 = 'Hi class property p2',
static_func = function(x, y)
return x + y
end,
class_method = function(cls)
cls:say_class_hi()
end,
say_class_hi = function(cls)
console.log(cls.p1)
end,
instance_method = function(self)
self:say_instance_hi()
end,
say_instance_hi = function(self)
console.log(this.p2)
end
}
`);
// luacode.value = `function foo.bar(self)
// end
// local Child = class({
// echo = function(self) end
// }, Parent)`
const luacode = ref(luastring);
const optionNames = Object.keys(defaultOptions);
const selectNames = ref(
Object.entries(defaultOptions)
.filter(([k, v]) => v)
.map(([k, v]) => k)
);
const selectOptions = computed(() => Object.fromEntries(selectNames.value.map((e) => [e, true])));
const jscode = computed(() => lua2js(luacode.value, selectOptions.value));
const selectOptions = computed(() => {
const opts = {}
selectNames.value.forEach((name) => (opts[name] = true));
optionNames.filter(name=>!selectNames.value.includes(name)).forEach(name=>{opts[name]=false});
return opts
});
const jscode = computed(() => {
return lua2js(luacode.value, selectOptions.value)
});
const luaast = computed(() => lua2ast(luacode.value, selectOptions.value));
const jscode_highlight_html = computed(() => hljs.highlight(jscode.value, { language: "js" }).value);
Expand Down Expand Up @@ -132,6 +66,10 @@ watch(checkAll, (checkAll) => {
selectNames.value = [];
}
});
watch(selectNames, (selectNames) => {
// force jscode re-render
luacode.value = luacode.value +' ';
})
</script>

<template>
Expand Down
67 changes: 67 additions & 0 deletions src/components/code.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
local x = [[`\`\\`]]
for i, e in ipairs(t) do
print(i, e)
end
for i, e in ipairs(t) do
print(e)
end
local a = dict({a=1}, b)
local a = list({1,2}, b)
print(a[1])
local a = {unpack(t)}
local function snake_case_name(x, y)
if x > 0 or y > 0then
return nil, string.format('error: x is %s and y is %s', x, y)
else
return x + y, x - y
end
end
console.log(string.format("hello %s", world))
console.log(string.format([[hello:
you are multiple line %s]], world))
type(x+1)
table.concat(t, ",")
table_concat(t, ",")
table_concat({1,2,3}, ",")
t[#t+1] = 1
table_insert(t,1,a)
table.insert(t,1,a)
table_insert(t, 1)
table.insert(t, 1)
local array = {1,2}
local dict = {a=1, b=2}
Test = class {
a = {1, 2}
}
local function foo(x, y)
return x + y
end
local c = {a=1}
function c.foo(x, y)
return x + y
end
function c.foo(self, x, y)
return x + y + self.n
end
function c:foo(x, y)
return x + y + self.n
end
local TestClass = class {
p1 = 'Hi class property p1',
p2 = 'Hi class property p2',
static_func = function(x, y)
return x + y
end,
class_method = function(cls)
cls:say_class_hi()
end,
say_class_hi = function(cls)
console.log(cls.p1)
end,
instance_method = function(self)
self:say_instance_hi()
end,
say_instance_hi = function(self)
console.log(this.p2)
end
}
41 changes: 21 additions & 20 deletions src/lua2js.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ import parserBabel from "prettier/parser-babel.js";
const defaultOptions = {
printToConsoleLog: true,
tryUseOfLoop: true,
indexMinusOne: true,
returnNilToThrow: true,
indexMinusOne: false,
returnNilToThrow: false,
errorToThrow: true,
tostring: true,
dict: true,
list: true,
dict: false,
list: false,
unpack: true,
tonumber: true,
class: true,
class: false,
selfToThis: true,
clsToThis: true,
typeToTypeof: true,
stringFormat: true,
tableConcat: true,
tableInsert: true,
stringFormat: false,
tableConcat: false,
tableInsert: false,
camelStyle: false,
};
function joinUnderscore(length) {
Expand Down Expand Up @@ -84,6 +84,7 @@ const IdentifierMap = {
constructor: "_constructor",
extends: "_extends",
class: "_class",
super: "_super",
default: "_js_default",
debugger: "_debugger",
};
Expand Down Expand Up @@ -299,7 +300,7 @@ function luaLiteral2Js(s) {
const head = s[0];
const res = getLuaStringToken(s);
if (head == "[") {
return "`" + res.replaceAll("\\", "\\\\").replaceAll("`", "\\`") + "`";
return "`" + res.replaceAll("\\", "\\\\").replaceAll("`", "\\`").replaceAll("$", "\\$") + "`";
} else {
return head + res + head;
}
Expand Down Expand Up @@ -450,7 +451,7 @@ function ast2js(ast, opts = {}) {
}
function _ast2js(ast) {
if (ast instanceof Array) {
return ast.map(_ast2js).join(";");
return ast.map(_ast2js).join(";\n");
}
switch (ast.type) {
case "Chunk":
Expand Down Expand Up @@ -573,11 +574,11 @@ function ast2js(ast, opts = {}) {
case "IfStatement":
return ast.clauses.map(_ast2js).join("\n");
case "IfClause":
return `if (${_ast2js(ast.condition)}) {${_ast2js(ast.body)}}`;
return `if (${_ast2js(ast.condition)}) {\n${_ast2js(ast.body)}\n}`;
case "ElseClause":
return `else {${ast.body.map(_ast2js).join(";")}}`;
return `else {\n${ast.body.map(_ast2js).join(";\n")}\n}`;
case "ElseifClause":
return `else if (${_ast2js(ast.condition)}) {${_ast2js(ast.body)}}`;
return `else if (${_ast2js(ast.condition)}) {\n${_ast2js(ast.body)}\n}`;
case "FunctionDeclaration":
tagVarargAsSpread(ast.parameters);
if (ast.isClassMode) {
Expand Down Expand Up @@ -617,7 +618,7 @@ function ast2js(ast, opts = {}) {
ast.parameters = ast.parameters.slice(1);
traverseAst(ast.body, clsToThis);
}
const main = `(${ast.parameters.map(_ast2js).join(", ")}){${_ast2js(ast.body)}}`;
const main = `(${ast.parameters.map(_ast2js).join(", ")}){\n${_ast2js(ast.body)}}`;
if (ast.identifier == null) {
return `function ${main}`;
} else {
Expand All @@ -641,17 +642,17 @@ function ast2js(ast, opts = {}) {
if (ast.asExport) {
return `export default ${smartPack(ast.arguments)}`;
} else {
return `return ${smartPack(ast.arguments)}`;
return `return ${smartPack(ast.arguments)};`;
}

case "CallStatement":
return _ast2js(ast.expression);
case "CallExpression":
if (ast.base.type == "Identifier" && ast.base.name == "class" && ast.className) {
if (opts.class && ast.base.type == "Identifier" && ast.base.name == "class" && ast.className) {
ast.arguments[0].isClassMode = true;
const extendsToken = ast.arguments.length == 1 ? "" : "extends " + _ast2js(ast.arguments[1]);
return `class ${ast.className} ${extendsToken} ${_ast2js(ast.arguments[0])}`;
} else if (isClassExtends(ast)) {
} else if (opts.class && isClassExtends(ast)) {
const [cls, pcls] = ast.arguments;
cls.isClassMode = true;
return `class ${_ast2js(cls)} extends ${_ast2js(pcls)}`;
Expand Down Expand Up @@ -733,9 +734,9 @@ function ast2js(ast, opts = {}) {
} else {
compare_op = step < 0 ? ">=" : "<=";
}
return `for (let ${v}=${start}; ${v} ${compare_op} ${_ast2js(ast.end)}; ${v}=${v}+${step}) {${_ast2js(
return `for (let ${v}=${start}; ${v} ${compare_op} ${_ast2js(ast.end)}; ${v}=${v}+${step}) {\n${_ast2js(
ast.body
)}}`;
)}\n}`;
}

case "ForGenericStatement": {
Expand All @@ -762,7 +763,7 @@ function ast2js(ast, opts = {}) {
} else {
iter = ast.iterators.map(_ast2js);
}
return `for (let ${smartPack(ast.variables)} of ${iter}) {${_ast2js(ast.body)}}`;
return `for (let ${smartPack(ast.variables)} of ${iter}) {\n${_ast2js(ast.body)}}`;
}
case "WhileStatement":
return `while (${_ast2js(ast.condition)}) {${_ast2js(ast.body)}}`;
Expand Down

0 comments on commit a144ef8

Please sign in to comment.