-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppl_foppl_parser.py
executable file
·39 lines (32 loc) · 1.27 KB
/
ppl_foppl_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
# This file is part of PyFOPPL, an implementation of a First Order Probabilistic Programming Language in Python.
#
# License: MIT (see LICENSE.txt)
#
# 21. Feb 2018, Tobias Kohn
# 20. Mar 2018, Tobias Kohn
#
from ..fe_clojure import ppl_clojure_forms as clj
from ..ppl_ast import *
from .ppl_clojure_lexer import ClojureLexer
from .ppl_clojure_parser import ClojureParser
#######################################################################################################################
class FopplParser(ClojureParser):
def visit_loop(self, count, initial_data, function, *args):
if not clj.is_integer(count):
raise SyntaxError("loop requires an integer value as first argument")
count = count.value
initial_data = initial_data.visit(self)
function = function.visit(self)
args = [arg.visit(self) for arg in args]
result = initial_data
i = 0
while i < count:
result = AstCall(function, [AstValue(i), result] + args)
i += 1
return result
#######################################################################################################################
def parse(source):
clj_ast = list(ClojureLexer(source))
ppl_ast = FopplParser().visit(clj_ast)
return ppl_ast