-
-
Save koo5/5793813 to your computer and use it in GitHub Desktop.
language.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
#spaces...spaces everywhere (pyyaml doesnt play with tabs) | |
import yaml | |
modules = [] | |
codes = """ | |
- | |
type: metadata | |
name: base library | |
- | |
type: function declaration | |
name: print | |
code: | |
- | |
type: python function call | |
name: print | |
module: __builtin__ | |
--- | |
- | |
type: metadata | |
name: hello world | |
- | |
type: function call | |
module: base library | |
name: print | |
arguments: | |
- | |
type: string | |
value: hello world! | |
--- | |
- | |
type: metadata | |
name: for fun | |
- | |
type: for loop | |
items: #(expression) | |
type: list | |
value: [1.2.3] | |
item: item | |
code: | |
- | |
type: function call | |
name: print | |
argument: item | |
""" | |
class Interpreter: | |
def __init__(self, modules): | |
self.modules = modules | |
self.python_modules = dict() | |
self.stack=[] | |
def evaluate(self, item): | |
if item.has_key("value"): | |
return item["value"] | |
else: | |
print i, "oops" | |
def eval_args(self,args): | |
print i, "evaling", args,":" | |
result = map(lambda x:self.evaluate(x), args) | |
print i, result | |
return result | |
def repr_item(self, item): | |
ret = item["name"] | |
if item.has_key(self): | |
ret += "(" + item[self]["value"] + ")" | |
return ret | |
def function_by_name(self, module, name): | |
# | |
for item in module: | |
if item["type"] == "function declaration": | |
if item["name"] == name: | |
return item | |
print i, "wat?" | |
return None | |
def python_import(self, module): | |
if not self.python_modules.has_key(module): | |
print i,"importing ",module | |
self.python_modules[module]=__import__(module) | |
return self.python_modules[module] | |
def python_call(self, module, name): | |
print i,"calling",module,".",name | |
arguments = self.stack.pop() | |
self.python_import(module).__dict__[name](*arguments) | |
def run(self, item, args=None): | |
print i,"run",item,"args:",args | |
if isinstance(item, list): | |
# | |
print i,"list with",len(item),"items" | |
indent() | |
for subitem in item: | |
if subitem["type"] not in ["function declaration", "metadata"]: | |
self.run(subitem) | |
outdent() | |
elif isinstance(item, dict): | |
print i,"[",item["type"],"] ", item["intended presentation"] if item.has_key("intended presentation") else "" | |
indent() | |
if item["type"] == "is": | |
print i,item["subject"], "is ", item["object"] | |
print i,"# ->>>reasoner. append to the tree? stick it inside a reasoner?" | |
elif item["type"] == "function call": | |
print i,"call", item["name"] | |
if item.has_key("module"): | |
module = module_by_name(item["module"]) | |
else: | |
module = self.main_module | |
self.stack.append(self.eval_args(item["arguments"])) | |
self.run(self.function_by_name(module, item["name"])) | |
elif item["type"] == "for loop": | |
print i,"for", (item["items"]), "as" ,item["item"] | |
indent() | |
for loop_item in evaluate(item["items"]): | |
item["item"][self]["value"] = loop_item | |
self.run(item["code"]) | |
outdent() | |
elif item["type"] == "python function call": | |
self.python_call(item["module"], item["name"]) | |
elif item["type"] == "metadata": | |
pass | |
elif item["type"] == "function declaration": | |
self.run(item["code"]) | |
else: | |
print i,"what do?" | |
outdent() | |
def get_vars(scope): | |
if isset(scope["runtime"]) and isset(scope["runtime"]["vars"]): | |
return scope["runtime"]["vars"] | |
else: | |
return None | |
def print_binds(scope): | |
vars = get_vars(scope) | |
if vars: | |
print ">" | |
indent() | |
for name, value in vars: | |
print i, name,":", value | |
outdent() | |
#indentation | |
i = "" | |
def indent(): | |
global i | |
i+=" " | |
def outdent(): | |
global i | |
i = i[:-4] | |
def get_module_name(m): | |
for item in m: | |
if item["type"] == "metadata": | |
return item["name"] | |
def module_by_name(name): | |
##map(modules, lambda x: True if x[ | |
result = [] | |
for module in modules: | |
if get_module_name(module) == name: | |
result.append(module) | |
if len(result) != 1: | |
print "result:",result | |
return result[0] | |
def run_module(name): | |
m = module_by_name(name) | |
print "module", name | |
indent() | |
machine = Interpreter(modules) | |
machine.main_module = m | |
machine.run(m) | |
outdent() | |
#(open("code.yaml", "r").read()) | |
for doc in yaml.load_all(codes): | |
modules.append(doc) | |
#print "modules:",modules | |
run_module("hello world") | |
#a procedure should as much be a queryable knowledge thing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment