Git Product home page Git Product logo

watson's Introduction

.github/workflows/main.yml Quicklisp

watson

watson: WAT (WebAssembly Text format) Structured ON Lisp

Usage

Simple case

(ql:quickload :watson)

(defpackage :sample-package
  (:use :cl
        :watson))
(in-package :sample-package)

(defimport.wat log console.log (func ((i32))))

(defun.wat main () ()
  (let (((x i32) 5))
    (log (factorial x))))

(defun.wat factorial ((x i32)) (i32)
  (let ((result i32))
    (if (i32.ge-u 1 x)
        (set-local result 1)
        (set-local result (i32.mul x (factorial (i32.sub x 1)))))
    (get-local result)))

(defexport.wat exported-func (func main))


(let ((*print-pretty* t))
  (princ (generate-wat-module *package*)))

The result is the following.

(module (import "console" "log" (func $LOG (param i32)))
 (func $MAIN (local $X i32) (set_local $X (i32.const 5))
  (call $LOG (call $FACTORIAL (get_local $X))))
 (func $FACTORIAL (param $X i32) (result i32) (local $RESULT i32)
  (if (i32.ge_u (i32.const 1) (get_local $X))
   (then (set_local $RESULT (i32.const 1)))
   (else
    (i32.mul (get_local $X)
     (call $FACTORIAL (i32.sub (get_local $X) (i32.const 1))))
    (set_local $RESULT)))
  (get_local $RESULT))
 (export "exported_func" (func $MAIN)))

Cf. Format above by hand

(module
  (import "console" "log" (func $LOG (param i32)))
  (func $MAIN
    (local $X i32)
    (set_local $X (i32.const 5))
    (call $LOG (call $FACTORIAL (get_local $X))))
  (func $FACTORIAL (param $X i32) (result i32)
    (local $RESULT i32)
    (if (i32.ge_u (i32.const 1) (get_local $X))
      (then (set_local $RESULT (i32.const 1)))
      (else (i32.mul (get_local $X)
                     (call $FACTORIAL (i32.sub (get_local $X) (i32.const 1))))
            (set_local $RESULT)))
    (get_local $RESULT))
  (export "exported_func" (func $MAIN)))

Macro

You can define and use macros.

(ql:quickload :watson)

(defpackage :sample-wat-macro
  (:use :cl
        :watson))
(in-package :sample-wat-macro)

(defimport.wat log console.log (func ((i32))))

(defmacro.wat incf-i32 (place &optional (added 1))
  `(set-local ,place (i32.add ,place ,added)))

(defun.wat main () ()
  (let (((x i32) 5))
    (incf-i32 x)
    (log x)))

(defexport.wat exported-func (func main))

(let ((*print-pretty* t))
  (princ (generate-wat-module *package*)))

The result is the following (formatted by hand).

(module
  (import "console" "log" (func $LOG (param i32)))
  (func $MAIN (local $X i32)
    (set_local $X (i32.const 5))
    (set_local $X (i32.add (get_local $X) (i32.const 1)))
    (call $LOG (get_local $X)))
  (export "exported_func" (func $MAIN)))

Installation

> (ql:quickload :watson)

TODO

Author

Copyright

Copyright (c) 2020 eshamster ([email protected])

License

Licensed under the MIT License.

watson's People

Contributors

eshamster avatar

Stargazers

Neos21 avatar Kyure_A avatar cxxxr avatar  avatar Ákos Kiss avatar hikettei avatar Bruno Dias avatar Alexander Artemenko avatar Masatake YAMATO avatar Wen-Chun Lin avatar Satoshi Imai avatar Takeshi Tsukamoto avatar Pa Mu Selvakumar avatar

Watchers

Sergey Katrevich avatar James Cloos avatar  avatar  avatar Ákos Kiss 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.