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
Mastering Transformers, published by Packt

Mastering Transformers This is the code repository for Mastering Transformers, published by Packt. Build state-of-the-art models from scratch with adv

Packt 195 Jan 01, 2023
Based on 125GB of data leaked from Twitch, you can see their monthly revenues from 2019-2021

Twitch Revenues Bu script'i kullanarak istediğiniz yayıncıların, Twitch'den sızdırılan 125 GB'lik veriye dayanarak, 2019-2021 arası aylık gelirlerini

4 Nov 11, 2021
Facebook AI Research Sequence-to-Sequence Toolkit written in Python.

Fairseq(-py) is a sequence modeling toolkit that allows researchers and developers to train custom models for translation, summarization, language mod

13.2k Jul 07, 2021
Phomber is infomation grathering tool that reverse search phone numbers and get their details, written in python3.

A Infomation Grathering tool that reverse search phone numbers and get their details ! What is phomber? Phomber is one of the best tools available fo

S41R4J 121 Dec 27, 2022
Segmenter - Transformer for Semantic Segmentation

Segmenter - Transformer for Semantic Segmentation

592 Dec 27, 2022
DANeS is an open-source E-newspaper dataset by collaboration between DATASET JSC (dataset.vn) and AIV Group (aivgroup.vn)

DANeS - Open-source E-newspaper dataset Source: Technology vector created by macrovector - www.freepik.com. DANeS is an open-source E-newspaper datase

DATASET .JSC 64 Aug 17, 2022
A combination of autoregressors and autoencoders using XLNet for sentiment analysis

A combination of autoregressors and autoencoders using XLNet for sentiment analysis Abstract In this paper sentiment analysis has been performed in or

James Zaridis 2 Nov 20, 2021
official ( API ) for the zAmericanEnglish app in [ Google play ] and [ App store ]

official ( API ) for the zAmericanEnglish app in [ Google play ] and [ App store ]

Plugin 3 Jan 12, 2022
SpeechBrain is an open-source and all-in-one speech toolkit based on PyTorch.

The goal is to create a single, flexible, and user-friendly toolkit that can be used to easily develop state-of-the-art speech technologies, including systems for speech recognition, speaker recognit

SpeechBrain 5.1k Jan 09, 2023
Contains descriptions and code of the mini-projects developed in various programming languages

TexttoSpeechAndLanguageTranslator-project introduction A pleasant application where the client will be given buttons like play,reset and exit. The cli

Adarsh Reddy 1 Dec 22, 2021
Ray-based parallel data preprocessing for NLP and ML.

Wrangl Ray-based parallel data preprocessing for NLP and ML. pip install wrangl # for latest pip install git+https://github.com/vzhong/wrangl See exa

Victor Zhong 33 Dec 27, 2022
Neural network models for joint POS tagging and dependency parsing (CoNLL 2017-2018)

Neural Network Models for Joint POS Tagging and Dependency Parsing Implementations of joint models for POS tagging and dependency parsing, as describe

Dat Quoc Nguyen 152 Sep 02, 2022
Mapping a variable-length sentence to a fixed-length vector using BERT model

Are you looking for X-as-service? Try the Cloud-Native Neural Search Framework for Any Kind of Data bert-as-service Using BERT model as a sentence enc

Han Xiao 11.1k Jan 01, 2023
Product-Review-Summarizer - Created a product review summarizer which clustered thousands of product reviews and summarized them into a maximum of 500 characters, saving precious time of customers and helping them make a wise buying decision.

Product-Review-Summarizer - Created a product review summarizer which clustered thousands of product reviews and summarized them into a maximum of 500 characters, saving precious time of customers an

Parv Bhatt 1 Jan 01, 2022
Anomaly Detection 이상치 탐지 전처리 모듈

Anomaly Detection 시계열 데이터에 대한 이상치 탐지 1. Kernel Density Estimation을 활용한 이상치 탐지 train_data_path와 test_data_path에 존재하는 시점 정보를 포함하고 있는 csv 형태의 train data와

CLUST-consortium 43 Nov 28, 2022
Code for Discovering Topics in Long-tailed Corpora with Causal Intervention.

Code for Discovering Topics in Long-tailed Corpora with Causal Intervention ACL2021 Findings Usage 0. Prepare environment Requirements: python==3.6 te

Xiaobao Wu 8 Dec 16, 2022
A library for Multilingual Unsupervised or Supervised word Embeddings

MUSE: Multilingual Unsupervised and Supervised Embeddings MUSE is a Python library for multilingual word embeddings, whose goal is to provide the comm

Facebook Research 3k Jan 06, 2023
Correctly generate plurals, ordinals, indefinite articles; convert numbers to words

NAME inflect.py - Correctly generate plurals, singular nouns, ordinals, indefinite articles; convert numbers to words. SYNOPSIS import inflect p = in

Jason R. Coombs 762 Dec 29, 2022
The simple project to separate mixed voice (2 clean voices) to 2 separate voices.

Speech Separation The simple project to separate mixed voice (2 clean voices) to 2 separate voices. Result Example (Clisk to hear the voices): mix ||

vuthede 31 Oct 30, 2022
Black for Python docstrings and reStructuredText (rst).

Style-Doc Style-Doc is Black for Python docstrings and reStructuredText (rst). It can be used to format docstrings (Google docstring format) in Python

Telekom Open Source Software 13 Oct 24, 2022