ライブコーディング
http://twitter.com/hogelog/statuses/772016130
hogelogさんに騙されてライブコーディングなるものを見てきました。生コーディングですよ!ドキドキですね。
Forth処理系の実装をC言語でされていて、適当に横からツッコミ入れてはツッコミを訂正されるという無能ぷりを発揮しました。
C言語難しい!
とりあえずDでもやっつけくさいのを書いといたんで載せておきます。間違ってたら教えてください。
import std.stdio, std.string, std.conv; void push(int i){ stack ~= i; } int pop(){ int ret = stack[$-1]; stack.length = stack.length - 1; return ret; } void define(string str){ string[] token = str.split(); string state = token[2..length-1].join(" "); dict[token[1]] = { eval(state); }; } bool isint(string str){ try { to!(int)(str); } catch (ConvError e){ return false; } return true; } void eval(string str){ foreach (token; str.split()){ if (token.isint()){ push(to!(int)(token)); } else { if (token in dict){ dict[token](); } else { writeln("error: bakabaka sine!"); } } } } int[] stack; void delegate()[string] dict; void init(){ dict["+"] = { int rhs = pop; int lhs = pop; push(lhs + rhs); }; dict["-"] = { int rhs = pop; int lhs = pop; push(lhs - rhs); }; dict["*"] = { int rhs = pop; int lhs = pop; push(lhs * rhs); }; dict["/"] = { int rhs = pop; int lhs = pop; push(lhs / rhs); }; dict["%"] = { int rhs = pop; int lhs = pop; push(lhs % rhs); }; dict["."] = { int temp = pop; writeln(temp); push(temp); }; } void main(){ init(); string line; while ((line = readln) !is null){ line = line.chomp(); if (startsWith(line, ":") && endsWith(line, ";")){ define(line); } else { eval(line); } writeln(stack); } }