[svn:parrot] r42273 - in trunk: runtime/parrot/library t/library

fperrad at svn.parrot.org fperrad at svn.parrot.org
Thu Nov 5 18:24:34 UTC 2009


Author: fperrad
Date: Thu Nov  5 18:24:33 2009
New Revision: 42273
URL: https://trac.parrot.org/parrot/changeset/42273

Log:
[library] genfile handles conditioned line

Modified:
   trunk/runtime/parrot/library/Configure.pir
   trunk/t/library/configure.t

Modified: trunk/runtime/parrot/library/Configure.pir
==============================================================================
--- trunk/runtime/parrot/library/Configure.pir	Thu Nov  5 18:07:03 2009	(r42272)
+++ trunk/runtime/parrot/library/Configure.pir	Thu Nov  5 18:24:33 2009	(r42273)
@@ -13,7 +13,9 @@
 
 =item variable substitution
 
-=item slash/backslash substitution (for Windows).
+=item slash/backslash substitution (for Windows)
+
+=item conditioned line #IF/UNLESS/ELSIF/ELSE
 
 =back
 
@@ -44,9 +46,10 @@
     .param string outfile
     .param pmc config
     $S0 = slurp(tmpl)
-    $S0 = subst_config($S0, config)
+    $S0 = conditioned_line($S0, config)
+    $S0 = interpolate_var($S0, config)
     $S1 = sysinfo .SYSINFO_PARROT_OS
-    $S0 = subst_slash($S0, $S1)
+    $S0 = replace_slash($S0, $S1)
     output(outfile, $S0)
     printerr "\n\tGenerating '"
     printerr outfile
@@ -96,7 +99,81 @@
     rethrow e
 .end
 
-.sub 'subst_config'
+.sub 'conditioned_line'
+    .param string content
+    .param pmc config
+    $P0 = split "\n", content
+    .local string result, line
+    .local int truth, former_truth
+    former_truth = 0
+    result = ''
+  L1:
+    unless $P0 goto L2
+    line = shift $P0
+    $I0 = index line, "#IF("
+    unless $I0 == 0 goto L10
+    $I0 = index line, "):"
+    if $I0 < 0 goto L3
+    $I1 = $I0 - 4
+    $S0 = substr line, 4, $I1
+    truth = cond_eval($S0, config)
+    former_truth = truth
+    unless truth goto L1
+    $I0 += 2
+    $S0 = substr line, $I0
+    goto L4
+  L10:
+    $I0 = index line, "#UNLESS("
+    unless $I0 == 0 goto L20
+    $I0 = index line, "):"
+    if $I0 < 0 goto L3
+    $I1 = $I0 - 8
+    $S0 = substr line, 8, $I1
+    truth = cond_eval($S0, config)
+    former_truth = not truth
+    if truth goto L1
+    $I0 += 2
+    $S0 = substr line, $I0
+    goto L4
+  L20:
+    $I0 = index line, "#ELSIF("
+    unless $I0 == 0 goto L30
+    $I0 = index line, "):"
+    if $I0 < 0 goto L3
+    if former_truth goto L1
+    $I1 = $I0 - 7
+    $S0 = substr line, 7, $I1
+    truth = cond_eval($S0, config)
+    former_truth = truth
+    unless truth goto L1
+    $I0 += 2
+    $S0 = substr line, $I0
+    goto L4
+  L30:
+    $I0 = index line, "#ELSE:"
+    unless $I0 == 0 goto L40
+    if former_truth goto L1
+    $S0 = substr line, 6
+    goto L4
+  L40:
+  L3:
+    $S0 = line
+  L4:
+    result .= $S0
+    result .= "\n"
+    goto L1
+  L2:
+    .return (result)
+.end
+
+.sub 'cond_eval'
+    .param string expr
+    .param pmc config
+    $I0 = config[expr]
+    .return ($I0)
+.end
+
+.sub 'interpolate_var'
     .param string content
     .param pmc config
     $P0 = split "\n", content
@@ -140,7 +217,7 @@
     .return (result)
 .end
 
-.sub 'subst_slash'
+.sub 'replace_slash'
     .param string str
     .param string OS
     unless OS == 'MSWin32' goto L1

Modified: trunk/t/library/configure.t
==============================================================================
--- trunk/t/library/configure.t	Thu Nov  5 18:07:03 2009	(r42272)
+++ trunk/t/library/configure.t	Thu Nov  5 18:24:33 2009	(r42273)
@@ -17,48 +17,85 @@
 
     load_bytecode 'Configure.pbc'
 
-    plan(12)
-    test_subst_config()
-    test_subst_slash()
+    plan(23)
+    test_conditioned_line()
+    test_interpolate_var()
+    test_replace_slash()
 .end
 
-.sub 'test_subst_config'
+.sub 'test_conditioned_line'
+    .local pmc config
+    config = new 'Hash'
+    config['foo'] = 1
+    config['bar'] = 0
+    config['baz'] = 1
+
+    $S0 = conditioned_line("plain text\nwithout #", config)
+    is($S0, "plain text\nwithout #\n", "plain text")
+
+    $S0 = conditioned_line("#IF(malformed", config)
+    is($S0, "#IF(malformed\n", "malformed")
+
+    $S0 = conditioned_line("#IF(foo):positive", config)
+    is($S0, "positive\n", "#IF positive")
+    $S0 = conditioned_line("#IF(bar):negative", config)
+    is($S0, "", "#IF negative")
+
+    $S0 = conditioned_line("#UNLESS(bar):positive", config)
+    is($S0, "positive\n", "#UNLESS positive")
+    $S0 = conditioned_line("#UNLESS(foo):negative", config)
+    is($S0, "", "#UNLESS negative")
+
+    $S0 = conditioned_line("#IF(foo):positive\n#ELSE:alternate", config)
+    is($S0, "positive\n", "#IF/ELSE positive")
+    $S0 = conditioned_line("#IF(bar):negative\n#ELSE:alternate", config)
+    is($S0, "alternate\n", "#IF/ELSE alternate")
+
+    $S0 = conditioned_line("#IF(foo):positive\n#ELSIF(baz):alternate", config)
+    is($S0, "positive\n", "#IF/ELSIF positive")
+    $S0 = conditioned_line("#IF(bar):negative\n#ELSIF(baz):alternate", config)
+    is($S0, "alternate\n", "#IF/ELSIF alternate")
+    $S0 = conditioned_line("#IF(bar):negative\n#ELSIF(bar):negative", config)
+    is($S0, "", "#IF/ELSIF negative")
+.end
+
+.sub 'test_interpolate_var'
     .local pmc config
     config = new 'Hash'
     config['foo'] = 'bar'
-    $S0 = subst_config("# plain text", config)
+    $S0 = interpolate_var("# plain text", config)
     is($S0, "# plain text\n", "plain text")
-    $S0 = subst_config("\t at echo foo", config)
+    $S0 = interpolate_var("\t at echo foo", config)
     is($S0, "\t at echo foo\n", "@ alone")
-    $S0 = subst_config("here @foo@ variable", config)
+    $S0 = interpolate_var("here @foo@ variable", config)
     is($S0, "here bar variable\n", "variable")
-    $S0 = subst_config("here @foo@ variable, and here @foo at .", config)
+    $S0 = interpolate_var("here @foo@ variable, and here @foo at .", config)
     is($S0, "here bar variable, and here bar.\n", "variable")
 .end
 
-.sub 'test_subst_slash'
+.sub 'test_replace_slash'
     $S1 = "path/to/somewhere/"
-    $S0 = subst_slash($S1, 'MSWin32')
+    $S0 = replace_slash($S1, 'MSWin32')
     is($S0, "path\\to\\somewhere\\", "paths on win32")
-    $S0 = subst_slash($S1, '*nix')
+    $S0 = replace_slash($S1, '*nix')
     is($S0, $S1, "paths on *nix")
 
     $S1 = "prove t/*.t"
-    $S0 = subst_slash($S1, 'MSWin32')
+    $S0 = replace_slash($S1, 'MSWin32')
     is($S0, "prove t\\\\*.t")
-    $S0 = subst_slash($S1, '*nix')
+    $S0 = replace_slash($S1, '*nix')
     is($S0, $S1)
 
     $S1 = "prove t//*.t"
-    $S0 = subst_slash($S1, 'MSWin32')
+    $S0 = replace_slash($S1, 'MSWin32')
     is($S0, "prove t/*.t")
-    $S0 = subst_slash($S1, '*nix')
+    $S0 = replace_slash($S1, '*nix')
     is($S0, "prove t/*.t")
 
     $S1 = "http:////host//paths//"
-    $S0 = subst_slash($S1, 'MSWin32')
+    $S0 = replace_slash($S1, 'MSWin32')
     is($S0, "http://host/paths/", "url on win32")
-    $S0 = subst_slash($S1, '*nix')
+    $S0 = replace_slash($S1, '*nix')
     is($S0, "http://host/paths/", "url on *nix")
 .end
 


More information about the parrot-commits mailing list