changeset 1175:7ae024f5d91b trunk

[svn] - libcalc (paranormal virtual evaluation machine): add rand() instruction, global registers (global_reg0-reg99).
author nenolod
date Fri, 08 Jun 2007 10:56:12 -0700
parents 8aab955fb114
children 44d28af95a23
files ChangeLog src/paranormal/libcalc/dict.c src/paranormal/libcalc/function.c
diffstat 3 files changed, 60 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Jun 08 08:51:30 2007 -0700
+++ b/ChangeLog	Fri Jun 08 10:56:12 2007 -0700
@@ -1,3 +1,11 @@
+2007-06-08 15:51:30 +0000  William Pitcock <nenolod@sacredspiral.co.uk>
+  revision [2542]
+  - remove unneeded externs
+  
+  trunk/src/paranormal/drawing.c |    2 --
+  1 file changed, 2 deletions(-)
+
+
 2007-06-08 14:33:14 +0000  Yoshiki Yazawa <yaz@cc.rim.or.jp>
   revision [2540]
   - move transport plugins into Transport subdirectory.
--- a/src/paranormal/libcalc/dict.c	Fri Jun 08 08:51:30 2007 -0700
+++ b/src/paranormal/libcalc/dict.c	Fri Jun 08 10:56:12 2007 -0700
@@ -27,6 +27,9 @@
 
 #include "dict.h"
 
+static int global_dict_initialized = 0;
+static symbol_dict_t global_dict;
+
 static void more_variables (symbol_dict_t *dict) {
   var_t *new_var;
 
@@ -40,15 +43,44 @@
   dict->variables = new_var;
 }
 
+static int dict_define_variable (symbol_dict_t *dict, const char *name) {
+  var_t *var;
+
+  if (dict->v_count >= dict->v_space)
+    more_variables (dict);
+
+  var = &dict->variables[dict->v_count];
+
+  var->value = 0.0;
+  var->name = g_strdup (name);
+
+  return dict->v_count++;
+}
+
 symbol_dict_t *dict_new (void) {
   symbol_dict_t *dict;
-  
-  dict = (symbol_dict_t *) g_malloc (sizeof(symbol_dict_t));
+
+  if (global_dict_initialized != 1) {
+    int i;
+
+    global_dict.v_count = 0;
+    global_dict.v_space = V_SPACE_INIT;
+    global_dict.variables = (var_t *) g_new(var_t, dict->v_space);
+    global_dict_initialized = 1;
+
+    for (i = 0; i < 100; i++) {
+      gchar tmpbuf[40];
+      snprintf(tmpbuf, 40, "global_reg%d", i);
+      dict_define_variable(&global_dict, tmpbuf);
+    }
+  }
+
+  dict = g_new(symbol_dict_t, 1);
 
   /* Allocate space for variables. */
   dict->v_count = 0;
   dict->v_space = V_SPACE_INIT;
-  dict->variables = (var_t *)g_malloc (dict->v_space * sizeof(var_t));
+  dict->variables = (var_t *) g_new (var_t, dict->v_space);
   
   return dict;
 }
@@ -67,23 +99,14 @@
   g_free (dict);
 }
 
-static int dict_define_variable (symbol_dict_t *dict, const char *name) {
-  var_t *var;
-
-  if (dict->v_count >= dict->v_space)
-    more_variables (dict);
-
-  var = &dict->variables[dict->v_count];
-
-  var->value = 0.0;
-  var->name = g_strdup (name);
-
-  return dict->v_count++;
-}
-
 int dict_lookup (symbol_dict_t *dict, const char *name) {
   int i;
 
+  for (i = 0; i < global_dict.v_count; i++) {
+    if (strcmp (global_dict.variables[i].name, name) == 0)
+      return -i;
+  }
+
   for (i = 0; i < dict->v_count; i++) {
     if (strcmp (dict->variables[i].name, name) == 0)
       return i;
@@ -95,5 +118,12 @@
 
 double *dict_variable (symbol_dict_t *dict, const char *var_name) {
   int id = dict_lookup (dict, var_name);
+
+  /* global variables are presented as negative offset. negating
+   * a negative number results in a positive offset. --nenolod
+   */
+  if (id < 0)
+    return &global_dict.variables[-id].value;
+
   return &dict->variables[id].value;
 }
--- a/src/paranormal/libcalc/function.c	Fri Jun 08 08:51:30 2007 -0700
+++ b/src/paranormal/libcalc/function.c	Fri Jun 08 10:56:12 2007 -0700
@@ -73,6 +73,10 @@
   return (y == 0) ? 0 : (x / y);
 }
 
+static double f_rand (ex_stack *stack) {
+  return rand() % pop (stack);
+}
+
 /* */
 
 static const func_t init[] = {
@@ -85,6 +89,7 @@
   { "log", f_log },
   { "if", f_if },
   { "div", f_div }
+  { "rand", f_rand }
 };
 
 int function_lookup (const char *name) {