Saturday, December 23, 2023

Go AST <=> JSON

Go lang compiler is written in Go, so parsing source code and AST are available as packages.
Getting AST is "one line of code"
But the AST is using recursive pointers, and serializing to JSON requires some extra effort.

Here is a useful tool for that:

asty-org/asty: Converts golang AST to JSON and JSON to AST @GitHub

AST → JSON | JSON → AST

Marshals golang AST into JSON and unmarshals it back from JSON.

It allows building pattern matching, statistical analysis, language transformation, search/data-mine/anything algorithms for golang with any other language (I like to do it with python. Check out asty-python)


Demo:

Go-to-AST-JSON: Convert Go to AST-JSON instantly


how it works

Analyzing AST in Go with JSON tools - DEV Community


Basic AST Traversal in Go - zupzup


Here is a minimalistic example of using Go AST

package main

import (
    "fmt"
    "go/ast"
    "go/parser"
    "go/token"
    "os"
)

func main() {
    if len(os.Args) < 2 {
        fmt.Println("Please provide a filename as a command line argument.")
        os.Exit(1)
    }
    filename := os.Args[1]

    // read source from text file
    data, err := os.ReadFile(filename)
    if err != nil {
        fmt.Println("Error reading file:", err)
        os.Exit(1)
    }
    src := string(data)
    fmt.Println(src)

    // Initialize a new token file set
    fset := token.NewFileSet()
    // Parse the source code
    f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
    if err != nil {
        fmt.Println(err)
        return
    }
    // Print the AST
    ast.Print(fset, f)
}

Abstract syntax tree - Wikipedia

An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurring in the text. It is sometimes called just a syntax tree.

AI + React: v0 by Vercel, with Tailwind CSS

v0 by Vercel

Introducing v0: Generative UI - YouTube

v0: Vercel's AI-Powered React Component Creator
The AI-powered v0 looked fascinating on its release, but has been stuck behind a waitlist till now. Vercel has dropped that list, opening it to all users. Even Larry David would admit it's pretty, pretty good.

Announcing v0: Generative UI – Vercel

Here’s how it works:Describe the interface you want to build
v0 produces code using open-source tools like React, Tailwind CSS, and Shadcn UI
Select an iteration and keep editing in v0
When you're ready, copy and paste that code into your app and develop from there

v0 FAQ

v0 is a generative user interface system by Vercel powered by AI. It generates copy-and-paste friendly React code based on shadcn/ui and Tailwind CSS that people can use in their projects.