Skip to content

Instantly share code, notes, and snippets.

//
// graph.h
// graph
//
// Created by oskimura on 2016/01/18.
// Copyright (c) 2016年 oskimura. All rights reserved.
//
#ifndef __graph__graph__
#define __graph__graph__
@oskimura
oskimura / list.cc
Last active January 18, 2016 08:45
#include <stdio.h>
template <typename T>
class list
{
struct node {
node *next;
node *prev;
T val;
};
@oskimura
oskimura / sample.erl
Last active December 20, 2015 15:53
-module(sample).
-export([func1/0]).
func1 () ->
ok.
func2([]) ->
ok;
func2(X) ->
ok.
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname factorial -mnesia debug verbose
eval(File) ->
{ok, B} = file:read_file(File),
Forms = scan(erl_scan:tokens([],binary_to_list(B),1),[]),
F = fun(X) -> {ok,Y} = erl_parse:parse_form(X), Y end,
[F(X) || X <- Forms].

Erlangでオレオレ言語の作り方

言語実装 Advent Calendar 2015の22日目として書かれました。 ErlangでBEAMで動くコンパイラする作成を説明します。

ulangというサンプルプログラムを作ったのでこれを元に説明します。 https://github.com/oskimura/ulang.git

主な流れとしては字句解析器、構文解析器を行って中間表現に変換しcompileモジュールを使ってBEAMで実行可能なバイナリを作成するという流れです。 今回はulang.xrlで字句解析を行い、ulang_yecc.yrlで構文解析及び中間表現の出力、compiler.erlでバイナリ出力するようにつくりました。

let rec findlist f list =
match list with
[] -> None
| (x :: xs) ->
if (f x) then
Some x
else
findlist f xs;;

ASTについて

module

module := {attribute,Line,module,ModuleName},

export

export := {attribute,Line,export,[{f1,Arg},...]},

function

function := {function, Line, FunctionName, ArgNumber [Clause1,...]}

-module(useless).
-export([eval/1]).
eval(File) ->
{ok, B} = file:read_file(File),
Forms = scan(erl_scan:tokens([],binary_to_list(B),1),[]),
F = fun(X) -> {ok,Y} = erl_parse:parse_form(X), Y end,
[F(X) || X <- Forms].
scan({done,{ok,T,N},S},Res) ->

1 Introduction

The following document describes the Erlang BEAM instruction set used for the BEAM threaded-code emulation. To make it self contained the document starts with a short introduction to the BEAM Virtual Machine clarifying different notions used later in the text.

このドキュメントの詳細はErlang BEAMのBEAMVMでつかわれている命令セットである。文章の後半に含まれているBEAM Virtual Machineのさまざまな記法をこの文章の最初にかかれています。

1.1 Historical Note

This document describes BEAM as it was in 1997. BEAM has grown and changed significantly between then and the time this note was added (2012). This information is mainly for historical interest.

@oskimura
oskimura / beam.md
Last active August 14, 2019 01:02

Erlang Beam ファイルフォーマット このフォーマットはEA IFF 85 - Standard for Interchange Format Filesがベースになっている。この「標準」は広くつかわれていない。 IFF graphic file formatがAmigaと Blorb でつかわれている。にもかかわらず、私はこのフォーマットをIFFを元に起草した。なぜならIFF大体において正しい。 アライメントが整ったチャンクだけではない。代わりに4バイトのアライメントで代用する。だから、Beamファイルの開始時には FORMの代わりにFOR1を読み込みプログラムに識別子に使うことをゆるすように古典的IFFからBEAM IFFにかわりました。 'FOR1'という名前はIFFの将来拡張方法として、 IFF文書に含まれています。

Form header

|||