Semi-formal AI-Guided Engineering Language
SAGE is a 3-level specification language designed for human-AI collaboration. Write specs at your comfort levelβfrom pure natural language to formal mathematical specificationsβand let AI assistants generate precise implementations.
π― Why SAGE?
- 38% fewer tokens than natural language prompts
- Reduces ambiguity with structured contracts and types
- Better code quality through explicit requirements and constraints
- Scales with your needs from quick prototypes to mission-critical systems
This is the pure Lean 4 implementation providing a complete compiler pipeline with LSP integration for VS Code.
# 1. Clone and build
git clone https://github.com/lememta/sage-lang
cd sage-lang
./build.sh
# 2. Try the compiler
.lake/build/bin/sage examples/level1-structured.sage
# 3. Run tests
.lake/build/bin/test
# 4. Install VS Code extension
./scripts/setup-vscode-lsp.shThat's it! Open any .sage file in VS Code for syntax highlighting and real-time diagnostics.
sage-lang/
βββ lakefile.lean # Lake build configuration
βββ Sage.lean # Main module exports
βββ Main.lean # CLI compiler entry point
βββ MainLSP.lean # LSP server entry point
βββ Test.lean # Test framework and runner
βββ build.sh # Build script
βββ Sage/ # Core compiler components
β βββ Token.lean # Token types (60+ token definitions)
β βββ Lexer.lean # Lexical analyzer
β βββ AST.lean # Abstract syntax tree
β βββ Parser.lean # Recursive descent parser
β βββ TypeCheck.lean # Semantic analysis & validation
β βββ LSP/ # Language Server Protocol
β βββ Types.lean # LSP protocol types
β βββ Analysis.lean # SAGE code analysis
β βββ Server.lean # LSP server implementation
βββ Test/ # Comprehensive test suites
β βββ Lexer.lean # Lexer component tests
β βββ Parser.lean # Parser component tests
β βββ TypeChecker.lean # Type checker tests
β βββ Integration.lean # End-to-end pipeline tests
βββ vscode-extension/ # VS Code integration
βββ examples/ # Example SAGE specifications
βββ scripts/ # Development tools
./build.shThis builds:
sage- The SAGE compiler CLIsage-lsp- The SAGE Language Servertest- The comprehensive test suite
# Build compiler only
lake build sage
# Build LSP server only
lake build sage-lsp
# Build test suite only
lake build test
# Clean build artifacts
lake clean.lake/build/bin/sage examples/level1-structured.sageThe LSP server is designed to be used with VS Code. See the VS Code extension setup below.
To run manually:
.lake/build/bin/sage-lspFrom the repository root:
./scripts/setup-vscode-lsp.shThis will:
- Build the LSP server
- Install extension dependencies
- Install the extension to VS Code
Then restart VS Code.
-
Build the LSP server:
lake build sage-lsp
-
Install the extension:
cp -r vscode-extension ~/.vscode/extensions/sage-lang-lsp-0.1.0 -
Restart VS Code
The LSP extension provides:
- Syntax highlighting - Color coding for SAGE constructs
- Real-time diagnostics - Parse and type errors as you type
- File watching - Automatic updates when files change
The implementation follows a standard compiler pipeline:
- Lexer (
Sage/Lexer.lean) - Tokenizes source text - Parser (
Sage/Parser.lean) - Builds AST from tokens - Type Checker (
Sage/TypeCheck.lean) - Validates types and semantics - LSP Server (
Sage/LSP/Server.lean) - Provides IDE integration
To add new language features:
- Add token types to
Sage/Token.lean - Update lexer in
Sage/Lexer.lean - Add AST nodes to
Sage/AST.lean - Update parser in
Sage/Parser.lean - Add type checking rules in
Sage/TypeCheck.lean - Update LSP analysis in
Sage/LSP/Analysis.lean
# Run comprehensive test suite
.lake/build/bin/testThis runs all tests including:
- Sample framework tests
- Lexer component tests
- Parser component tests
- Type checker tests
- Integration tests (end-to-end pipeline)
# Test the compiler with example files
.lake/build/bin/sage examples/level0-natural.sage
.lake/build/bin/sage examples/level1-structured.sage
.lake/build/bin/sage examples/level2-formal.sage
# Test LSP (requires VS Code)
# Open a .sage file and check for diagnostics- β Token definitions (60+ token types)
- β AST structure
- β Full lexer implementation
- β Complete parser implementation
- β Type checker with semantic validation
- β Comprehensive test framework
- β LSP server with JSON-RPC
- β VS Code extension integration
- β Real-time diagnostics
- π§ Advanced type inference
- π§ Contract verification
- β³ Code completion
- β³ Go to definition
- β³ Find references
- β³ Hover information
- β³ Code actions
- β³ Formal verification support
Source Code (.sage)
β
Lexer (tokenize)
β
Tokens
β
Parser (parse)
β
AST (Program)
β
Type Checker (typeCheck)
β
Validated Program
The LSP server runs as a separate process and communicates with VS Code via JSON-RPC over stdio:
VS Code ββ JSON-RPC ββ sage-lsp ββ SAGE Compiler
The server maintains document state and provides diagnostics on:
- File open (
textDocument/didOpen) - File change (
textDocument/didChange) - Diagnostic request (
textDocument/diagnostic)
-
Check the server is built:
ls -la lean/.lake/build/bin/sage-lsp
-
Check VS Code output:
- Open VS Code
- View β Output
- Select "SAGE Language Server" from dropdown
-
Test server manually:
echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}' | .lake/build/bin/sage-lsp
-
Check extension is installed:
ls -la ~/.vscode/extensions/sage-lang-lsp-0.1.0 -
Check for errors:
- Help β Toggle Developer Tools
- Check Console tab for errors
-
Reload VS Code:
- Cmd+Shift+P β "Developer: Reload Window"
When contributing to the Lean implementation:
- Follow Lean 4 style guidelines
- Add documentation comments for public functions
- Test with example
.sagefiles - Update this README if adding new features