Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ac754af8a1
Fetching contributors…

Cannot retrieve contributors at this time

file 138 lines (112 sloc) 3.183 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
// This will initalize and drive the main interpeter.

#include "lorito.h"
#include "interp.h"
#include "loader.h"
#include "pmc.h"
#include "internal_pmc.h"

#include <stdlib.h>

Lorito_Interp *
lorito_init()
{
  Lorito_Interp *result = (Lorito_Interp *) malloc(sizeof(Lorito_Interp));

  Lorito_PMC *null_pmc = lorito_pmc_new(result, 0);

  result->next_fileid = 0;
  result->allocated_file = 0;
  result->files = NULL;
  result->last_seg = NULL;

  result->symbol_len = 0;
  result->symbols = NULL;

  result->magic = (unsigned short int) rand();

  result->null_pmc = null_pmc;

  result->ctx = lorito_ctx_new(result, null_pmc, null_pmc);
  result->default_lookup = (Lorito_PMC *) lorito_c_method_new(result, lorito_pmc_default_lookup);

  return result;
}

/* Run the interp. Uses the last file's first main code segment. */
int
lorito_run(Lorito_Interp *interp)
{
  interp->last_error = lorito_string(interp, 0, "");

  if (interp->next_fileid == 0)
  {
    interp->last_error = lorito_string(interp, 0, "No bytecode loaded.");
    return -1;
  }

  Lorito_File *file = interp->files[interp->next_fileid-1];
  Lorito_Codeseg *main = NULL;

  int i;
  for (i=0; i < file->codeseg_count; i++)
  {
    if ((file->codesegs[i]->flags & SEG_FLAG_main) == SEG_FLAG_main)
    {
      main = file->codesegs[i];
      break;
    }
  }

  if (main == NULL)
  {
    interp->last_error = lorito_string(interp, 0, "No entry point found");
    return -1;
  }

  interp->ctx = lorito_ctx_new(interp, interp->ctx, (Lorito_PMC *) main);
  core_exec(interp);
  return 0;
}

Lorito_Str *
lorito_string(Lorito_Interp *interp, int length, const char *original)
{
  int i = 0;
  
  //printf("Lorito_Str: %s\n", original);
  // 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];
  }

  if (length == 0)
  {
    // Lorito counts length as total length, including the Null byte.
    length = strlen(original) + 1;
  }

  // See if it compares with an existing symbol
  for (i = 0; i < interp->symbol_len; i++)
  {
    if (length != interp->symbols[i]->length)
      continue;
    //printf("Lorito_Str Cmp: %s\n", interp->symbols[i]->original);
    if (strncmp(original, interp->symbols[i]->original, length) == 0)
    {
      return interp->symbols[i];
    }
  }

  //printf("Lorito_Str Create: %s\n", original);
  // Otherwise, create one.
  i = interp->symbol_len;
  interp->symbol_len++;
  interp->symbols = realloc(interp->symbols, interp->symbol_len * sizeof(Lorito_Str *));

  interp->symbols[i] = malloc(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)
{
  int cnt = target->args_cnt++;
  target->args[cnt] = item;
}

Lorito_PMC *
lorito_pop_arg(Lorito_Interp *interp, Lorito_Ctx *target)
{
  if (target->args_cnt == 0)
    return null;

  int cnt = --target->args_cnt;
  return target->args[cnt];
}

void
lorito_clr_arg(Lorito_Interp *interp, Lorito_Ctx *target)
{
  target->args_cnt = 0;
}
Something went wrong with that request. Please try again.