Skip to content

Module#21

Merged
jpksh90 merged 3 commits into
mainfrom
module_ast
Nov 10, 2025
Merged

Module#21
jpksh90 merged 3 commits into
mainfrom
module_ast

Conversation

@jpksh90

@jpksh90 jpksh90 commented Nov 10, 2025

Copy link
Copy Markdown
Owner

This pull request refactors the internal representation of programs in the Slang language compiler and runtime. The main change is the introduction of a new SlangModule type to encapsulate top-level functions and inlined functions, along with a synthetic __module__main__ function that contains all top-level statements. This improves modularity and prepares the codebase for future multi-module support. Several components—AST, CFG builder, interpreter, and UI—are updated to work with this new structure, and tests are adjusted to match the new output format.

Core IR and AST changes

  • Introduced the SlangModule class to group top-level functions and inlined functions, and updated ProgramUnit to hold a list of modules instead of statements. The IR builder now wraps top-level statements into a synthetic __module__main__ function inside a module. [1] [2]
  • Updated pretty-printing and tree visualization to support modules, so output and UI now show functions grouped under modules and display the synthetic main function. [1] [2]

Control Flow Graph (CFG) and Interpreter updates

  • Refactored the CFG builder to operate on modules, generating the CFG for the synthetic main function when present, and properly handling break/continue propagation in control flow. [1] [2] [3]
  • Updated the interpreter to register all top-level functions in the environment and execute the synthetic module main body, ensuring correct execution of top-level statements.

Test and snapshot updates

  • Adjusted test code and snapshot outputs to reflect the new module structure and pretty-printed format, replacing the previous raw AST dump with readable code-like output for each test case. [1] [2] [3] [4] [5] [6] [7]

Miscellaneous

  • Cleaned up unused imports in Ast.kt and added necessary imports for module support in the UI. [1] [2]
  • Simplified the snapshot approval script to operate non-recursively on a given directory and drop git commit logic, making it easier to use for local snapshot management.

Copilot AI review requested due to automatic review settings November 10, 2025 17:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the intermediate representation (IR) structure by introducing a SlangModule abstraction to better organize top-level code and functions.

Key Changes:

  • Introduced SlangModule to wrap top-level functions and inline functions, with a synthetic __module__main__ function containing top-level statements
  • Updated the interpreter, CFG builder, and test infrastructure to work with the new module-based structure
  • Modified test snapshots to show pretty-printed output instead of raw AST structure

Reviewed Changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/main/kotlin/slang/hlir/Ast.kt Added SlangModule data class and updated ProgramUnit to contain a list of modules instead of statements
src/main/kotlin/slang/hlir/AstBuilder.kt Modified visitCompilationUnit to wrap top-level code in a SlangModule with a synthetic __module__main__ function
src/main/kotlin/slang/runtime/Interpreter.kt Updated interpreter to register module functions and execute the synthetic module main body
src/main/kotlin/slang/hlir/ControlFlowGraph.kt Enhanced CFG builder to extract and build CFG for the synthetic module main function, with improved break/continue handling
src/test/kotlin/DataflowAnalysisTest.kt Updated to access functions through the new module structure using flatMap { it.functions }
src/test/kotlin/IRBuilderTests.kt Changed to use prettyPrint() instead of toString(), but discards error messages in the error case
src/main/kotlin/slang/ui/AstVisualizer.kt Added tree node rendering for SlangModule in the AST visualizer
build.gradle.kts Modified approveSnapshots task to require a test path parameter instead of a commit flag
scripts/approve_snapshots.sh Simplified script to rename files in a specified directory instead of git operations
src/test/kotlin/IRBuilderTests.testCase*.approved.txt Updated snapshot files to show pretty-printed IR instead of raw AST representation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

val topLevelInlineFuncs = stmts.filterIsInstance<Stmt.ExprStmt>().map { it.expr }
.filterIsInstance<Expr.InlinedFunction>()
val topLevelfuncs = stmts.filterIsInstance<Stmt.Function>()
val slangModule = SlangModule(listOf(moduleMain) + topLevelfuncs, topLevelInlineFuncs )

Copilot AI Nov 10, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Extra space before closing parenthesis. The code has topLevelInlineFuncs ) but should be topLevelInlineFuncs) for consistent formatting.

Suggested change
val slangModule = SlangModule(listOf(moduleMain) + topLevelfuncs, topLevelInlineFuncs )
val slangModule = SlangModule(listOf(moduleMain) + topLevelfuncs, topLevelInlineFuncs)

Copilot uses AI. Check for mistakes.
Comment thread src/main/kotlin/slang/hlir/Ast.kt Outdated
val stmt: List<SlangModule>,
) : SlastNode()

data class SlangModule(val functions : List<Stmt.Function>, val inlinedFuncs : List<Expr.InlinedFunction>) : SlastNode()

Copilot AI Nov 10, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Inconsistent spacing in property declaration. There's an extra space in val functions : List<Stmt.Function> - should be val functions: List<Stmt.Function> (no space before the colon). This is inconsistent with Kotlin style conventions.

Suggested change
data class SlangModule(val functions : List<Stmt.Function>, val inlinedFuncs : List<Expr.InlinedFunction>) : SlastNode()
data class SlangModule(val functions: List<Stmt.Function>, val inlinedFuncs : List<Expr.InlinedFunction>) : SlastNode()

Copilot uses AI. Check for mistakes.
Comment thread src/main/kotlin/slang/hlir/Ast.kt Outdated
val stmt: List<SlangModule>,
) : SlastNode()

data class SlangModule(val functions : List<Stmt.Function>, val inlinedFuncs : List<Expr.InlinedFunction>) : SlastNode()

Copilot AI Nov 10, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Inconsistent spacing in property declaration. There are extra spaces in val inlinedFuncs : List<Expr.InlinedFunction> - should be val inlinedFuncs: List<Expr.InlinedFunction> (no spaces before the colon). This is inconsistent with Kotlin style conventions.

Suggested change
data class SlangModule(val functions : List<Stmt.Function>, val inlinedFuncs : List<Expr.InlinedFunction>) : SlastNode()
data class SlangModule(val functions : List<Stmt.Function>, val inlinedFuncs: List<Expr.InlinedFunction>) : SlastNode()

Copilot uses AI. Check for mistakes.
Comment thread src/test/kotlin/IRBuilderTests.kt Outdated
Comment on lines +17 to +18
{it.prettyPrint(0)},
{""}

Copilot AI Nov 10, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error messages are being silently discarded. When the HLIR transformation fails (returns Result.Err), this code returns an empty string "", losing all error information. This causes tests like testCase8 (which expects error output about variable redeclaration) to produce empty output instead of showing the actual errors. Consider returning a formatted error message instead, e.g., {errors -> errors.joinToString("\n") { it.toString() }}.

Suggested change
{it.prettyPrint(0)},
{""}
{ it.prettyPrint(0) },
{ errors -> errors.joinToString("\n") { it.toString() } }

Copilot uses AI. Check for mistakes.
@sonarqubecloud

Copy link
Copy Markdown

@jpksh90 jpksh90 merged commit 1497052 into main Nov 10, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants