Truth table: Difference between revisions

Line 4,049:
<lang smalltalk>[:repeat |
expr := Stdin
request:'Enter boolean expression (name variables a,b,c...):'
defaultAnswer:'a|b'.
ast := Parser parseExpression:expr inNameSpace:nil onError:repeat.
"
ensure that only boolean logic operations are inside (sandbox)
"
(ast messageSelectors asSet conform:[:each | #( '|' '&' 'not' 'xor' '==>' ) includes:each]) ifFalse:repeat.
conform:[:each | #( '|' '&' 'not' 'xor' '==>' ) includes:each])
ifFalse:repeat.
] valueWithRestart.
 
Line 4,063 ⟶ 4,065:
"
varNames := StringCollection streamContents:[:s | ast variableNodesDo:[:each | s nextPut:each name]].
 
"
generate code for a block (aka lambda) to evaluate it; this makes a string like:
Line 4,068 ⟶ 4,071:
"
code := '[' , ((varNames collect:[:nm | ':',nm]) asString), ' | ' , expr , ']'.
 
"
eval the code, to get the block
Anonymous user