mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
refactor
This commit is contained in:
@@ -42,7 +42,7 @@ export const map_destructuring_identifiers = (node, mapper) => {
|
|||||||
|
|
||||||
export const collect_imports = module => {
|
export const collect_imports = module => {
|
||||||
return uniq(
|
return uniq(
|
||||||
module.stmts
|
module.children
|
||||||
.filter(n => n.type == 'import')
|
.filter(n => n.type == 'import')
|
||||||
.filter(n => !n.is_external)
|
.filter(n => !n.is_external)
|
||||||
.map(n => n.full_import_path)
|
.map(n => n.full_import_path)
|
||||||
|
|||||||
@@ -532,7 +532,7 @@ const goto_definition = (state, index) => {
|
|||||||
} else {
|
} else {
|
||||||
let loc
|
let loc
|
||||||
if(d.module != null) {
|
if(d.module != null) {
|
||||||
const exp = map_find(state.parse_result.modules[d.module].stmts, n => {
|
const exp = map_find(state.parse_result.modules[d.module].children, n => {
|
||||||
if(n.type != 'export') {
|
if(n.type != 'export') {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/eval.js
20
src/eval.js
@@ -157,8 +157,8 @@ const codegen = (node, node_cxt, parent) => {
|
|||||||
} else if(node.type == 'do'){
|
} else if(node.type == 'do'){
|
||||||
return [
|
return [
|
||||||
// hoist function decls to the top
|
// hoist function decls to the top
|
||||||
...node.stmts.filter(s => s.type == 'function_decl'),
|
...node.children.filter(s => s.type == 'function_decl'),
|
||||||
...node.stmts.filter(s => s.type != 'function_decl'),
|
...node.children.filter(s => s.type != 'function_decl'),
|
||||||
].reduce(
|
].reduce(
|
||||||
(result, stmt) => result + (do_codegen(stmt)) + ';\n',
|
(result, stmt) => result + (do_codegen(stmt)) + ';\n',
|
||||||
''
|
''
|
||||||
@@ -850,7 +850,7 @@ const eval_statement = (s, scope, calls, context) => {
|
|||||||
if(s.type == 'do') {
|
if(s.type == 'do') {
|
||||||
const node = s
|
const node = s
|
||||||
// hoist function decls to the top
|
// hoist function decls to the top
|
||||||
const function_decls = node.stmts
|
const function_decls = node.children
|
||||||
.filter(s => s.type == 'function_decl')
|
.filter(s => s.type == 'function_decl')
|
||||||
.map(s => {
|
.map(s => {
|
||||||
const {ok, children, calls: next_calls} = eval_children(s, scope, calls, context)
|
const {ok, children, calls: next_calls} = eval_children(s, scope, calls, context)
|
||||||
@@ -872,10 +872,10 @@ const eval_statement = (s, scope, calls, context) => {
|
|||||||
|
|
||||||
const initial_scope = {...scope, ...hoisted_functions_scope}
|
const initial_scope = {...scope, ...hoisted_functions_scope}
|
||||||
|
|
||||||
const {ok, assignments, returned, stmts, calls: next_calls} = node.stmts.reduce(
|
const {ok, assignments, returned, children, calls: next_calls} = node.children.reduce(
|
||||||
({ok, returned, stmts, scope, calls, assignments}, s) => {
|
({ok, returned, children, scope, calls, assignments}, s) => {
|
||||||
if(returned || !ok) {
|
if(returned || !ok) {
|
||||||
return {ok, returned, scope, calls, stmts: [...stmts, s], assignments}
|
return {ok, returned, scope, calls, children: [...children, s], assignments}
|
||||||
} else if(s.type == 'function_decl') {
|
} else if(s.type == 'function_decl') {
|
||||||
const node = function_decls.find(decl => decl.index == s.index)
|
const node = function_decls.find(decl => decl.index == s.index)
|
||||||
return {
|
return {
|
||||||
@@ -885,7 +885,7 @@ const eval_statement = (s, scope, calls, context) => {
|
|||||||
assignments,
|
assignments,
|
||||||
scope,
|
scope,
|
||||||
calls,
|
calls,
|
||||||
stmts: [...stmts, node],
|
children: [...children, node],
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const {
|
const {
|
||||||
@@ -902,14 +902,14 @@ const eval_statement = (s, scope, calls, context) => {
|
|||||||
assignments: {...assignments, ...next_assignments},
|
assignments: {...assignments, ...next_assignments},
|
||||||
scope: nextscope,
|
scope: nextscope,
|
||||||
calls: next_calls,
|
calls: next_calls,
|
||||||
stmts: [...stmts, node],
|
children: [...children, node],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ok: true, returned: false, stmts: [], scope: initial_scope, calls, assignments: {}}
|
{ok: true, returned: false, children: [], scope: initial_scope, calls, assignments: {}}
|
||||||
)
|
)
|
||||||
const {node: next_node, scope: next_scope} =
|
const {node: next_node, scope: next_scope} =
|
||||||
apply_assignments({...node, children: stmts, result: {ok}}, assignments)
|
apply_assignments({...node, children: children, result: {ok}}, assignments)
|
||||||
return {
|
return {
|
||||||
ok,
|
ok,
|
||||||
node: next_node,
|
node: next_node,
|
||||||
|
|||||||
@@ -228,7 +228,7 @@ const node_has_toplevel_await = node => {
|
|||||||
export const check_imports = modules => {
|
export const check_imports = modules => {
|
||||||
// TODO allow circular imports
|
// TODO allow circular imports
|
||||||
return map_object(modules, (module, node) => {
|
return map_object(modules, (module, node) => {
|
||||||
const imports = node.stmts
|
const imports = node.children
|
||||||
.filter(n => n.type == 'import')
|
.filter(n => n.type == 'import')
|
||||||
.reduce(
|
.reduce(
|
||||||
(imports, n) => [
|
(imports, n) => [
|
||||||
|
|||||||
@@ -1438,10 +1438,7 @@ const update_children_not_rec = (node, children = node.children) => {
|
|||||||
is_statement: true,
|
is_statement: true,
|
||||||
}
|
}
|
||||||
} else if(node.type == 'do'){
|
} else if(node.type == 'do'){
|
||||||
return {...node,
|
return {...node, is_statement: true}
|
||||||
stmts: children,
|
|
||||||
is_statement: true,
|
|
||||||
}
|
|
||||||
} else if(node.type == 'function_decl'){
|
} else if(node.type == 'function_decl'){
|
||||||
return {...node,
|
return {...node,
|
||||||
is_statement: true,
|
is_statement: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user