Ape – an easy-to-embed programming language in two C files

栏目: IT技术 · 发布时间: 4年前

内容简介:Ape is an easy to use programming language and library written in C. It's an offspring ofIt's under development so everything in the language and the api might change.Add ape.h and ape.c to your project and compile ape.c with a C compiler before linking.

The Ape Programming Language

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, 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).

Basic types

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

Operators

Math:
+ - * /

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) {
    const person = {}
    person.name = name
    person.greet = fn() {
        println("Hello, I'm " + name)
    }
    return person
}

Errors

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

Modules

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

foo::bar()

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

License

The MIT License (MIT)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

玻璃笼子

玻璃笼子

[美]尼古拉斯·卡尔 / 杨柳 / 中信出版社 / 2015-11 / 49.00元

这是一本关于自动化的书,它提醒我们自动化对人类的影响,人们心安理得享受技术带来的便利却忽视了,它已经渗透进了生活和工作改变了我们的思维和认知方式。商家在设计程序和应用时,早就把他们的想法埋入了编程和APP中。 卡尔的作品无疑是给我们这个时代灌入了的一剂清醒药。他独特的思考问题角度,犀利甚至略为偏激 的言论再加上丰富的*前沿的科技案例会让人读起来畅快淋漓,且醍醐灌顶,意识到自动化等高科技潜移默......一起来看看 《玻璃笼子》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具