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
Spacy-ginza-ner-webapi - Named Entity Recognition API with spaCy and GiNZA

Named Entity Recognition API with spaCy and GiNZA I wrote a blog post about this

Yuki Okuda 3 Feb 27, 2022
Artificial Conversational Entity for queries in Eulogio "Amang" Rodriguez Institute of Science and Technology (EARIST)

🤖 Coeus - EARIST A.C.E 💬 Coeus is an Artificial Conversational Entity for queries in Eulogio "Amang" Rodriguez Institute of Science and Technology,

Dids Irwyn Reyes 3 Oct 14, 2022
🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.

English | 简体中文 | 繁體中文 | 한국어 State-of-the-art Machine Learning for JAX, PyTorch and TensorFlow 🤗 Transformers provides thousands of pretrained models

Hugging Face 77.1k Dec 31, 2022
A Python script which randomly chooses and prints a file from a directory.

___ ____ ____ _ __ ___ / _ \ | _ \ | _ \ ___ _ __ | '__| / _ \ | |_| || | | || | | | / _ \| '__| | | | __/ | _ || |_| || |_| || __

yesmaybenookay 0 Aug 06, 2021
Knowledge Oriented Programming Language

KoPL: 面向知识的推理问答编程语言 安装 | 快速开始 | 文档 KoPL全称 Knowledge oriented Programing Language, 是一个为复杂推理问答而设计的编程语言。我们可以将自然语言问题表示为由基本函数组合而成的KoPL程序,程序运行的结果就是问题的答案。目前,

THU-KEG 62 Dec 12, 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
Transformer-based Text Auto-encoder (T-TA) using TensorFlow 2.

T-TA (Transformer-based Text Auto-encoder) This repository contains codes for Transformer-based Text Auto-encoder (T-TA, paper: Fast and Accurate Deep

Jeong Ukjae 13 Dec 13, 2022
Natural language computational chemistry command line interface.

nlcc Install pip install nlcc Must have Open-AI Codex key: export OPENAI_API_KEY=your key here then nlcc key bindings ctrl-w copy to clipboard (Note

Andrew White 37 Dec 14, 2022
Official PyTorch code for ClipBERT, an efficient framework for end-to-end learning on image-text and video-text tasks

Official PyTorch code for ClipBERT, an efficient framework for end-to-end learning on image-text and video-text tasks. It takes raw videos/images + text as inputs, and outputs task predictions. ClipB

Jie Lei 雷杰 612 Jan 04, 2023
DeeBERT: Dynamic Early Exiting for Accelerating BERT Inference

DeeBERT This is the code base for the paper DeeBERT: Dynamic Early Exiting for Accelerating BERT Inference. Code in this repository is also available

Castorini 132 Nov 14, 2022
Code and datasets for our paper "PTR: Prompt Tuning with Rules for Text Classification"

PTR Code and datasets for our paper "PTR: Prompt Tuning with Rules for Text Classification" If you use the code, please cite the following paper: @art

THUNLP 118 Dec 30, 2022
NLP and Text Generation Experiments in TensorFlow 2.x / 1.x

Code has been run on Google Colab, thanks Google for providing computational resources Contents Natural Language Processing(自然语言处理) Text Classificati

1.5k Nov 14, 2022
A python package for deep multilingual punctuation prediction.

This python library predicts the punctuation of English, Italian, French and German texts. We developed it to restore the punctuation of transcribed spoken language.

Oliver Guhr 27 Dec 22, 2022
Use the state-of-the-art m2m100 to translate large data on CPU/GPU/TPU. Super Easy!

Easy-Translate is a script for translating large text files in your machine using the M2M100 models from Facebook/Meta AI. We also privide a script fo

Iker García-Ferrero 41 Dec 15, 2022
Yes it's true :broken_heart:

Information WARNING: No longer hosted If you would like to be on this repo's readme simply fork or star it! Forks 1 - Flowzii 2 - Errorcrafter 3 - vk-

Dropout 66 Dec 31, 2022
Pre-Training with Whole Word Masking for Chinese BERT

Pre-Training with Whole Word Masking for Chinese BERT

Yiming Cui 7.7k Dec 31, 2022
Extracting Summary Knowledge Graphs from Long Documents

GraphSum This repo contains the data and code for the G2G model in the paper: Extracting Summary Knowledge Graphs from Long Documents. The other basel

Zeqiu (Ellen) Wu 10 Oct 21, 2022
Trained T5 and T5-large model for creating keywords from text

text to keywords Trained T5-base and T5-large model for creating keywords from text. Supported languages: ru Pretraining Large version | Pretraining B

Danil 61 Nov 24, 2022
source code for paper: WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach.

WhiteningBERT Source code and data for paper WhiteningBERT: An Easy Unsupervised Sentence Embedding Approach. Preparation git clone https://github.com

49 Dec 17, 2022