All Projects → kgabis → Ape

kgabis / Ape

Ape Programming Language

Programming Languages

c
50402 projects - #5 most used programming language
language
365 projects

Projects that are alternatives of or similar to Ape

Customasm
💻 An assembler for custom, user-defined instruction sets! https://hlorenzi.github.io/customasm/web/
Stars: ✭ 211 (+8.21%)
Mutual labels:  compiler, vm
Passerine
A small extensible programming language designed for concise expression with little code.
Stars: ✭ 341 (+74.87%)
Mutual labels:  compiler, vm
Mtail
extract internal monitoring data from application logs for collection in a timeseries database
Stars: ✭ 3,028 (+1452.82%)
Mutual labels:  compiler, vm
Tengo
A fast script language for Go
Stars: ✭ 2,528 (+1196.41%)
Mutual labels:  compiler, vm
Typhon
A virtual machine for Monte.
Stars: ✭ 49 (-74.87%)
Mutual labels:  compiler, vm
Smlvm
Smallrepo Virtual Machine
Stars: ✭ 265 (+35.9%)
Mutual labels:  compiler, vm
Ark
ArkScript is a small, fast, functional and scripting language for C++ projects
Stars: ✭ 312 (+60%)
Mutual labels:  compiler, vm
Tinyrb
A tiny subset of Ruby with a Lua'esc VM
Stars: ✭ 452 (+131.79%)
Mutual labels:  compiler, vm
Radon
A scripting language.
Stars: ✭ 22 (-88.72%)
Mutual labels:  compiler, vm
Luago Book
《自己动手实现Lua》随书源代码
Stars: ✭ 514 (+163.59%)
Mutual labels:  compiler, vm
Quickjs
The official repo is at bellard/quickjs.
Stars: ✭ 1,429 (+632.82%)
Mutual labels:  compiler, vm
Rubyx
RubyX compiles ruby to binary (in ruby), hoping to be that X times faster
Stars: ✭ 78 (-60%)
Mutual labels:  compiler, vm
Quickjs
QuickJS是一个小型并且可嵌入的Javascript引擎,它支持ES2020规范,包括模块,异步生成器和代理器。
Stars: ✭ 2,199 (+1027.69%)
Mutual labels:  compiler, vm
Llvm Guide Zh
User Guides For those new to the LLVM system.(LLVM系统的新用户指南,中文翻译版)
Stars: ✭ 180 (-7.69%)
Mutual labels:  compiler
Prototype
(deprecated) The journey continues at ASNEXT: https://github.com/AssemblyScript/assemblyscript
Stars: ✭ 2,114 (+984.1%)
Mutual labels:  compiler
Rubyspeed
Compile ruby functions to C
Stars: ✭ 180 (-7.69%)
Mutual labels:  compiler
Potigol
Linguagem Potigol - Linguagem de programação funcional moderna para iniciantes - A Functional Programming Language for Beginners
Stars: ✭ 179 (-8.21%)
Mutual labels:  compiler
Faerie
Magical ELF and Mach-o object file writer backend
Stars: ✭ 187 (-4.1%)
Mutual labels:  compiler
Appfairy
A CLI tool to Migrate a Webflow project into a React app
Stars: ✭ 183 (-6.15%)
Mutual labels:  compiler
Lam
🚀 a lightweight, universal actor-model vm for writing scalable and reliable applications that run natively and on WebAssembly
Stars: ✭ 176 (-9.74%)
Mutual labels:  vm

The Ape Programming Language

Try Ape in your browser on Ape Playground.

About

Ape is an easy to use programming language and library written in C. It's an offspring of Monkey language (from Writing An Interpreter In Go and Writing A Compiler In Go books by Thorsten Ball), but it evolved to be more procedural with variables, loops, operator overloading, modules, and more.

Current state

It's under development so everything in the language and the api might change.

Example

fn contains_item(to_find, items) {
    for (item in items) {
        if (item == to_find) {
            return true
        }
    }
    return false
}

const cities = ["Warszawa", "Rabka", "Szczecin"]
if (contains_item("Warszawa", cities)) {
    println("found!")
}

Embedding

Add ape.h and ape.c to your project and compile ape.c with a C compiler before linking.

#include "ape.h"

int main() {
    ape_t *ape = ape_make();
    ape_execute(ape, "println(\"hello world\")");
    ape_destroy(ape);
    return 0;
}

An example that shows how to call Ape functions from C code and vice versa can be found here.

Language

Ape is a dynamically typed language with mark and sweep garbage collection. It's compiled to bytecode and executed on internal VM. It's fairly fast for simple numeric operations and not very heavy on allocations (custom allocators can be configured). More documentation can be found here.

Basic types

bool, string, number (double precision float), array, map, function, error

Operators

Math:
+ - * / %

Binary:
^ | & << >>

Logical:
! < > <= >= == != && ||

Assignment:
= += -= *= /= %= ^= |= &= <<= >>=

Defining constants and variables

const constant = 2
constant = 1 // fail
var variable = 3
variable = 7 // ok

Arrays

const arr = [1, 2, 3]
arr[0] // -> 1

Maps

const map = {"lorem": 1, 'ipsum': 2, dolor: 3}
map.lorem // -> 1, dot is a syntactic sugar for [""]
map["ipsum"] // -> 2
map['dolor'] // -> 3

Conditional statements

if (a) {
    // a
} else if (b) {
    // b
} else {
    // c
}

Loops

while (true) {
    // body
}

var items = [1, 2, 3]
for (item in items) {
    if (item == 2) {
        break
    } else {
        continue
    }
}

for (var i = 0; i < 10; i += 1) {
    // body
}

Functions

const add_1 = fn(a, b) { return a + b }

fn add_2(a, b) {
    return a + b
}

fn map_items(items, map_fn) {
    const res = []
    for (item in items) {
        append(res, map_fn(item))
    }
    return res
}

map_items([1, 2, 3], fn(x){ return x + 1 })

fn make_person(name) {
    return {
        name: name,
        greet: fn() {
            println("Hello, I'm " + this.name)
        },
    }
}

Errors

const err = error("something bad happened")
if (is_error(err)) {
    println(err)
}

fn() {
    recover (e) { // e is a runtime error wrapped in error
        return null
    }
    crash("something bad happened") // crashes are recovered with "recover" statement
}

Modules

import "foo" // import "foo.bn" and load global symbols prefixed with foo::

foo::bar()

import "bar/baz" // import "bar/baz.bn" and load global symbols prefixed with baz::
baz::foo()

Operator overloading

fn vec2(x, y) {
    return {
        x: x,
        y: y,
        __operator_add__: fn(a, b) { return vec2(a.x + b.x, a.y + b.y)},
        __operator_sub__: fn(a, b) { return vec2(a.x - b.x, a.y - b.y)},
        __operator_minus__: fn(a) { return vec2(-a.x, -a.y) },
        __operator_mul__: fn(a, b) {
            if (is_number(a)) {
                return vec2(b.x * a, b.y * a)
            } else if (is_number(b)) {
                return vec2(a.x * b, a.y * b)
            } else {
                return vec2(a.x * b.x, a.y * b.y)
            }
        },
    }
}

Splitting and joining

ape.c can be split into separate files by running utils/split.py:

utils/split.py --input ape.c --output-path ape

It can be joined back into a single file with utils/join.py:

utils/join.py --template utils/ape.c.templ --path ape --output ape.c

Visual Studio Code extension

A Visual Studio Code extension can be found here.

My other projects

  • parson - JSON library
  • kgflags - command-line flag parsing library
  • agnes - header-only NES emulation library

License

The MIT License (MIT)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].