https://github.com/adam-mcdaniel/sage Skip to content Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} adam-mcdaniel / sage Public * Notifications * Fork 2 * Star 120 A programming language that's wise beyond its bytes! adam-mcdaniel.net/sage License MIT license 120 stars 2 forks Activity Star Notifications * Code * Issues 11 * Pull requests 1 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights adam-mcdaniel/sage This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 3 branches 3 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/a] Use Git or checkout with SVN using the web URL. [gh repo clone adam-m] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @adam-mcdaniel adam-mcdaniel Update README.md ... 3170df3 Jan 3, 2024 Update README.md 3170df3 Git stats * 511 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Changed name of cargo-test workflow June 14, 2023 00:15 assets Added to readme December 20, 2023 22:53 benches Cargo fmt December 1, 2023 16:39 docs Added to examples December 30, 2023 00:07 examples Added to examples December 30, 2023 00:07 src Added to comments December 29, 2023 02:05 tests Added to readmes December 20, 2023 23:28 .gitignore Added to benchmark September 22, 2023 10:57 CHANGELOG.md Version 0.0.3-alpha December 27, 2023 07:58 CORE.md Update CORE.md June 13, 2023 22:42 Cargo.toml Version 0.0.3-alpha December 27, 2023 07:58 LICENSE Changed to MIT license December 8, 2023 20:32 README.md Update README.md January 3, 2024 12:17 build-docs.dunesh Added to docs December 8, 2023 20:04 build.rs Added better formatting, fixed Apply typecheck, fixed union typecheck... September 16, 2022 21:03 View code [ ] The Sage Programming Language Table of Contents Community What is Sage? Why Sage? How useful is Sage? How do I use Sage? What does Sage look like? Feature Roadmap Where can I learn more? How do I contribute? About the Author README.md The Sage Programming Language Sage advice for your coding conundrums! [code1] [sage] Here's a link to the online compiler playground! Table of Contents * Community * What is Sage? * Why Sage? * How useful is Sage? * How do I use Sage? * What does Sage look like? * Feature Roadmap * Where can I learn more? * How do I contribute? * About the Author Community Join the Discord server to chat about Sage! Let us know if you have any thoughts or comments about the language! What is Sage? Sage is a programming language that tries to be maximally portable, expressive, and intuitive. It borrows some aspects of Rust, C, and Python. It currently has an x86 compiler backend, a C source backend, and a VM interpreter backend which can run on the web. [code2] [code1] [code3] Sage is licensed under the MIT License, and has been under development since April 2022. Why Sage? Sage is very portable -- run it on your thermostat! Here's the complete list of core virtual machine instructions and their C equivalents: Instruction C Equivalent while while (reg) { if if (reg) { else } else { end } set N reg = N; call funs[reg](); ret return; save *tape_ptr = reg; res reg = *tape_ptr; move N tape_ptr += N; where reg = tape_ptr; deref push(tape_ptr); tape_ptr = *tape_ptr; refer tape_ptr = pop(); index reg = (cell*)(reg) + *tape_ptr; add reg += *tape_ptr; sub reg -= *tape_ptr; mul reg *= *tape_ptr; div reg /= *tape_ptr; rem reg %= *tape_ptr; nand reg = ~(reg & *tape_ptr); gez reg = reg >= 0; You could fit this instruction set on a T-shirt! The compiler can target this limited "core" instruction set, with an expanded "standard" instruction set for floating point operations and foreign functions. The core instruction set is designed to be as simple as possible for anyone to implement their own backend. Try to see if you can implement it yourself for your backend of choice! The virtual machine has some important optimization properties: Although Sage's VM is a very simple zero-address-code representation, it preserves all the information to reconstruct an LLVM-like three-address-code representation of the original higher level IR. This makes the instruction set capable of applying LLVM's optimizations while being much easier to implement. Sage's innovation is in the backend, not the frontend. This combination of simplicity and capacity for optimization was my motivation for creating Sage. I wanted to create a virtual machine with the largest speed + expression + portability to implementation difficulty ratio, and a high level language that could compile to it. I think Sage is a good solution to this problem. This project is based on some ideas I had while working on Harbor for a hackathon. How useful is Sage? Sage is a very young project, and is not ready for production. It's still possible to write very useful programs in it, though. SageOS is an operating system with a userspace written in Sage. Its graphical shell and presentation app (both written in Sage) use the FFI to draw to the screen, receive input from the mouse and keyboard, interact with the filesystem, and schedule new processes. You can look at the shell code here. Shell1 Shell2 The presentation app parses PPM image files from the filesystem and renders them to the screen. You can look at the presentation code here. Presentation Sage's FFI is simple and can directly call C functions or backend-specific builtins. Check out the web-demo's FFI example that calls some JavaScript code to draw graphics or alert the user! How do I use Sage? To start using sage, install it with cargo: $ cargo install --git https://github.com/adam-mcdaniel/sage Then, you can run a sage file with the sage command: $ sage examples/frontend/interactive-calculator.sg You can also compile a sage file to C with the --target flag: $ sage examples/frontend/interactive-calculator.sg --target c $ # Or `-t c` for short $ sage examples/frontend/interactive-calculator.sg -tc $ gcc out.c -o out $ ./out Check out the code for the web-demo to see how to use Sage in a web page. What does Sage look like? Here's an example of a polymorphic linked list in Sage using Rust-like enums! It's straightforward to implement operations like map with just a few lines. [code1] Here's an example of Sage's structural typing: a Rectangle can be created by concatenating the fields of a Position and a Size! [code2] Here's an example of Sage's pattern matching: it's easy to deconstruct a value using match, if let, or a simple let binding. Sage's match expressions are very powerful! [code3] Go to the web-demo or the examples/frontend folder to see more code examples. Feature Roadmap * [*] Compiler Backends + [*] x86 (semi-implemented and unoptimized) + [ ] RISC-V + [ ] ARM + [ ] LLVM (highly desired!) + [*] C (fully-implemented but unoptimized) + [*] Interpreter (fully-implemented but unoptimized) + [*] Web Backend o [*] Interpreter o [ ] Visual demo like the web-demo for Harbor * [*] Static variables and constant expressions * [*] Conditional compilation * [*] Polymorphic functions * [*] Mutability checks * [*] Rust-like enums * [*] Pattern matching * [*] Structural typing * [*] Associated constants and methods * [*] Recursive polymorphic types * [ ] Iterators and list/vector/array comprehensions * [ ] Hindley-Milner type inference * [ ] VSCode extension (syntax highlighting, code completion, etc.) * [ ] Typeclasses * [ ] no-std implementation of compiler * [ ] Modules * [ ] A standard library + [ ] Type Reflection Module + [ ] Collections Module + [ ] Networking Module + [ ] Filesystem Module + [ ] Graphics Module + [ ] Audio Module + [ ] GUI Module + [ ] WebAssembly Module + [ ] Foreign Function Interface Module (create backend with .toml file) + [ ] Memory Management Module * [ ] Better frontend parser (switch to Nom?) * [ ] A package manager * [ ] AST Macros * [ ] C frontend (compile C to Sage VM) * [ ] Self-hosting implementation Where can I learn more? You can read my blog post (~20 minute read) about the programming language to learn more about the implementation! Join the Discord server to chat about Sage! How do I contribute? If you want to contribute, you can open an issue or a pull request. Adding backends for other architectures is a great way to contribute! We also need a VSCode syntax highlighting extension! About the Author I'm a 21 year old computer science graduate student at the University of Tennessee, Knoxville. Rust is my favorite language, and I've written many other compilers. This is the last project I started as a teenager, and I was the only author to touch any of the code up to version v0.0.2-alpha (12/25/2023)! I'm looking for work opportunities for Summer 2024 (after I finish my Masters degree), so if you're interested in hiring me, please reach out to me at amcdan23@vols.utk.edu! About A programming language that's wise beyond its bytes! adam-mcdaniel.net/sage Topics c rust mobile web frontend compiler portable pattern-matching x86-64 wasm algebraic-data-types polymorphism turing-tarpit structural-typing Resources Readme License MIT license Activity Stars 120 stars Watchers 5 watching Forks 2 forks Report repository Releases 3 Release v0.0.3-alpha Latest Dec 27, 2023 + 2 releases Contributors 2 * @adam-mcdaniel adam-mcdaniel adam mcdaniel * @bichanna bichanna Nobuharu Shimazu Languages * Rust 100.0% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.