Giter Club home page Giter Club logo

milasm's Introduction

MILAsm

TL&DR

This is a readable Assembly dialect made of prolog-like pattern matching macros for CLR ILAsm.

Write this:

((write: "Hello, world!"))

Instead of that:

ldstr "Hello, world!"
call void [mscorlib]System.Console::Write (string)

The macrosystem is just a proof of concept, but it works: you can build the code, write your own macros, write your own CLR applications.

An example: hello.il

<<console_input_output.min>>
<<branching.min>>
<<instructions.min>>

.assembly extern mscorlib {}

.assembly hello
{
    .ver 1:0:1:0
}

.module hello.exe

.method static void main() cil managed
{
        .maxstack 2
        .entrypoint
    
        .locals init (int32 a, int32 b)

        ((write: "Input positive x: "))
        ((read line))
        ((parse it as int32))
        ((store it as local a))
        ((check if ((local a)) > ((int32 0)) ))
        ((if so {{
                    ((write: "It is positive!\n")) 
                    ((load int32 2))
                    ((store it as local b))
                     
                    ((write: "x * 2 = "))

                    ((load local a))
                    ((load local b))
                    ((multiply them))

                    ((write int32))      
                    ((write line: ""))      
            }}else{{
                    ((write: "It isn't positive.\n"))
            }} 
        ))
        ((read line))           
        ((return))
}

This piece of code is a valid ILAsm code with MILAsm macros. Not a mock-up.

It reads a number from the console, checks if it's positive, and if it is, returns the double of this number. What's important, you can read the intention from the code yourself even if you don't know any of CLR assembly. The langauge is meant to be both low-level and readable.

How it works?

Macros are defined in free-form syntax using double square brackets, like this:

[[write $type -> call void [mscorlib]System.Console::Write ($type) ]]

The macro expansion works like this: the part before the -> is a pattern, and the part after the arrow is the substitution. $type is a macro argument. All the words starting from $ are macro arguments. When a pattern occurs in the code, it's being replaced recursively until no recursive patterns are left.

ldstr "Hello!"
((write string))

Becomes:

ldstr "Hello!"
call void [mscorlib]System.Console::Write (string)

Of course, you can multiline the body of a macro like this:

[[write: $text -> 
    ldstr $text 
    call void [mscorlib]System.Console::Write (string) 
]]

And you can make recursive macros:

[[write: $text -> 
    ldstr $text 
    ((write string))
]]

And with this new macro, you can turn this:

ldstr "Hello!"
call void [mscorlib]System.Console::Write (string)

To that:

((write: "Hello!"))

When you want to write a macro with the block of other macros or some ILAsm code as a macros variable, use double curly brackets: {{ }}. You can make macros for if or for with that.

Note that apart from the parenthesis, the syntax of all the macros is non-restricted. Macroexpansion uses primitive Prolog-style pattern-matching with atoms being always separated by space, so you can use almost any symbols in your macro definition. Just remember that all identifiers starting with $ are supposed to be macro variables.

And there is one special macro variable: $#. It is a macro expansion for the instance ID. it is intended for branching, so you can make labels like HERE_$#: automatically expanding like HERE_$5: or HERE_$16: or HERE_$192: anew every time the macro expansion is happening.

Another feature is file inclusion dedicated to macros only. You can write your macros definitions in the same file your code is, but you can store them elsewhere and then include them with double corner braces like this:

<<console_input_output.min>>

I didn't want to mess with all CLR infrastructure, so this is not the usual C-style inline. it deliberately works with macros definitions only ignoring all the other text: test code or comments. I'd recommend keeping an extension for these files .min (for "macros include") to avoid mixing them with other code files but this is not an inherent restriction.

milasm's People

Contributors

akalenuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

vonj

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.