GithubHelp home page GithubHelp logo

yoshikitakashima / walkngo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from raff/walkngo

0.0 0.0 0.0 193 KB

A "walker" for Go AST

License: MIT License

C++ 5.35% Go 92.91% Makefile 0.21% sed 1.25% Shell 0.27%

walkngo's Introduction

walkngo

A "walker" for Go AST

This is the "skeleton" for something that can parse a Go source file and print it in some other format or, in any case, a way to understand how Go AST works.

The walker package contains the AST walker/visitor, the printer package contains modules that can "print out" the tree as different (probably broken) languages.

  • The "GoPrinter" module generates a Go source that compile and should work just as good as the original.
  • The "CPrinter" module tries to convert the Go source file to C (actually C++).
  • There is also a "DebugPrinter" module that wraps a real "printer" module but prints out method calls and parameters (enabled via --debug-printer).

The main program accepts a --lang argument to select the output language (c for C++, go and rust)

walkngo --lang=c walkngo.go

Usage:

walkngo [--lang=c|go|rust] [--debug] [--debug-printer] [--outdir={output-folder}] file.go|folder

Where:

  • --lang={lang} : convert the Go source files to the specified language
  • --debug : print out AST nodes for debugging
  • --debug-printer : print out calls to Printer methods
  • --outdir={output-folder} : creates output files in output-folder following original paths

If a folder is specified as input, the program will "walk" the directory structure and convert all files with extension ".go" (it skips folders with name starting with ".")

Notes:

When running with "go run" you should use:

go run walkngo.go -- go-source-file.go

The '--' tells 'go run' to stop looking for go files, otherwise it gets utterly confused.

If you build it you can just run:

walkngo go-source-file.go

Runtime:

The "runtime" folder contains the implementation of some Go runtime and common modules that the language translator can call.

For C++ there is some support for goroutines (via C++11 threads) and channels (C++11 queue, mutex, condition variables) and some initial implementations of the fmt, time and sync modules.

Also, multiple initializations and multiple return values are implemented using C++11 tuples (make_tuple and tie).

Note that the current implementation is very basic, just to verify that things work more or less as expected.

TODO:

  • Slices
  • Variable initialization: in go all variables are initizialized to their "zero value". In C/C++ they are whatever they are.
  • Module initialization: in go each module/file can have an init() method, that is called when the module is imported.
  • recover: panic is currently implemented as a method that causes a NPE. It should be implemented as a method throwing a Panic exception and the recover method can catch it (Would it work with defer ?).
  • named return values: right now the name in the method declaration is commented out so that it doesn't generate an error.It should be possible to add these as variable inside the body, so that they can be properly referenced, and then make sure that a return with no parameters is changed to a return with those variables.
  • select on channels: not sure of what to do here.
  • range statement: added initial support for range on lists. range on maps doesn't work completely and range on channel is missing (but it could potentially be implemented by adding an iterator to Chan ?

walkngo's People

Contributors

raff avatar yoshikitakashima avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.