summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2011-04-22 18:24:59 -0400
committerTavian Barnes <tavianator@gmail.com>2011-04-22 18:25:09 -0400
commit13e11b2fafe60cad7d04d80c602adc12d6080036 (patch)
treea36a93bc780b9b9e0eec30a486d8a4a9a6ddbd24
parent4e3c3918450d004bc00de4ad58eb7b707156f9aa (diff)
downloaddimension-13e11b2fafe60cad7d04d80c602adc12d6080036.tar.xz
Handle symbol aliases in dmnsn_find_symbol() directly.
Fixes infinite loops when macro arguments have the same name as existing symbols.
-rw-r--r--dimension/common.rules8
-rw-r--r--dimension/parse.c9
-rw-r--r--tests/dimension/directives.pov12
-rwxr-xr-xtests/dimension/directives.sh16
4 files changed, 36 insertions, 9 deletions
diff --git a/dimension/common.rules b/dimension/common.rules
index 9383d4b..a987c69 100644
--- a/dimension/common.rules
+++ b/dimension/common.rules
@@ -22,14 +22,8 @@
/* Fundamental language elements */
IDENTIFIER: "identifier" {
- const char *id = $1;
- dmnsn_astnode *symbol = dmnsn_find_symbol(symtable, id);
- while (symbol && symbol->type == DMNSN_AST_IDENTIFIER) {
- id = symbol->ptr;
- symbol = dmnsn_find_symbol(symtable, id);
- }
$$ = dmnsn_new_astleaf(DMNSN_AST_IDENTIFIER, @$);
- $$.ptr = dmnsn_strdup(id);
+ $$.ptr = dmnsn_strdup($1);
dmnsn_free($1);
}
;
diff --git a/dimension/parse.c b/dimension/parse.c
index 74075a7..6651d41 100644
--- a/dimension/parse.c
+++ b/dimension/parse.c
@@ -131,8 +131,13 @@ dmnsn_find_symbol(dmnsn_symbol_table *symtable, const char *id)
DMNSN_ARRAY_FOREACH_REVERSE (dmnsn_dictionary **, dict, symtable) {
symbol = dmnsn_dictionary_at(*dict, id);
- if (symbol)
- break;
+ if (symbol) {
+ if (symbol->type == DMNSN_AST_IDENTIFIER) {
+ id = symbol->ptr;
+ } else {
+ break;
+ }
+ }
}
return symbol;
diff --git a/tests/dimension/directives.pov b/tests/dimension/directives.pov
index 089622c..5c3cfa3 100644
--- a/tests/dimension/directives.pov
+++ b/tests/dimension/directives.pov
@@ -64,6 +64,18 @@
Inc(Counter)
#end
+// Test macro parameters with the same name as existing variables
+#declare Test1 = 0;
+#declare Test2 = 1;
+#declare Test3 = 2;
+#macro ScopeTest(Test1, Test2, Test3)
+ #declare Test1 = Test2 - Test3;
+#end
+ScopeTest(Test1, Test3, Test2)
+sphere {
+ 0, Test1
+}
+
#declare Box =
box {
<-1, -1, -1>, <1, 1, 1>
diff --git a/tests/dimension/directives.sh b/tests/dimension/directives.sh
index 7c66263..9e3974a 100755
--- a/tests/dimension/directives.sh
+++ b/tests/dimension/directives.sh
@@ -56,6 +56,17 @@ directives_exp="$(echo -n \
(identifier "Inc") \( (identifier "Counter") \)
#end
+ #declare (identifier "Test1") = (integer "0") ;
+ #declare (identifier "Test2") = (integer "1") ;
+ #declare (identifier "Test3") = (integer "2") ;
+ #macro (identifier "ScopeTest") \( (identifier "Test1") , (identifier "Test2") , (identifier "Test3") \)
+ #declare (identifier "Test1") = (identifier "Test2") - (identifier "Test3") ;
+ #end
+ (identifier "ScopeTest") \( (identifier "Test1") , (identifier "Test3") , (identifier "Test2") \)
+ sphere {
+ (integer "0") , (identifier "Test1")
+ }
+
#declare (identifier "Box") =
box {
< - (integer "1") , - (integer "1") , - (integer "1") > ,
@@ -90,6 +101,11 @@ $(echo -n \
(vector (integer 1) (integer 1) (integer 1) (integer 0) (integer 0))
pigment-modifiers)))
(object
+ (sphere
+ (vector (integer 0) (integer 0) (integer 0) (integer 0) (integer 0))
+ (integer 1))
+ object-modifiers)
+ (object
(box
(vector (integer -1) (integer -1) (integer -1) (integer 0) (integer 0))
(vector (integer 1) (integer 1) (integer 1) (integer 0) (integer 0)))