plops / cl-golang-generator

Common Lisp to convert s-expressions to go language (works for some examples)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction

Common Lisp program that transforms s-expressions into Go language. It is currently still work in progress.

This is the sixth in a series of code generators.

  • https://github.com/plops/cl-cpp-generator I tried this generator with C/C++/ISPC/OpenCL/Cuda. It was my first attempt. The largest project with this is a firmware for a microcontroller. The difficult part is to get placement of semicolons right. I’m not terribly happy with the current solution. People don’t like that every function call has to be explicitly stated as such. Initially I thought it would help me to add function parameter completion in emacs. But I never figured out how to do that and in later code generators I simplified the lisp syntax.
  • https://github.com/plops/cl-ada-generator (abandoned) I always wanted to have a VHDL code generator and thought that ADA would be a good target to support a similar language. Unfortunately ADA and VHDL have a lot of syntactic constructs which I find difficult to express in Lisp semantics that I can remember. So I stopped working in this direction.
  • https://github.com/plops/cl-py-generator At a new job I generated LOTS of python code (75k lines) with this generator. The difficult part was to get indents right. It works really well. I should backport some features from cl-golang-generator: Variable type declaration, when, unless, unitary - and unitary /, logior, logand, incf, decf.
  • https://github.com/plops/cl-js-generator I played around with webrtc and webgl in the browser. I used parenscript before and I really like the way it upholds lisp semantics (every expression returns a value). However, the generated javascript can be difficult to read (and compare to s-expressions). For this project my goal was to have a very minimal mapping from s-expressions to javascript. Turns out converting lisp to javascript is very straightforward.
  • https://github.com/plops/cl-wolfram-generator (abandoned) At some point I was able to download a wolfram language license. I think this language is very close to Lisp. I tried some code generation but the free license would only work on one computer which is not how I work.

cl-golang-generator is based on cl-py-generator. Go doesn’t really need indenting (go fmt can fix this) and also doesn’t have lots of semicolons (like C). That makes the s-expression to golang conversion quite simple. The hardest part for this code generator is to support type declaration. I did the simplest possible thing and only search for declarations in the first forms of the body of let and defun. I really like that solution. It can even notice (most of the time) if a variable has a typo in the declaration.

Syntax

lispcommentexampleexpanded
ntuplecomma separated list(:= (ntuple val err) (bla))val, err := bla()
parencomma separated list in parens(paren 1 2 3)(1,2,3)
bracescomma separated list in braces(braces 1 2 3){1,2,3}
curlyfor instantiation(curly []int 1 2 3)[]int{1,2,3}
castlike a funcall without parens
dict
do0combine instructions
letvar declaration
assign”:=” declaration
”:=””:=” declaration use in for
defun
defun-declarationfor type declaration
lambdaunnamed function
defmethod
defmethod-interfacefor interface declaration
defstruct0struct declaration
deftype
definterface
setfseveral =
constseveral const declarations
if
wheneasier than if for multi form
unless
caseswitch statement
ecaselike case but without default
for
foreach
while
dotimes
not
”+,-,*,/”
logior, logand
or, and
”/=,^=,<=,!=,==’
<-
“<,<<,>>”
incf, decf
string
char
arefarray access
slice
dotcombine with dots

special instructions

  • like a function call but without parens: go, range, chan, defer, return

Description of the examples

  • The first examples are direct translations of Go Programming Language Book (Donovan, Kernighan, 2015). These have “gopl” in the name.
  • Then I tried to create a cross platform GUI with Fayne. My goal was to plot like Python xarray xrp.imshow. I’m not to impressed. Documentation of Fayne is quite limited and I couldn’t figure out how to get a mouse cursor.
  • I like the later gopl chapter about memoization and the grpc example I copied from some blog post.
  • I would like to have interactive reloading of code to be able to experiment with distributed systems. Unfortunately the go ecosystem isn’t that mature yet.
  • Next I would like to link to Cuda to offload computation to GPUs.

Known problems/ToDo:

  • Currently the example code generation relies on the repo being /home/martin/stage/cl-golang-generator. This can should be changed in each gen.lisp file by modifying the defparameter path. Of course this is unacceptable. I haven’t figured out how to get the repo path using asdf or defsystem.
  • I don’t quite understand how go directories should be layed out. I guess I should read golang-standards/project-layout at some point.
  • Check if all examples still work. I changed some s-expression semantics around example 07.
  • Implement test cases or use the examples.

References

Books

  • Donovan, Kernighan: Go Programming Language
    • introduces everything in the language
    • not enough practical advice
  • Ketelsen, St Martin, Kennedy: Go in Action
    • rss matcher
    • packaging, vendoring, gb reproducible build
    • interface, type embedding
    • unbuffered and buffered channels
    • concurrency patterns (looks useful), pool
    • logging, json
    • testing
  • Butcher, Farina: Go in Practice
    • json and yaml config files
    • coreos/etcd distributed shared config and service discovery
    • path matching
    • buffered channels
    • error, panic
    • debug, log
    • log to network (logstash, heka, 12factor app treat logs as event streams), handle back pressure
    • stack traces
    • unit testing, mocking, stubbing, interface canary test, parallel benchmark, detect race
    • webserver, handling assests, forms, post, multipart, incremental save
    • rest api, timout, versioning
    • multiple cloud providers, avoid lock-in
    • describe interface for cloud storage, handling errors
    • gather host information, cross-compile, runtime monitor
    • communication between services, grpc, protobuf (i think i like that)
    • reflection, tags in struct
  • Cox-Buday: Concurrency in Go: Tools and Techniques for Developers
    • https://katherine.cox-buday.com/blog/2018/07/18/the-utility-of-a-cup/ describes authors state of mind, fear is the mind killer
    • race
    • concurrency building blocks (looks good), once, pool, channels, select
    • sync package
    • benchmark
    • concurrency pattern, prevent goroutine leaks, or channel, pipeline, generators, fan-out, fan-in; or-done, tee- and bridge-channel, queuing, context package (i think that’s old)
    • prime finder
    • scale, error propagation, timeout, cancel, heartbeat, replicate requests, rate limit
    • runtime
  • Bischof: Das Netzbetriebssystem Plan 9
    • some examples of how to program with channels
    • 9P protocol, network database (ndb)
    • panel library (gui, cut & paste)
  • Hoare: Communicating Sequential Processes
  • Jones, Roscoe: Reflections on the Work of C.A.R Hoare
    • price of reliability is the utmost simplicity. it is the price that the very rich find most hard to pay
    • algol 68, PL/I, Ada
    • Ada: his advice and judgement largely went unheeded
    • occam: entities must not be multiplied more than necessary
  • Stanley-Marbell: Inferno programming with Limbo
    • http://doc.cat-v.org/inferno/books/inferno_programming_with_limbo/Inferno_Programming_With_Limbo.pdf
    • limbo has slices, modules, :=
    • types separated with colon
    • still uses semicolons
    • cyclic keyword in structs (adt)
    • dynamic module loading, self-referential modules
    • threads, channels, alt (like go select), pipeline
    • synthetic file, file2chan
    • file server
    • SPIN, promela protocol meta language
    • modeling, verification of concurrent applications
    • generates diagrams with dot
    • distribution of resources in the network
    • styx rpc protocol: Attach, Clone, Walk, Clunk, Stat, Wstat, Open, Create, Read, Write, Remove, Nop, Flush, Error
    • much simpler than nfs
    • mount driver: system calls to styx messages
    • server registry: filesystem operations to channel in user-level application
    • filesystem filter example
    • http server
    • web service
    • styx traffic through http
    • crypto, certificates, key ring
    • graphics
    • Point, Rect, Context, Display, Screen, Image, Tk, Wmlib
    • about the author:

VSCode

Installing 7 tools at /home/martin/go/bin in module mode.
  gotests
  gomodifytags
  impl
  goplay
  dlv
  staticcheck
  gopls

Installing github.com/cweill/gotests/gotests@latest (/home/martin/go/bin/gotests) SUCCEEDED
Installing github.com/fatih/gomodifytags@latest (/home/martin/go/bin/gomodifytags) SUCCEEDED
Installing github.com/josharian/impl@latest (/home/martin/go/bin/impl) SUCCEEDED
Installing github.com/haya14busa/goplay/cmd/goplay@latest (/home/martin/go/bin/goplay) SUCCEEDED
Installing github.com/rogpeppe/godef@latest (/home/martin/go/bin/godef) SUCCEEDED
Installing github.com/go-delve/delve/cmd/dlv@latest (/home/martin/go/bin/dlv) SUCCEEDED
Installing honnef.co/go/tools/cmd/staticcheck@latest (/home/martin/go/bin/staticcheck) SUCCEEDED
Installing golang.org/x/tools/gopls@latest (/home/martin/go/bin/gopls) SUCCEEDED
Installing github.com/ramya-rao-a/go-outline@latest (/home/martin/go/bin/go-outline) SUCCEEDED

About

Common Lisp to convert s-expressions to go language (works for some examples)


Languages

Language:Common Lisp 99.6%Language:Shell 0.4%