quakeforge/tools/qwaq/builtins.c
Bill Currie c248372e20 Initial integer type support. qfcc /is/ partially broken when it comes to
integer constants and float function args/return values.

pr_comp.h:
	o  add the integer opcodes to pr_opcode_e
pr_edict.c:
	o  add "quaternion" and "integer" to type_name[]
	o  support quatnernion and integers types when printing values
	o  support the integer opcodes when bounds checking
pr_exec.c
	o  enable the integer opcodes
pr_opcode:
	o  add the integer opcodes to the opcode table
	o  logical operators all result in an integer rather than a value
expr.h:
	o  rename int_val to integer_val
qfcc.h:
	o  kill another magic number
expr.c:
	o  move the opcode to string conversion out of type_mismatch and into
	   get_op_string
	o  rename int_val to integer_val
	o  general integer type support.
	o  generate an internal comipiler error for null opcodes rather than
	   segging.
pr_imm.c:
	o  rename int_val to integer_val
	o  support integer constants, converting to float when needed.
pr_lex.c:
	o  magic number death and support quaternions and integers in type_size[]
qc-lex.l
	o  rename int_val to integer_val
	o  support quaternion and integer type keywords
qc-parse.y:
	o  rename int_val to integer_val
	o  use binary_expr instead of new_binary_expr for local initialized
	   variables
builtins.c:
	o  rename int_val to integer_val
	o  fix most (all?) of the INT related FIXMEs
defs.qc:
	o  use integer instead of float where it makes sense
main.c:
	o  read_result is now integer rather than float
main.qc:
	o  float -> integer where appropriate
	o  new test for int const to float arg
2001-07-23 01:31:22 +00:00

135 lines
2.4 KiB
C

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <QF/progs.h>
#include <QF/zone.h>
#define RETURN_EDICT(p, e) ((p)->pr_globals[OFS_RETURN].integer_var = EDICT_TO_PROG(p, e))
#define RETURN_STRING(p, s) ((p)->pr_globals[OFS_RETURN].integer_var = PR_SetString((p), s))
int *read_result; //FIXME: eww
static void
bi_fixme (progs_t *pr)
{
PR_Error (pr, "unimplemented function\n");
}
static void
bi_print (progs_t *pr)
{
char *str;
str = G_STRING (pr, (OFS_PARM0));
fprintf (stdout, "%s", str);
}
static void
bi_GarbageCollect (progs_t *pr)
{
PR_GarbageCollect (pr);
}
static void
bi_errno (progs_t *pr)
{
G_INT (pr, OFS_RETURN) = errno;
}
static void
bi_strerror (progs_t *pr)
{
int err = G_INT (pr, OFS_PARM0);
RETURN_STRING (pr, strerror (err));
}
static void
bi_open (progs_t *pr)
{
char *path = G_STRING (pr, OFS_PARM0);
int flags = G_INT (pr, OFS_PARM1);
int mode = G_INT (pr, OFS_PARM2);
G_INT (pr, OFS_RETURN) = open (path, flags, mode);
}
static void
bi_close (progs_t *pr)
{
int handle = G_INT (pr, OFS_PARM0);
G_INT (pr, OFS_RETURN) = close (handle);
}
static void
bi_read (progs_t *pr)
{
int handle = G_INT (pr, OFS_PARM0);
int count = G_INT (pr, OFS_PARM1);
int res;
char *buffer;
buffer = Hunk_TempAlloc (count);
if (!buffer)
PR_Error (pr, "%s: couldn't allocate %d bytes", "bi_read", count);
res = read (handle, buffer, count);
if (res != -1) // FIXME: this just won't work :/
RETURN_STRING (pr, buffer);
*read_result = res;
}
static void
bi_write (progs_t *pr)
{
int handle = G_INT (pr, OFS_PARM0);
char *buffer = G_STRING (pr, OFS_PARM1);
int count = G_INT (pr, OFS_PARM2);
G_INT (pr, OFS_RETURN) = write (handle, buffer, count);
}
static void
bi_seek (progs_t *pr)
{
int handle = G_INT (pr, OFS_PARM0);
int pos = G_INT (pr, OFS_PARM1);
int whence = G_INT (pr, OFS_PARM2);
G_INT (pr, OFS_RETURN) = lseek (handle, pos, whence);
}
static void
bi_traceon (progs_t *pr)
{
pr->pr_trace = true;
}
static void
bi_traceoff (progs_t *pr)
{
pr->pr_trace = false;
}
builtin_t builtins[] = {
bi_fixme,
bi_print,
bi_GarbageCollect,
bi_errno,
bi_strerror,
bi_open,
bi_close,
bi_read,
bi_write,
bi_seek,
bi_traceon,
bi_traceoff,
};
void
BI_Init (progs_t *progs)
{
progs->builtins = builtins;
progs->numbuiltins = sizeof (builtins) / sizeof (builtins[0]);
}