codeasm 0.3.0

Generate code form ASTs.
Documentation
  • Coverage
  • 15.89%
    24 out of 151 items documented0 out of 138 items with examples
  • Size
  • Source code size: 33.56 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 6.92 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 14s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ZJZCORE/codeasm
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ZJZCORE

codeasm

This library can translate AST into source code for multiple programming languages.

Currently, we support the following programming languages:

  • Go
  • Python

Examples

Go Example

use codeasm::go_asm::*;

let main_body = Block::new()
    .push(Expr::raw("fmt").attr("Println").call(["Hello World".into()]).into());
let pkg = Package::new("main")
    .push(Decl::import("fmt"))
    .push(Decl::func("main", [], [], main_body));
print!("{pkg}")

Generated code:

package main
import "fmt"
func main() {
    fmt.Println("Hello world!")
}

Python Example

use codeasm::py_asm::*;

let main_body = Block::new()
    .push(Expr::raw("print").call(["Hello World".into()], Vec::<(&str, _)>::new()).into());

let if_cond = Expr::raw("__name__").binop("==", "__main__".into());
let if_body = Block::new().push(Expr::raw("main").call([], Vec::<(&str, _)>::new()).into());

let file = File::new()
    .push(Stmt::func("main", DefArgs::new(), Type::unknow(), main_body))
    .push(Stmt::if_([(if_cond, if_body)], Block::new()));
print!("{file}")

Generated code:

def main():
    print("Hello World")

if (__name__ == "__main__"):
    main()