summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-10-19 00:12:57 +0000
committerPaul Buetow <paul@buetow.org>2008-10-19 00:12:57 +0000
commit982e35bd0bd9bc9b55c0f898556c3e1831141258 (patch)
tree95d12d94983ac396c693f96825cc3beea8cad50c
parent41e590f05d295a40ba4633d493be1ffe28f16ddf (diff)
synonyms work
-rw-r--r--README16
-rw-r--r--docs/help.txt2
-rw-r--r--docs/pod/fype.1.gzbin6105 -> 6311 bytes
-rw-r--r--docs/pod/fype.html14
-rw-r--r--docs/pod/fype.man18
-rw-r--r--docs/pod/fype.pod13
-rw-r--r--docs/pod/fype.tex18
-rw-r--r--docs/pod/fype.txt16
-rw-r--r--docs/stats.txt6
-rw-r--r--docs/version.txt2
-rw-r--r--examples/synonyms.fy32
-rwxr-xr-xfypebin425663 -> 425827 bytes
-rw-r--r--src/build.h2
-rw-r--r--src/core/interpret.c21
-rw-r--r--src/core/symbol.c3
-rw-r--r--src/core/symbol.h2
-rw-r--r--tmp/test.fy31
17 files changed, 186 insertions, 10 deletions
diff --git a/README b/README
index 66a9695..5c8b9b6 100644
--- a/README
+++ b/README
@@ -205,6 +205,22 @@ VARIABLES
say foo;
}
+SYNONYMS TO VARIABLES/IDENTIFIERS
+ Each variable can have as many synonyms as wished. A synonym is another
+ name to access the content of a specific variable. Here is an example of
+ how to use synomyms:
+
+ my foo = "foo";
+ my bar = \foo;
+ foo = "bar";
+
+ # The synonym variable should now also set to "bar"
+ assert "bar" == bar;
+
+ Synonyms can be used for all kind of identifiers. It's not limited to
+ normal variables but can be also used for function and procedure names
+ etc.
+
BUILT IN FUNCTIONS
In Fype, operators are built in functions as well. The difference is,
that they may be written in infix notation instead in front of the
diff --git a/docs/help.txt b/docs/help.txt
index ad6748e..0e73284 100644
--- a/docs/help.txt
+++ b/docs/help.txt
@@ -1,4 +1,4 @@
-Fype v0.1-devel Build 9215
+Fype v0.1-devel Build 9219
Copyright by Paul C. Buetow (2005 - 2008) <fype@dev.buetow.org>
-e Executes given code string (see synopses)
-h Prints this help
diff --git a/docs/pod/fype.1.gz b/docs/pod/fype.1.gz
index e05d4c5..e39eee0 100644
--- a/docs/pod/fype.1.gz
+++ b/docs/pod/fype.1.gz
Binary files differ
diff --git a/docs/pod/fype.html b/docs/pod/fype.html
index b88a1b4..02c30c4 100644
--- a/docs/pod/fype.html
+++ b/docs/pod/fype.html
@@ -38,6 +38,7 @@
</ul>
<li><a href="#variables">VARIABLES</a></li>
+ <li><a href="#synonyms_to_variables_identifiers">SYNONYMS TO VARIABLES/IDENTIFIERS</a></li>
<li><a href="#built_in_functions">BUILT IN FUNCTIONS</a></li>
<ul>
@@ -310,6 +311,19 @@ not.</p>
<p>
</p>
<hr />
+<h1><a name="synonyms_to_variables_identifiers">SYNONYMS TO VARIABLES/IDENTIFIERS</a></h1>
+<p>Each variable can have as many synonyms as wished. A synonym is another name to access the content of a specific variable. Here is an example of how to use synomyms:</p>
+<pre>
+ my foo = &quot;foo&quot;;
+ my bar = \foo;
+ foo = &quot;bar&quot;;</pre>
+<pre>
+ # The synonym variable should now also set to &quot;bar&quot;
+ assert &quot;bar&quot; == bar;</pre>
+<p>Synonyms can be used for all kind of identifiers. It's not limited to normal variables but can be also used for function and procedure names etc.</p>
+<p>
+</p>
+<hr />
<h1><a name="built_in_functions">BUILT IN FUNCTIONS</a></h1>
<p>In Fype, operators are built in functions as well. The difference is, that they may be written in infix notation instead in front of the arguments. The types inside the () specify the return types.</p>
<p>
diff --git a/docs/pod/fype.man b/docs/pod/fype.man
index b3d89bf..42daf43 100644
--- a/docs/pod/fype.man
+++ b/docs/pod/fype.man
@@ -129,7 +129,7 @@
.\" ========================================================================
.\"
.IX Title "FYPE 1"
-.TH FYPE 1 "2008-09-06" "Fype v0.1-devel Build 9208" "The Fype Users Manual Page"
+.TH FYPE 1 "2008-10-19" "Fype v0.1-devel Build 9219" "The Fype Users Manual Page"
.SH "NAME"
\&\fBFype\fR is \fBF\fRor \fBY\fRour \fBP\fRrogram \fBE\fRxecution
.PP
@@ -345,6 +345,22 @@ not.
\& say foo;
\& }
.Ve
+.SH "SYNONYMS TO VARIABLES/IDENTIFIERS"
+.IX Header "SYNONYMS TO VARIABLES/IDENTIFIERS"
+Each variable can have as many synonyms as wished. A synonym is another name to access the content of a specific variable. Here is an example of how to use synomyms:
+.PP
+.Vb 3
+\& my foo = "foo";
+\& my bar = \efoo;
+\& foo = "bar";
+.Ve
+.PP
+.Vb 2
+\& # The synonym variable should now also set to "bar"
+\& assert "bar" == bar;
+.Ve
+.PP
+Synonyms can be used for all kind of identifiers. It's not limited to normal variables but can be also used for function and procedure names etc.
.SH "BUILT IN FUNCTIONS"
.IX Header "BUILT IN FUNCTIONS"
In Fype, operators are built in functions as well. The difference is, that they may be written in infix notation instead in front of the arguments. The types inside the () specify the return types.
diff --git a/docs/pod/fype.pod b/docs/pod/fype.pod
index 2e41645..2773c4e 100644
--- a/docs/pod/fype.pod
+++ b/docs/pod/fype.pod
@@ -219,6 +219,19 @@ not.
say foo;
}
+=head1 SYNONYMS TO VARIABLES/IDENTIFIERS
+
+Each variable can have as many synonyms as wished. A synonym is another name to access the content of a specific variable. Here is an example of how to use synomyms:
+
+ my foo = "foo";
+ my bar = \foo;
+ foo = "bar";
+
+ # The synonym variable should now also set to "bar"
+ assert "bar" == bar;
+
+Synonyms can be used for all kind of identifiers. It's not limited to normal variables but can be also used for function and procedure names etc.
+
=head1 BUILT IN FUNCTIONS
In Fype, operators are built in functions as well. The difference is, that they may be written in infix notation instead in front of the arguments. The types inside the () specify the return types.
diff --git a/docs/pod/fype.tex b/docs/pod/fype.tex
index bcdea5f..19fa9f2 100644
--- a/docs/pod/fype.tex
+++ b/docs/pod/fype.tex
@@ -272,6 +272,24 @@ not.
say foo;
}
\end{verbatim}
+\section{SYNONYMS TO VARIABLES/IDENTIFIERS\label{SYNONYMS_TO_VARIABLES_IDENTIFIERS}\index{SYNONYMS TO VARIABLES/IDENTIFIERS}}
+
+
+Each variable can have as many synonyms as wished. A synonym is another name to access the content of a specific variable. Here is an example of how to use synomyms:
+
+\begin{verbatim}
+ my foo = "foo";
+ my bar = \foo;
+ foo = "bar";
+\end{verbatim}
+\begin{verbatim}
+ # The synonym variable should now also set to "bar"
+ assert "bar" == bar;
+\end{verbatim}
+
+
+Synonyms can be used for all kind of identifiers. It's not limited to normal variables but can be also used for function and procedure names etc.
+
\section{BUILT IN FUNCTIONS\label{BUILT_IN_FUNCTIONS}\index{BUILT IN FUNCTIONS}}
diff --git a/docs/pod/fype.txt b/docs/pod/fype.txt
index 66a9695..5c8b9b6 100644
--- a/docs/pod/fype.txt
+++ b/docs/pod/fype.txt
@@ -205,6 +205,22 @@ VARIABLES
say foo;
}
+SYNONYMS TO VARIABLES/IDENTIFIERS
+ Each variable can have as many synonyms as wished. A synonym is another
+ name to access the content of a specific variable. Here is an example of
+ how to use synomyms:
+
+ my foo = "foo";
+ my bar = \foo;
+ foo = "bar";
+
+ # The synonym variable should now also set to "bar"
+ assert "bar" == bar;
+
+ Synonyms can be used for all kind of identifiers. It's not limited to
+ normal variables but can be also used for function and procedure names
+ etc.
+
BUILT IN FUNCTIONS
In Fype, operators are built in functions as well. The difference is,
that they may be written in infix notation instead in front of the
diff --git a/docs/stats.txt b/docs/stats.txt
index b49fcc8..ffca974 100644
--- a/docs/stats.txt
+++ b/docs/stats.txt
@@ -1,4 +1,4 @@
===> Num of C source files : 42
-===> Num of C source lines : 7460
-===> Num of Fype source examples : 13
-===> Num of Fype source lines : 321
+===> Num of C source lines : 7482
+===> Num of Fype source examples : 14
+===> Num of Fype source lines : 353
diff --git a/docs/version.txt b/docs/version.txt
index 41b77c9..84a6485 100644
--- a/docs/version.txt
+++ b/docs/version.txt
@@ -1 +1 @@
-Fype v0.1-devel Build 9215
+Fype v0.1-devel Build 9219
diff --git a/examples/synonyms.fy b/examples/synonyms.fy
new file mode 100644
index 0000000..3ffb105
--- /dev/null
+++ b/examples/synonyms.fy
@@ -0,0 +1,32 @@
+#*
+ * Examples of how to use synonyms
+ *#
+
+# Create a variable foo, and bar is a synonym for foo
+my foo = "foo";
+my bar = \foo;
+
+# Reset the value of foo
+foo = "bar";
+
+# The synonym variable should now also set to "bar"
+assert "bar" == say bar;
+
+# Create a new procedure baz
+proc baz {
+ say "I am baz";
+}
+
+# Make a synonym baz, and undefine baz
+my bay = \baz;
+undef baz;
+
+# bay still has a reference of the original procedure baz
+bay; # this prints aut "I am baz"
+
+assert 0 == defined baz;
+assert 1 == defined bay;
+
+# This removes the procedure from memory
+undef bay;
+
diff --git a/fype b/fype
index ae7db04..f36669e 100755
--- a/fype
+++ b/fype
Binary files differ
diff --git a/src/build.h b/src/build.h
index c6f0270..4e73ee5 100644
--- a/src/build.h
+++ b/src/build.h
@@ -35,7 +35,7 @@
#ifndef BUILD_H
#define BUILD_H
-#define BUILDNR 9215
+#define BUILDNR 9233
#define OS_FREEBSD
#endif
diff --git a/src/core/interpret.c b/src/core/interpret.c
index a69e85a..2c80163 100644
--- a/src/core/interpret.c
+++ b/src/core/interpret.c
@@ -222,6 +222,22 @@ _var_assign(Interpret *p_interpret) {
if (p_interpret->tt == TT_ASSIGN) {
_NEXT
+ if (p_interpret->tt == TT_VID) {
+ _NEXT
+ if (p_interpret->tt != TT_IDENT)
+ _INTERPRET_ERROR("Expected identifier", p_interpret->p_token);
+
+ char *c_name_ = token_get_val(p_interpret->p_token);
+ Symbol *p_symbol = scope_get(p_interpret->p_scope, c_name_);
+
+ if (p_symbol == NULL)
+ _INTERPRET_ERROR("No such symbol", p_interpret->p_token);
+
+ symbol_ref_up(p_symbol);
+ scope_newset(p_interpret->p_scope, c_name, p_symbol);
+ _NEXT
+
+ } else {
Stack *p_stack = p_interpret->p_stack;
p_interpret->p_stack = stack_new();
@@ -236,11 +252,10 @@ _var_assign(Interpret *p_interpret) {
p_token = stack_top(p_interpret->p_stack);
Symbol *p_symbol = symbol_new(SYM_VARIABLE, p_token);
scope_newset(p_interpret->p_scope, c_name, p_symbol);
-
} else {
return (0);
}
-
+ }
} else {
Token *p_token = token_new_integer(0);
Symbol *p_symbol = symbol_new(SYM_VARIABLE, p_token);
@@ -912,6 +927,7 @@ _term(Interpret *p_interpret) {
}
break;
+ /*
case TT_PARANT_AL:
{
Token *p_token = p_interpret->p_token;
@@ -922,6 +938,7 @@ _term(Interpret *p_interpret) {
_INTERPRET_ERROR("arrays not yet fully implemented", p_token_arr);
}
break;
+ */
case TT_PARANT_L:
{
diff --git a/src/core/symbol.c b/src/core/symbol.c
index 2343cdb..37a0cae 100644
--- a/src/core/symbol.c
+++ b/src/core/symbol.c
@@ -42,12 +42,14 @@ symbol_new(SymbolType sym, void *p_val) {
p_symbol->sym = sym;
p_symbol->p_val = p_val;
+ p_symbol->i_refs = 1;
return p_symbol;
}
void
symbol_delete(Symbol *p_symbol) {
+ if (--p_symbol->i_refs == 0) {
switch (symbol_get_sym(p_symbol)) {
case SYM_PROCEDURE:
{
@@ -58,6 +60,7 @@ symbol_delete(Symbol *p_symbol) {
NO_DEFAULT;
}
free(p_symbol);
+ }
}
void
diff --git a/src/core/symbol.h b/src/core/symbol.h
index 2bd248c..a077bd6 100644
--- a/src/core/symbol.h
+++ b/src/core/symbol.h
@@ -41,6 +41,7 @@
#define symbol_set_sym(s,st) s->sym = st
#define symbol_get_val(s) s->p_val
#define symbol_get_sym(s) s->sym
+#define symbol_ref_up(s) ++s->i_refs
#define IS_A_FUNCTION(s) (s == SYM_INLINEFUNCTION || s == SYM_FUNCTION)
#define IS_NOT_A_FUNCTION(s) !IS_A_FUNCTION(s)
@@ -55,6 +56,7 @@ typedef enum {
typedef struct {
SymbolType sym;
void *p_val;
+ unsigned i_refs;
} Symbol;
Symbol* symbol_new(SymbolType sym, void *p_val);
diff --git a/tmp/test.fy b/tmp/test.fy
index afe2cec..3ffb105 100644
--- a/tmp/test.fy
+++ b/tmp/test.fy
@@ -1,3 +1,32 @@
+#*
+ * Examples of how to use synonyms
+ *#
+# Create a variable foo, and bar is a synonym for foo
+my foo = "foo";
+my bar = \foo;
+
+# Reset the value of foo
+foo = "bar";
+
+# The synonym variable should now also set to "bar"
+assert "bar" == say bar;
+
+# Create a new procedure baz
+proc baz {
+ say "I am baz";
+}
+
+# Make a synonym baz, and undefine baz
+my bay = \baz;
+undef baz;
+
+# bay still has a reference of the original procedure baz
+bay; # this prints aut "I am baz"
+
+assert 0 == defined baz;
+assert 1 == defined bay;
+
+# This removes the procedure from memory
+undef bay;
-my foo = [1, 2.2, "3"];