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
Python package to easily retrain OpenAI's GPT-2 text-generating model on new texts

gpt-2-simple A simple Python package that wraps existing model fine-tuning and generation scripts for OpenAI's GPT-2 text generation model (specifical

Max Woolf 3.1k Jan 07, 2023
PyJPBoatRace: Python-based Japanese boatrace tools 🚤

pyjpboatrace :speedboat: provides you with useful tools for data analysis and auto-betting for boatrace.

5 Oct 29, 2022
Code for lyric-section-to-comment generation based on huggingface transformers.

CommentGeneration Code for lyric-section-to-comment generation based on huggingface transformers. Migrate Guyu model and code (both 12-layers and 24-l

Yawei Sun 8 Sep 04, 2021
Final Project for the Intel AI Readiness Boot Camp NLP (Jan)

NLP Boot Camp (Jan) Synopsis Full Name: Prameya Mohanty Name of your School: Delhi Public School, Rourkela Class: VIII Title of the Project: iTransect

TheCodingHub 1 Feb 01, 2022
A toolkit for document-level event extraction, containing some SOTA model implementations

Document-level Event Extraction via Heterogeneous Graph-based Interaction Model with a Tracker Source code for ACL-IJCNLP 2021 Long paper: Document-le

84 Dec 15, 2022
AEC_DeepModel - Deep learning based acoustic echo cancellation baseline code

AEC_DeepModel - Deep learning based acoustic echo cancellation baseline code

凌逆战 75 Dec 05, 2022
Constituency Tree Labeling Tool

Constituency Tree Labeling Tool The purpose of this package is to solve the constituency tree labeling problem. Look from the dataset labeled by NLTK,

张宇 6 Dec 20, 2022
MHtyper is an end-to-end pipeline for recognized the Forensic microhaplotypes in Nanopore sequencing data.

MHtyper is an end-to-end pipeline for recognized the Forensic microhaplotypes in Nanopore sequencing data. It is implemented using Python.

willow 6 Jun 27, 2022
✨Fast Coreference Resolution in spaCy with Neural Networks

✨ NeuralCoref 4.0: Coreference Resolution in spaCy with Neural Networks. NeuralCoref is a pipeline extension for spaCy 2.1+ which annotates and resolv

Hugging Face 2.6k Jan 04, 2023
Blazing fast language detection using fastText model

Luga A blazing fast language detection using fastText's language models Luga is a Swahili word for language. fastText provides a blazing fast language

Prayson Wilfred Daniel 18 Dec 20, 2022
The training code for the 4th place model at MDX 2021 leaderboard A.

The training code for the 4th place model at MDX 2021 leaderboard A.

Chin-Yun Yu 32 Dec 18, 2022
DataCLUE: 国内首个以数据为中心的AI测评(含模型分析报告)

DataCLUE 以数据为中心的AI测评(DataCLUE) DataCLUE: A Chinese Data-centric Language Evaluation Benchmark 内容导引 章节 描述 简介 介绍以数据为中心的AI测评(DataCLUE)的背景 任务描述 任务描述 实验结果

CLUE benchmark 135 Dec 22, 2022
AI-Broad-casting - AI Broad casting with python

Basic Code 1. Use The Code Configuration Environment conda create -n code_base p

Ecco is a python library for exploring and explaining Natural Language Processing models using interactive visualizations.

Visualize, analyze, and explore NLP language models. Ecco creates interactive visualizations directly in Jupyter notebooks explaining the behavior of Transformer-based language models (like GPT2, BER

Jay Alammar 1.6k Dec 25, 2022
Geometry-Consistent Neural Shape Representation with Implicit Displacement Fields

Geometry-Consistent Neural Shape Representation with Implicit Displacement Fields [project page][paper][cite] Geometry-Consistent Neural Shape Represe

Yifan Wang 100 Dec 19, 2022
Top2Vec is an algorithm for topic modeling and semantic search.

Top2Vec is an algorithm for topic modeling and semantic search. It automatically detects topics present in text and generates jointly embedded topic, document and word vectors.

Dimo Angelov 2.4k Jan 06, 2023
PyABSA - Open & Efficient for Framework for Aspect-based Sentiment Analysis

PyABSA - Open & Efficient for Framework for Aspect-based Sentiment Analysis

YangHeng 567 Jan 07, 2023
Maha is a text processing library specially developed to deal with Arabic text.

An Arabic text processing library intended for use in NLP applications Maha is a text processing library specially developed to deal with Arabic text.

Mohammad Al-Fetyani 184 Nov 27, 2022
Experiments in converting wikidata to ftm

FollowTheMoney / Wikidata mappings This repo will contain tools for converting Wikidata entities into FtM schema. Prefixes: https://www.mediawiki.org/

Friedrich Lindenberg 2 Nov 12, 2021
Watson Natural Language Understanding and Knowledge Studio

Material de demonstração dos serviços: Watson Natural Language Understanding e Knowledge Studio Visão Geral: https://www.ibm.com/br-pt/cloud/watson-na

Vanderlei Munhoz 4 Oct 24, 2021