-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcbuilder.js
More file actions
156 lines (143 loc) · 4.82 KB
/
fcbuilder.js
File metadata and controls
156 lines (143 loc) · 4.82 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Adapted from <divergent>coder's toy language interpreter
// http://divergentcoder.com/javascript/playing-with-peg-js/
if (typeof Splaf === "undefined") {
Splaf = {}
}
Splaf.FlowchartBuilder = (function() {
var nextHandles = [1, 4]
var toText = function(structure) {
var elements = []
if (structure.type == 'binaryop') {
elements.push(toText(structure.args[0]))
elements.push(structure.operator)
elements.push(toText(structure.args[1]))
}
if (structure.type == 'literal')
elements.push(structure.value)
if (structure.type == 'variable')
elements.push('(' + structure.name + ')')
return elements.join(' ')
}
var wrap = function(text) {
var maxLength = 25
if (text.length > maxLength) {
for (var i = Math.floor(text.length / 2); i > 0 && text[i] != ' '; i--) {
}
if (text[i] == ' ')
text = text.substr(0, i) + '\n' + text.substr(i + 1)
}
return text
}
var Evaluate = function(program) {
var last = null
for (var i=0; i<program.length; i++)
last = EvalNode[program[i].type](program[i])
return last
}
var EvalNode = {
"block": function(node) {
var last = null
for (var i=0; i<node.code.length; i++)
last = EvalNode[node.code[i].type](node.code[i])
return last
},
"attribution": function(node) {
y += 30
var text = node.name + " = " + toText(node.value)
shapes.push(new Process(r, x, y, wrap(text)))
y += 30
currentShape = shapes[shapes.length - 1]
connections.push(r.connection(lastShape.shape,
currentShape.shape,
nextHandles[0],
nextHandles[1]))
lastShape = currentShape
nextHandles = [1, 4]
},
"prompt": function(node) {
y += 30
shapes.push(new InputOutput(r, x, y, "pede " + node.name))
y += 30
currentShape = shapes[shapes.length - 1]
connections.push(r.connection(lastShape.shape,
currentShape.shape,
nextHandles[0],
nextHandles[1]))
lastShape = currentShape
nextHandles = [1, 4]
},
"print": function(node) {
y += 30
var textElements = []
for (var i=0; i<node.arguments.length; i++)
textElements.push(toText(node.arguments[i]))
var text = "diz: " + textElements.join(' ')
shapes.push(new InputOutput(r, x, y, wrap(text)))
y += 30
currentShape = shapes[shapes.length - 1]
connections.push(r.connection(lastShape.shape,
currentShape.shape,
nextHandles[0],
nextHandles[1]))
lastShape = currentShape
nextHandles = [1, 4]
},
"if": function(node) {
var yElse = y
y += 50
var conditionShape = new Decision(r, x, y, wrap(toText(node.condition)))
shapes.push(conditionShape)
y += 50
connections.push(r.connection(lastShape.shape,
conditionShape.shape,
nextHandles[0],
nextHandles[1]))
lastShape = conditionShape
nextHandles = [1, 4]
EvalNode[node.value.type](node.value)
y += 10
var nodeShape = new Node(r, x, y)
shapes.push(nodeShape)
y += 10
yEndif = y
connections.push(r.connection(lastShape.shape, nodeShape.shape))
if (node.elsevalue != null) {
x += 260
y = yElse
lastShape = conditionShape
nextHandles = [3, 6]
EvalNode[node.elsevalue.type](node.elsevalue)
x -= 260
connections.push(r.connection(lastShape.shape, nodeShape.shape, 1, 7))
} else {
connections.push(r.connection(conditionShape.shape, nodeShape.shape, 3, 7))
}
lastShape = nodeShape
nextHandles = [1, 4]
if (y < yEndif)
y = yEndif
},
"while": function(node) {
y += 50
var conditionShape = new Decision(r, x, y, wrap(toText(node.condition)))
shapes.push(conditionShape)
y += 50
connections.push(r.connection(lastShape.shape,
conditionShape.shape,
nextHandles[0],
nextHandles[1]))
lastShape = conditionShape
nextHandles = [1, 4]
EvalNode[node.value.type](node.value)
y += 10
var nodeShape = new Node(r, x, y)
shapes.push(nodeShape)
y += 10
connections.push(r.connection(lastShape.shape, conditionShape.shape, 2, 6))
connections.push(r.connection(conditionShape.shape, nodeShape.shape, 3, 7))
lastShape = nodeShape
nextHandles = [1, 4]
}
}
return Evaluate
})()