Skip to content

Commit c1b26ed

Browse files
committed
anonymous functions defined via brackets can now be easily combined...
… i.e. this can be done: above = <move 0,-0.5,0> box above ball above peg
1 parent 8980a18 commit c1b26ed

File tree

2 files changed

+171
-20
lines changed

2 files changed

+171
-20
lines changed

coffee/core/code-preprocessor-tests.coffee

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ define ['core/code-preprocessor-tests'], (foo) ->
22132213
expected: """
22142214
a = box
22152215
noFill()
2216-
rotate -> run a, -> scale 2, -> run a
2216+
rotate -> run a -> scale 2, -> run a
22172217
"""
22182218
notIdempotent: true
22192219
failsMootAppends: true
@@ -3007,6 +3007,95 @@ define ['core/code-preprocessor-tests'], (foo) ->
30073007
"""
30083008
notIdempotent: true
30093009
failsMootAppends: true
3010+
,
3011+
notes: """
3012+
testing some more advanced higher-order-functions
3013+
examples
3014+
"""
3015+
input: """
3016+
drawPieces = [<box>, <move>,<ball>]
3017+
if random > 0.5
3018+
▶drawThis = drawPieces.reduce (acc,x) -> -> x(acc)
3019+
else
3020+
▶drawThis = drawPieces.reduceRight (acc,x) -> -> x(acc)
3021+
drawThis()
3022+
"""
3023+
expected: """
3024+
drawPieces = [box, move, ball]
3025+
if random() > 0.5
3026+
▶drawThis = drawPieces.reduce (acc,x) -> -> x(acc)
3027+
else
3028+
▶drawThis = drawPieces.reduceRight (acc,x) -> -> x(acc)
3029+
drawThis()
3030+
"""
3031+
notIdempotent: true
3032+
failsMootAppends: true
3033+
,
3034+
notes: """
3035+
testing some more advanced higher-order-functions
3036+
examples
3037+
"""
3038+
input: """
3039+
drawPieces = [<box>, <move>,<ball>]
3040+
rotate
3041+
▶if random > 0.5
3042+
▶▶drawThis = drawPieces.reduce (acc,x) -> -> x(acc)
3043+
▶else
3044+
▶▶drawThis = drawPieces.reduceRight (acc,x) -> -> x(acc)
3045+
▶drawThis()
3046+
"""
3047+
expected: """
3048+
drawPieces = [box, move, ball]
3049+
rotate ->
3050+
▶if random() > 0.5
3051+
▶▶drawThis = drawPieces.reduce (acc,x) -> -> x(acc)
3052+
▶else
3053+
▶▶drawThis = drawPieces.reduceRight (acc,x) -> -> x(acc)
3054+
▶drawThis()
3055+
"""
3056+
notIdempotent: true
3057+
failsMootAppends: true
3058+
3059+
,
3060+
notes: """
3061+
testing some more advanced higher-order-functions
3062+
examples
3063+
"""
3064+
input: """
3065+
rotate
3066+
▶box
3067+
peg
3068+
3069+
a = <move 1>
3070+
box a ball
3071+
"""
3072+
expected: """
3073+
rotate ->
3074+
▶box()
3075+
peg()
3076+
3077+
a = ((parametersForBracketedFunctions) -> (move 1, -> (if parametersForBracketedFunctions? then parametersForBracketedFunctions() else null)))
3078+
box a -> ball()
3079+
"""
3080+
notIdempotent: true
3081+
failsMootAppends: true
3082+
3083+
,
3084+
notes: """
3085+
testing some more advanced higher-order-functions
3086+
examples
3087+
"""
3088+
input: """
3089+
above = <move 0,-0.5,0>
3090+
box above ball above peg
3091+
"""
3092+
expected: """
3093+
above = ((parametersForBracketedFunctions) -> (move 0,-0.5,0, -> (if parametersForBracketedFunctions? then parametersForBracketedFunctions() else null)))
3094+
box above -> ball above -> peg()
3095+
"""
3096+
notIdempotent: true
3097+
failsMootAppends: true
3098+
30103099

30113100
]
30123101

coffee/core/code-preprocessor.coffee

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,11 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
776776
code = code.replace(/([\w\d])(.*?) times[:]?([^\w\d])/g, "($1$2).times -> $3")
777777
if detailedDebug then console.log "transformTimesSyntax-7\n" + code + " error: " + error
778778

779-
code = code.replace(/;*[\t ]*else/g, " else")
779+
code = code.replace(/;+[\t ]*else/g, " else")
780+
if detailedDebug then console.log "transformTimesSyntax-8\n" + code + " error: " + error
781+
782+
code = code.replace(/^(\t*) else/gm, "$1else")
783+
if detailedDebug then console.log "transformTimesSyntax-9\n" + code + " error: " + error
780784

781785
return @normaliseCode(code, error)
782786

@@ -867,12 +871,12 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
867871

868872
return [code, error]
869873

870-
adjustImplicitCalls: (code, error, userDefinedFunctions, userDefinedFunctionsWithArguments) ->
874+
adjustImplicitCalls: (code, error, userDefinedFunctions, userDefinedFunctionsWithArguments, bracketsVariables) ->
871875
# if there is an error, just propagate it
872876
return [undefined, error] if error?
873877

874-
expressionsAndUserDefinedFunctionsRegex = @expressionsRegex + userDefinedFunctions
875-
allFunctionsRegex = @allCommandsRegex + "|" + expressionsAndUserDefinedFunctionsRegex
878+
expressionsAndUserDefinedFunctionsRegex = @expressionsRegex + userDefinedFunctions + bracketsVariables
879+
allFunctionsRegex = @allCommandsRegex + "|" + expressionsAndUserDefinedFunctionsRegex + bracketsVariables
876880

877881

878882
# adding () to single tokens on their own at the start of a line
@@ -914,7 +918,8 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
914918
delimitersForCommands = ":|;|\\,|\\?|\\)|//|\\#|\\s+if|\\s+else|\\s+then"
915919
delimitersForExpressions = delimitersForCommands + "|" + "\\+|-|\\*|/|%|&|]|<|>|==|!=|>=|<=|!(?![=])|\\s+and\\s+|\\s+or\\s+|\\s+not\\s+|\\|"
916920

917-
rx = RegExp("([^\\w\\d\\r\\n])("+@allCommandsRegex+")[ \\t]*("+delimitersForCommands+")",'g')
921+
if detailedDebug then console.log "adjustImplicitCalls-4 brackets vars:" + bracketsVariables
922+
rx = RegExp("([^\\w\\d\\r\\n])("+@allCommandsRegex+bracketsVariables+")[ \\t]*("+delimitersForCommands+")",'g')
918923
for i in [1..2]
919924
code = code.replace(rx, "$1$2()$3")
920925
if detailedDebug then console.log "adjustImplicitCalls-4\n" + code + " error: " + error
@@ -971,7 +976,7 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
971976
# it easy to change qualities of some
972977
# primitives without affecting the
973978
# primitives that come afterwards.
974-
findQualifiers: (code, error) ->
979+
findQualifiers: (code, error, bracketsVariables) ->
975980
# if there is an error, just propagate it
976981
return [undefined, error] if error?
977982

@@ -996,14 +1001,14 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
9961001
# making sure that the qualifiers can't span
9971002
# a function definition
9981003

999-
primitivesAndMatrixAndDiamondRegex = @primitivesAndMatrixRegex + '|♦'
1004+
primitivesAndMatrixAndDiamondRegex = @primitivesAndMatrixRegex + bracketsVariables + '|♦'
10001005

10011006
previousCodeTransformations = ''
10021007
code = code.replace(/->/g, "")
10031008
while code != previousCodeTransformations
10041009
previousCodeTransformations = code
10051010

1006-
rx = RegExp("(^|[^\\w\\d\\r\\n])("+@primitivesAndMatrixRegex+")(?![\\w\\d\\(])([^\\r\\n;'♠→]*?)("+primitivesAndMatrixAndDiamondRegex+")([^\\w\\d\\r\\n]*)",'gm')
1011+
rx = RegExp("(^|[^\\w\\d\\r\\n])("+@primitivesAndMatrixRegex+bracketsVariables+")(?![\\w\\d\\(])([^\\r\\n;'♠→]*?)("+primitivesAndMatrixAndDiamondRegex+")([^\\w\\d\\r\\n]*)",'gm')
10071012
replacement = '$1$2ing❤QUALIFIER$3$4$5'
10081013
code = code.replace(rx,replacement)
10091014

@@ -1014,7 +1019,7 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
10141019

10151020
return [code, error]
10161021

1017-
fleshOutQualifiers: (code, error) ->
1022+
fleshOutQualifiers: (code, error, bracketsVariables, bracketsVariablesArray) ->
10181023
# if there is an error, just propagate it
10191024
return [undefined, error] if error?
10201025

@@ -1032,6 +1037,10 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
10321037
primtvsAndQualsRegex = primtvsAndQualsRegex + @qualifyingCommands[i] + '|' + @qualifyingCommands[i]+"ing❤QUALIFIER|"
10331038
for i in [0...@primitives.length]
10341039
primtvsAndQualsRegex = primtvsAndQualsRegex + @primitives[i] + '|' + @primitives[i]+"ing❤QUALIFIER|"
1040+
for i in [0...bracketsVariablesArray.length]
1041+
primtvsAndQualsRegex = primtvsAndQualsRegex + bracketsVariablesArray[i] + '|' + bracketsVariablesArray[i]+"ing❤QUALIFIER|"
1042+
1043+
10351044
primtvsAndQualsRegex = primtvsAndQualsRegex + ''
10361045

10371046
previousCodeTransformations = ''
@@ -1047,15 +1056,18 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
10471056
while code != previousCodeTransformations
10481057
previousCodeTransformations = code
10491058

1050-
rx = RegExp("(^|[^\\w\\d\\r\\n])(("+@primitivesAndMatrixRegex+")ing❤QUALIFIER)(?![\\w\\d\\(])([^\\r\\n;→]*?)("+primtvsAndQualsRegex+")([^;\\r\\n]*)(.*)",'gm')
1059+
if detailedDebug then console.log "fleshOutQualifiers 0: @primitivesAndMatrixRegex: " + @primitivesAndMatrixRegex + " bracketsVariables: " + bracketsVariables + " primtvsAndQualsRegex: " + primtvsAndQualsRegex
1060+
1061+
rx = RegExp("(^|[^\\w\\d\\r\\n])(("+@primitivesAndMatrixRegex+bracketsVariables+")ing❤QUALIFIER)(?![\\w\\d\\(])([^\\r\\n;→]*?)("+primtvsAndQualsRegex+")([^;\\r\\n]*)(.*)",'gm')
10511062
replacement = '$1$3$4→ $5$6;$7'
10521063
code = code.replace(rx,replacement)
1064+
if detailedDebug then console.log "fleshOutQualifiers 1: " + code
10531065

1054-
rx = RegExp("(^|[^\\w\\d\\r\\n])(("+@primitivesAndMatrixRegex+")ing❤QUALIFIER)(?![\\w\\d\\(])([^\\r\\n;→♦❤]*?)♦",'g')
1066+
rx = RegExp("(^|[^\\w\\d\\r\\n])(("+@primitivesAndMatrixRegex+bracketsVariables+")ing❤QUALIFIER)(?![\\w\\d\\(])([^\\r\\n;→♦❤]*?)♦",'g')
10551067
replacement = '$1$3$4 →'
10561068
code = code.replace(rx,replacement)
10571069

1058-
if detailedDebug then console.log "fleshOutQualifiers 6: " + code
1070+
if detailedDebug then console.log "fleshOutQualifiers 2: " + code
10591071

10601072
# the trasformations above creates
10611073
# stuff like:
@@ -1066,7 +1078,8 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
10661078
# we don't need the diamond anymore
10671079
code = code.replace(/[♦\t ]*/g, "; ")
10681080

1069-
code = code.replace(/;*[\t ]*else/gm, " else")
1081+
code = code.replace(/;+[\t ]*else/gm, " else")
1082+
code = code.replace(/^(\t*) else/gm, "$1else")
10701083

10711084
# the trasformations above add lots of redundant
10721085
# semicolons and spaces like so:
@@ -1079,7 +1092,7 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
10791092
code = code.replace(/\s*->/g, "->")
10801093
if detailedDebug then console.log "fleshOutQualifiers 7: " + code
10811094

1082-
rx = RegExp("(^|[^\\w\\d\\r\\n])("+@primitivesAndMatrixRegex+")(?![\\w\\d\\(])(\\s*\\(?→)",'gm')
1095+
rx = RegExp("(^|[^\\w\\d\\r\\n])("+@primitivesAndMatrixRegex+bracketsVariables+")(?![\\w\\d\\(])(\\s*\\(?→)",'gm')
10831096
replacement = '$1$2 ->'
10841097
code = code.replace(rx,replacement)
10851098
if detailedDebug then console.log "fleshOutQualifiers 9: " + code
@@ -1093,7 +1106,8 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
10931106
code = code.replace(//g, "->")
10941107
if detailedDebug then console.log "fleshOutQualifiers 11: " + code
10951108

1096-
code = code.replace(/;*[\t ]*else/g, " else")
1109+
code = code.replace(/;+[\t ]*else/g, " else")
1110+
code = code.replace(/^(\t*) else/gm, "$1else")
10971111
code = code.replace(/;*[\t ]*then/g, " then")
10981112

10991113
return [code, error]
@@ -1174,6 +1188,44 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
11741188
#console.log "*****" + userDefinedFunctions
11751189
return [code, error, userDefinedFunctions, userDefinedFunctionsWithArguments]
11761190

1191+
findBracketVariables: (code, error) ->
1192+
# if there is an error, just propagate it
1193+
return [undefined, error] if error?
1194+
1195+
bracketsVariablesArray = []
1196+
1197+
# form a = <...
1198+
rx = RegExp("([a-zA-Z\\d]+)([ \\t]*)=[ \\t]*<",'gm')
1199+
while match = rx.exec code
1200+
bracketsVariablesArray.push(match[1])
1201+
#@primitives.push(match[1])
1202+
if detailedDebug then console.log "findbracketsVariables-1 pushing " + match[1]
1203+
if detailedDebug then console.log "findbracketsVariables-2\n" + code + " error: " + error
1204+
1205+
1206+
bracketsVariables = bracketsVariablesArray.join "|"
1207+
if bracketsVariables != ""
1208+
bracketsVariables = "|"+bracketsVariables
1209+
1210+
if detailedDebug then console.log "bracketsVariables: >" + bracketsVariables + "<"
1211+
1212+
rx = RegExp("([a-zA-Z\\d]+)([ \\t]*)=[ \\t]*<",'gm')
1213+
code = code.replace(rx, "BRACKETVAR$1BRACKETVAR = <")
1214+
if detailedDebug then console.log "findbracketsVariables-3\n" + code + " error: " + error
1215+
1216+
return [code, error, bracketsVariables, bracketsVariablesArray]
1217+
1218+
putBackBracketVarOriginalName: (code, error) ->
1219+
# if there is an error, just propagate it
1220+
return [undefined, error] if error?
1221+
1222+
1223+
rx = RegExp("BRACKETVAR([a-zA-Z\\d]+)BRACKETVAR",'gm')
1224+
code = code.replace(rx, "$1")
1225+
if detailedDebug then console.log "putBackBracketVarOriginalName-1\n" + code + " error: " + error
1226+
1227+
return [code, error]
1228+
11771229

11781230
evaluateAllExpressions: (code, error, userDefinedFunctions) ->
11791231
# if there is an error, just propagate it
@@ -1496,7 +1548,7 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
14961548
return [code, error]
14971549

14981550

1499-
preprocess: (code) ->
1551+
preprocess: (code, bracketsVariables) ->
15001552
# we'll keep any errors in here as we transform the code
15011553
# as soon as there is any error, all next stages of
15021554
# transformation do nothing
@@ -1506,6 +1558,14 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
15061558
[code, error, userDefinedFunctions, userDefinedFunctionsWithArguments] = @findUserDefinedFunctions(code, error)
15071559
if detailedDebug then console.log "preprocess-0.5\n" + code + " error: " + error
15081560

1561+
[code, error, bracketsVariables, bracketsVariablesArray] = @findBracketVariables(code, error)
1562+
if detailedDebug then console.log "preprocess-0.7\n" + code + " error: " + error
1563+
1564+
#@qualifyingCommandsRegex = @qualifyingCommands + bracketsVariables
1565+
#console.log "all commands plus bracket variables BEFORE: " + @primitivesAndMatrixRegex + bracketsVariables
1566+
#@allCommandsRegex = @allCommandsRegex + bracketsVariables
1567+
#console.log "all commands plus bracket variables: " + @primitivesAndMatrixRegex + bracketsVariables
1568+
15091569
[code, error] = @removeTickedDoOnce(code, error)
15101570
if detailedDebug then console.log "preprocess-2\n" + code + " error: " + error
15111571
[code, codeWithoutStringsOrComments, error] = @stripCommentsAndStrings(code, error)
@@ -1573,15 +1633,15 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
15731633
if detailedDebug then console.log "preprocess-9.2\n" + code + " error: " + error
15741634
[code, error] = @unbindFunctionsToArguments(code, error)
15751635
if detailedDebug then console.log "preprocess-9.5\n" + code + " error: " + error
1576-
[code, error] = @findQualifiers(code, error)
1636+
[code, error] = @findQualifiers(code, error,bracketsVariables)
15771637
if detailedDebug then console.log "preprocess-10\n" + code + " error: " + error
1578-
[code, error] = @fleshOutQualifiers(code, error)
1638+
[code, error] = @fleshOutQualifiers(code, error,bracketsVariables, bracketsVariablesArray)
15791639
if detailedDebug then console.log "preprocess-11\n" + code + " error: " + error
15801640
[code, error] = @adjustFunctionalReferences(code, error, userDefinedFunctions)
15811641
if detailedDebug then console.log "preprocess-17\n" + code + " error: " + error
15821642
[code, error] = @addCommandsSeparations(code, error, userDefinedFunctions)
15831643
if detailedDebug then console.log "preprocess-12\n" + code + " error: " + error
1584-
[code, error] = @adjustImplicitCalls(code, error, userDefinedFunctions, userDefinedFunctionsWithArguments)
1644+
[code, error] = @adjustImplicitCalls(code, error, userDefinedFunctions, userDefinedFunctionsWithArguments, bracketsVariables)
15851645
if detailedDebug then console.log "preprocess-13\n" + code + " error: " + error
15861646
[code, error] = @adjustDoubleSlashSyntaxForComments(code, error)
15871647
if detailedDebug then console.log "preprocess-14\n" + code + " error: " + error
@@ -1591,6 +1651,8 @@ define ['core/code-preprocessor-tests', 'core/colour-literals'], (CodePreprocess
15911651
if detailedDebug then console.log "preprocess-17\n" + code + " error: " + error
15921652
[code, error] = @fixParamPassingInBracketedFunctions(code, error, userDefinedFunctions)
15931653
if detailedDebug then console.log "preprocess-17.5\n" + code + " error: " + error
1654+
[code, error] = @putBackBracketVarOriginalName(code, error)
1655+
if detailedDebug then console.log "preprocess-17.7\n" + code + " error: " + error
15941656
[code, error] = @beautifyCode(code, error)
15951657
if detailedDebug then console.log "preprocess-18\n" + code + " error: " + error
15961658

0 commit comments

Comments
 (0)