Stack based programming language that compiles to x86_64 assembly or can alternatively be interpreted in Python

Related tags

Text Data & NLPlang
Overview

lang

lang is a simple stack based programming language written in Python. It can either be interpreted in Python, or be compiled to x86_64 assembly code using nasm. Note that the compiled executables will only run on 64 bit linux distributions since linux syscalls are used, although the Python simulation mode should work on all modern operating systems.

Installation

  1. Clone repository
  2. Make sure you have nasm and ld in your path as these are used for compiling programs
  3. Install python dependcies:
    • pip install pathlib
  4. Try to run one of the example files:
    • Try to simulate: ./main.py examples/fib/fib.lang simulate and
    • Try to compile: ./main.py examples/dib/fib.lang compile examples/fib/fib.asm
    • Try to run the compiled file: ./examples/fib/fib
  5. Add the main.py to your system path somehow, e.g. save a bash script called lang in your ~/bin:
    ~/coding/lang/main.py "$@"
    Then, you can do lang [program] [simulate | compile <out-file>]

Usage

Write a program with .lang extension, for example program.lang. Then you can either simulate it in Python with lang program.lang simulate. You can compile it to x86_64 assembly with lang program.lang compile program.asm. The compiled assembly code will be stored in the file specified, in this case program.asm. Additionally, an object file program.o will be generated, and the actual executable, simply named program with no extension. To run it, run ./program.

Features

lang (name not final) is a very simple stack based language, and currently does not have features you might be used to like variables, etc. Instead, you work with a stack. A program consists of a series of instructions. Instructions are separated by spaces, and newlines and excess whitespace are ignored. You can use # to type comments, anything afer a # will be ignored.

Currently the only type of data that is supported is signed integers. Nested if-else blocks and loops are supported.

Operation Syntax Description
PUSH int Push a number onto the stack, i.e. 45 pushes the number 45 onto the stack
POP pop Pop the top number off the stack
ADD + Pop the top two numbers off the stack, add them, and push the result back onto the stack
SUB - Pop the top two numbers a and b off the stack, subtract them (b - a), then push the result onto the stack
MUL * Pop the top two numbers a and b off the stack, multiply them, then push the result onto the stack
DIV / Pop the top two numbers a and b off the stack and performs integer division b // a on them. Then, pushes the ratio and remainder onto the stack, in that order, so the remainder is on top.
DUMP dump Pop the top number off the stack, and print it to standard output
DUP dup[n] dup will duplicate the top number on the stack and push it on top. dup2 will duplicate the second number from the top and push it to the top of the stack. You can also do dup3, etc.
SWAP swap Swaps the two topmost numbers on the stack
IF if Peeks at the top number off the stack. If it is 0, go to the next else or end. If it is nonzero, Go to the next instruction
EQ = Pops the top two numbers off the stack, and checks if they are equal. If they are, push 1 to the stack, otherwise push 0.
GE > Pops the top two numbers off the stack, and checks if the second number is greater than the top number. If it is, push 1 to the stack, otherwise push 0.
GEQ >= Pops the top two numbers off the stack, and checks if the second number is greater than or equal to the top number. If it is, push 1 to the stack, otherwise push 0.
LE < Pops the top two numbers off the stack, and checks if the second number is less than the top number. If it is, push 1 to the stack, otherwise push 0.
LEQ <= Pops the top two numbers off the stack, and checks if the second number is less than or equal to the top number. If it is, push 1 to the stack, otherwise push 0.
ELSE else If if fails, execution will jump to the else if one exists
END end Marks the end of an if-else block or a loop
WHILE while Peeks at the top number on the stack. If it is nonzero, execute the code until the next end. Then peek at the top number again and repeat until the top number is zero, then jump to the end

Examples

Code examples can be found in the examples directory, but here is an example program that calculates the fibonacci numbers less than or equal to 1000 and prints them to standard output:

1 1 while
  dup dump
  swap dup2 +
  dup 1000 >= if 0 end
end
Owner
Christoffer Aakre
Christoffer Aakre
中文医疗信息处理基准CBLUE: A Chinese Biomedical LanguageUnderstanding Evaluation Benchmark

English | 中文说明 CBLUE AI (Artificial Intelligence) is playing an indispensabe role in the biomedical field, helping improve medical technology. For fur

452 Dec 30, 2022
To classify the News into Real/Fake using Features from the Text Content of the article

Hoax-Detector Authenticity of news has now become a major problem. The Idea is to classify the News into Real/Fake using Features from the Text Conten

Aravindhan 1 Feb 09, 2022
Official implementation of Meta-StyleSpeech and StyleSpeech

Meta-StyleSpeech : Multi-Speaker Adaptive Text-to-Speech Generation Dongchan Min, Dong Bok Lee, Eunho Yang, and Sung Ju Hwang This is an official code

min95 169 Jan 05, 2023
Implementation of Memorizing Transformers (ICLR 2022), attention net augmented with indexing and retrieval of memories using approximate nearest neighbors, in Pytorch

Memorizing Transformers - Pytorch Implementation of Memorizing Transformers (ICLR 2022), attention net augmented with indexing and retrieval of memori

Phil Wang 364 Jan 06, 2023
Bnagla hand written document digiiztion

Bnagla hand written document digiiztion This repo addresses the problem of digiizing hand written documents in Bangla. Documents have definite fields

Mushfiqur Rahman 1 Dec 10, 2021
Collection of useful (to me) python scripts for interacting with napari

Napari scripts A collection of napari related tools in various state of disrepair/functionality. Browse_LIF_widget.py This module can be imported, for

5 Aug 15, 2022
Twewy-discord-chatbot - Build a Discord AI Chatbot that Speaks like Your Favorite Character

Build a Discord AI Chatbot that Speaks like Your Favorite Character! This is a Discord AI Chatbot that uses the Microsoft DialoGPT conversational mode

Lynn Zheng 231 Dec 30, 2022
Include MelGAN, HifiGAN and Multiband-HifiGAN, maybe NHV in the future.

Fast (GAN Based Neural) Vocoder Chinese README Todo Submit demo Support NHV Discription Include MelGAN, HifiGAN and Multiband-HifiGAN, maybe include N

Zhengxi Liu (刘正曦) 134 Dec 16, 2022
A retro text-to-speech bot for Discord

hawking A retro text-to-speech bot for Discord, designed to work with all of the stuff you might've seen in Moonbase Alpha, using the existing command

Nick Schorr 23 Dec 25, 2022
simpleT5 is built on top of PyTorch-lightning⚡️ and Transformers🤗 that lets you quickly train your T5 models.

Quickly train T5 models in just 3 lines of code + ONNX support simpleT5 is built on top of PyTorch-lightning ⚡️ and Transformers 🤗 that lets you quic

Shivanand Roy 220 Dec 30, 2022
A Survey of Natural Language Generation in Task-Oriented Dialogue System (TOD): Recent Advances and New Frontiers

A Survey of Natural Language Generation in Task-Oriented Dialogue System (TOD): Recent Advances and New Frontiers

Libo Qin 132 Nov 25, 2022
ZUNIT - Toward Zero-Shot Unsupervised Image-to-Image Translation

ZUNIT Dependencies you can install all the dependencies by pip install -r requirements.txt Datasets Download CUB dataset. Unzip the birds.zip at ./da

Chen Yuanqi 9 Jun 24, 2022
A Word Level Transformer layer based on PyTorch and 🤗 Transformers.

Transformer Embedder A Word Level Transformer layer based on PyTorch and 🤗 Transformers. How to use Install the library from PyPI: pip install transf

Riccardo Orlando 27 Nov 20, 2022
Official PyTorch implementation of SegFormer

SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers Figure 1: Performance of SegFormer-B0 to SegFormer-B5. Project page

NVIDIA Research Projects 1.4k Dec 29, 2022
IMS-Toucan is a toolkit to train state-of-the-art Speech Synthesis models

IMS-Toucan is a toolkit to train state-of-the-art Speech Synthesis models. Everything is pure Python and PyTorch based to keep it as simple and beginner-friendly, yet powerful as possible.

Digital Phonetics at the University of Stuttgart 247 Jan 05, 2023
Textpipe: clean and extract metadata from text

textpipe: clean and extract metadata from text textpipe is a Python package for converting raw text in to clean, readable text and extracting metadata

Textpipe 298 Nov 21, 2022
Official PyTorch implementation of "Dual Path Learning for Domain Adaptation of Semantic Segmentation".

Dual Path Learning for Domain Adaptation of Semantic Segmentation Official PyTorch implementation of "Dual Path Learning for Domain Adaptation of Sema

27 Dec 22, 2022
This is Assignment1 code for the Web Data Processing System.

This is a Python program to Entity Linking by processing WARC files. We recognize entities from web pages and link them to a Knowledge Base(Wikidata).

3 Dec 04, 2022
pkuseg多领域中文分词工具; The pkuseg toolkit for multi-domain Chinese word segmentation

pkuseg:一个多领域中文分词工具包 (English Version) pkuseg 是基于论文[Luo et. al, 2019]的工具包。其简单易用,支持细分领域分词,有效提升了分词准确度。 目录 主要亮点 编译和安装 各类分词工具包的性能对比 使用方式 论文引用 作者 常见问题及解答 主要

LancoPKU 6k Dec 29, 2022
Mlcode - Continuous ML API Integrations

mlcode Basic APIs for ML applications. Django REST Application Contains REST API

Sujith S 1 Jan 01, 2022