Coder Social home page Coder Social logo

wee's Introduction

wee

wee is a minimal instruction set designed to be as easy to port to a system/esolang as possible. This repository facilitates compiling C to wee. See usage. The C is first compiled to elvm IR using the elvm C compiler. The IR is then compiled to wee.

Try the web demo!

Description

wee programs have a memory array and 2 registers A and B. There are 11 instructions. An instruction either has no arguments or exactly one signed integer argument.

Instruction Description
mov n A = n
swap swap values of A and B
add A += B
sub A -= B
load A = memory[A]
store memory[A] = B
setlt if A < B, then set A = 1, else A = 0
jmpz n if A == 0, go to nth instruction
getc A = getchar()
putc putchar(A)
exit stop the program

Some key takeaways: There are no redundant comparison instructions. There is no jump register instruction. There are no labels nor data declarations.

Usage

To compile a C program to wee, clone the repo and run the compile script. The compile script requires docker. The first run will take a while to build the docker image.

git clone https://github.com/nathanfarlow/wee.git
cd wee
./compile.sh <file.c> <memory size>

Here, the memory size argument is the size of the memory array (number of words) your backend will support.

Now you can interpret the wee file or compile it to a new target! An example python interpreter can be found here. See the example directory for a example C program that demonstrates I/O and stdlib things and the commands to compile and run it.

FAQ

Q: Why does a simple C program produce so many instructions?

A: The elvm to wee compilation is efficient given the nature of the transformation. It incurs about a 6.5x instruction overhead (compared to elvm to brainfuck's ~1335x). The C to elvm compiler is very inefficient, though. I'm working on an optimizer here.

Q: How do I perform side effects beyond input/output of characters, like drawing to a screen?

A: I recommend in this case you repurpose getc/putc to be a communication channel between your program and your interpreter/native side. For example, maybe your wee program wants to draw to pixel (10, 20). Perhaps it calls putc 3 times: once with an integer denoting the "start draw" command, then 10, then 20.

Q: What about bitwise operations? Floating point math?

A: It won't be efficient, but you can do bitwise operations in pure wee. You can write slow divide, mod, and multiply functions. This will give you all you need to deconstruct a number into its bits and construct a number from bits. One idea to replace floating point math is to use fixed point arithmetic. An alternative to both of these ideas is to again use your repurposed putc/getc channel to natively compute values interpreter/native side.

wee's People

Contributors

nathanfarlow avatar

Stargazers

Pete Stenger avatar John Cesarz avatar Steven Pan avatar Anakin Dey avatar

Watchers

 avatar  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.