Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Swap all the code out to Intern Strings, or Symbols.

  • Loading branch information...
commit 2aca1e7f7023c8dcff56402b30413e8194e951f9 1 parent cfc5584
@atrodo authored
Showing with 60 additions and 8 deletions.
  1. +8 −5 core.c
  2. +36 −0 interp.c
  3. +3 −0  interp.h
  4. +13 −3 lorito.h
View
13 core.c
@@ -296,8 +296,8 @@ core_exec(Lorito_Interp *interp)
$P(op->dest) = $P(op->src1);
break;
case OP_STR:
- $S(op->dest)->size = $S(op->src1)->size;
- $S(op->dest)->data = $S(op->src1)->data;
+ $S(op->dest) = $S(op->src1);
+ //$S(op->dest)->data = $S(op->src1)->data;
break;
case OP_NUM:
$N(op->dest) = $N(op->src1);
@@ -330,10 +330,13 @@ core_exec(Lorito_Interp *interp)
INVALID_OP("load_const: outside range");
}
+ $S(op->dest) = lorito_string(interp, length, &((char *) data->data)[offset]);
+ /*
$S(op->dest) = malloc(sizeof(Lorito_Str));
$S(op->dest)->size = length;
$S(op->dest)->data = malloc(sizeof(char) * length);
strcpy($S(op->dest)->data, &((char *) data->data)[offset]);
+ */
break;
}
case OP_INT:
@@ -702,7 +705,7 @@ core_exec(Lorito_Interp *interp)
switch (regtype)
{
case OP_STR:
- fprintf(stdout, "%s", $S(op->src1)->data);
+ fprintf(stdout, "%s", $S(op->src1)->original);
break;
case OP_INT:
fprintf(stdout, "%d", $I(op->src1));
@@ -715,7 +718,7 @@ core_exec(Lorito_Interp *interp)
switch (regtype)
{
case OP_STR:
- fprintf(stdout, "%s\n", $S(op->src1)->data);
+ fprintf(stdout, "%s\n", $S(op->src1)->original);
break;
case OP_INT:
fprintf(stdout, "%d\n", $I(op->src1));
@@ -728,7 +731,7 @@ core_exec(Lorito_Interp *interp)
switch (regtype)
{
case OP_STR:
- fprintf(stderr, "%s", $S(op->src1)->data);
+ fprintf(stderr, "%s", $S(op->src1)->original);
break;
case OP_INT:
fprintf(stderr, "%d", $I(op->src1));
View
36 interp.c
@@ -19,6 +19,9 @@ lorito_init()
result->files = NULL;
result->last_seg = NULL;
+ result->symbol_len = 0;
+ result->symbols = NULL;
+
result->magic = (unsigned short int) rand();
result->default_lookup = (Lorito_PMC *) lorito_c_method_new(result, lorito_pmc_default_lookup);
@@ -35,6 +38,39 @@ lorito_run(Lorito_Interp *interp)
return 0;
}
+Lorito_Str *
+lorito_string(Lorito_Interp *interp, int length, const char *original)
+{
+ int i = 0;
+
+ // See if this char * has been entered before
+ for (i = 0; i < interp->symbol_len; i++)
+ {
+ if (*original == *interp->symbols[i].original)
+ return &interp->symbols[i];
+ }
+
+ // See if it compares with an existing symbol
+ for (i = 0; i < interp->symbol_len; i++)
+ {
+ if (length != interp->symbols[i].length)
+ continue;
+ if (strncmp(original, interp->symbols[i].original, length))
+ {
+ return &interp->symbols[i];
+ }
+ }
+
+ // Otherwise, create one.
+ i = interp->symbol_len;
+ interp->symbol_len++;
+ interp->symbols = realloc(interp->symbols, interp->symbol_len * sizeof(Lorito_Str));
+
+ interp->symbols[i].length = length;
+ interp->symbols[i].original = original;
+ return &interp->symbols[i];
+}
+
void
lorito_push_arg(Lorito_Interp *interp, Lorito_Ctx *target, Lorito_PMC *item)
{
View
3  interp.h
@@ -9,6 +9,9 @@ lorito_init();
int
lorito_run(Lorito_Interp *interp);
+Lorito_Str *
+lorito_string(Lorito_Interp *interp, int length, const char *original);
+
void
lorito_push_arg(Lorito_Interp *interp, Lorito_Ctx *target, Lorito_PMC *item);
View
16 lorito.h
@@ -34,11 +34,16 @@ struct lorito_interp_t
unsigned short magic;
+ // Files
int allocated_file;
struct lorito_file_t **files;
//
struct lorito_codeseg_t *last_seg;
+ //Intern Strings (Symbols)
+ int symbol_len;
+ struct lorito_str_t *symbols;
+
// Special PMCs
struct lorito_pmc_t *default_lookup;
struct lorito_pmc_t *null_pmc;
@@ -69,7 +74,12 @@ struct lorito_reg_t
};
typedef struct lorito_reg_t Lorito_Reg;
-
+struct lorito_str_t
+{
+ int length;
+ const char *original;
+};
+typedef struct lorito_str_t Lorito_Str;
// The enum of internal PMCs
enum INTERNAL_PMC_ENUM {
@@ -150,13 +160,13 @@ typedef struct lorito_pmc_t Lorito_PMC;
// Internal PMCs
-struct lorito_str_t
+struct lorito_box_str_t
{
struct lorito_pmc_t pmc;
int size;
void *data;
};
-typedef struct lorito_str_t Lorito_Str;
+typedef struct lorito_str_t Lorito_BoxStr;
struct lorito_file_t
{
Please sign in to comment.
Something went wrong with that request. Please try again.