Skip to content

Commit

Permalink
Semantic: main name is not configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 30, 2024
1 parent 9cd7ed4 commit d62cf31
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
10 changes: 3 additions & 7 deletions src/codegen/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ export interface JsImport {
path: string
}

export const emitModule = (module: Module, ctx: Context, mainFn?: string): EmitNode => {
export const emitModule = (module: Module, ctx: Context, main: boolean = false): EmitNode => {
const statements = emitTree(
module.block.statements.map(s => emitStatement(s, module, ctx)).map(s => ('resultVar' in s ? s.emit : s))
)
const mainFnInvoke =
mainFn !== undefined
? emitToken(
`try{${mainFn}();}catch(e){console.error(\`\${e.message}\\n\${e.stack.split("\\n").slice(1).join("\\n")}\`);}`
)
: undefined
const mainStr = `try{main();}catch(e){console.error(\`\${e.message}\\n\${e.stack.split("\\n").slice(1).join("\\n")}\`);}`
const mainFnInvoke = main ? emitToken(mainStr) : undefined
const imports = emitImports(module, ctx)
return emitTree([imports, statements, mainFnInvoke])
}
Expand Down
17 changes: 3 additions & 14 deletions src/package/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Package } from '.'
import { emitDeclaration } from '../codegen/declaration'
import { emitModule } from '../codegen/js'
import { Context } from '../scope'
import { findMain } from '../scope/util'
import { createSourceMap, foldEmitTree } from '../sourcemap'

export const emitPackage = async (isDir: boolean, pkg: Package, ctx: Context): Promise<void> => {
Expand Down Expand Up @@ -41,13 +42,7 @@ export const emitPackage = async (isDir: boolean, pkg: Package, ctx: Context): P
.toString()
.replace(/^[ \t]*(\/\/|\/\*|\*).*\s/gm, '')
: ''
// TODO: custom main module and main fn name
const mainFn =
m.mod && m.identifier.names.length === 1
? m.block.statements.find(s => s.kind === 'fn-def' && s.pub && s.name.value === 'main')
: undefined
const mainFnName = mainFn?.kind === 'fn-def' ? mainFn.name.value : undefined
const emitNode = emitModule(m, ctx, mainFnName)
const emitNode = emitModule(m, ctx, findMain(m) !== undefined)
const { emit, map } = foldEmitTree(emitNode)

const sourceMapLink = `//# sourceMappingURL=${moduleOutPath.name}.js.map`
Expand Down Expand Up @@ -75,13 +70,7 @@ export const emitPackage = async (isDir: boolean, pkg: Package, ctx: Context): P
await Promise.all(ps)
} else {
const m = pkg.modules[0]
// TODO: custom main module and main fn name
const mainFn =
m.mod && m.identifier.names.length === 1
? m.block.statements.find(s => s.kind === 'fn-def' && s.pub && s.name.value === 'main')
: undefined
const mainFnName = mainFn?.kind === 'fn-def' ? mainFn.name.value : undefined
const emitNode = emitModule(m, ctx, mainFnName)
const emitNode = emitModule(m, ctx, findMain(m) !== undefined)
const { emit } = foldEmitTree(emitNode)
const jsPath = join(ctx.config.outPath, parse(ctx.config.pkgPath).name) + '.js'
await writeFile(jsPath, emit).then(() => console.info(`emit: js ${jsPath} [${emit.length}B]`))
Expand Down
9 changes: 9 additions & 0 deletions src/scope/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Module } from '../ast'
import { Identifier } from '../ast/operand'
import { Statement } from '../ast/statement'
import { VirtualIdentifier } from './vid'

export const vidFromString = (str: string): VirtualIdentifier => ({ names: str.split('::') })
Expand All @@ -20,3 +22,10 @@ export const idToVid = (id: Identifier): VirtualIdentifier => ({ names: id.names
export const concatVid = (a: VirtualIdentifier, b: VirtualIdentifier): VirtualIdentifier => ({
names: [...a.names, ...b.names]
})

export const findMain = (module: Module): Statement | undefined => {
if (module.mod && module.identifier.names.length === 1) {
return module.block.statements.find(s => s.kind === 'fn-def' && s.pub && s.name.value === 'main')
}
return undefined
}

0 comments on commit d62cf31

Please sign in to comment.