File: ieval.c

package info (click to toggle)
ngs-js 0.2.5-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,800 kB
  • ctags: 2,977
  • sloc: ansic: 34,455; sh: 4,827; perl: 261; makefile: 208
file content (100 lines) | stat: -rw-r--r-- 2,340 bytes parent folder | download | duplicates (5)
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
/*
 * Show how the evaluation can be done in phases.
 * Copyright (c) 1998 New Generation Software (NGS) Oy
 *
 * Author: Markku Rossi <mtr@ngs.fi>
 */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 */

/*
 * $Source: /usr/local/cvsroot/ngs/js/examples/ieval.c,v $
 * $Id: ieval.c,v 1.1 1998/08/05 11:34:22 mtr Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <js.h>

/*
 * Global functions.
 */

int
main (int argc, char *argv[])
{
  char buf[1024];
  JSInterpPtr interp;

  /* Create one interpreter. */
  interp = js_create_interp (NULL);

  /* Enter the query loop. */
  while (1)
    {
      unsigned char *bc;
      unsigned char *bc_copy;
      unsigned int bc_len;
      char *cp;

      printf ("What file should I evaluate? ");

      if (fgets (buf, sizeof (buf), stdin) == NULL)
	break;

      cp = strchr (buf, '\n');
      if (cp)
	*cp = '\0';

      if (js_compile_to_byte_code (interp, buf, &bc, &bc_len) == 0)
	{
	  printf ("Compilation to the byte-code failed: %s\n",
		  js_error_message (interp));
	  continue;
	}

      printf ("Compilation returned %d bytes of byte-code data.\n",
	      bc_len);

      bc_copy = malloc (bc_len);
      assert (bc_copy != NULL);
      memcpy (bc_copy, bc, bc_len);

      while (1)
	{
	  printf ("Hit ENTER to execute the code.  ^D exits the loop. ");
	  if (getchar () == EOF)
	    break;

	  if (js_execute_byte_code (interp, bc_copy, bc_len) == 0)
	    printf ("Execution failed: %s\n", js_error_message (interp));
	}

      free (bc_copy);
      printf ("\n");
    }

  printf ("\n");

  js_destroy_interp (interp);

  return 1;
}