2001-06-15 19:38:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
#include <QF/mathlib.h>
|
2001-06-26 07:21:20 +00:00
|
|
|
#include <QF/va.h>
|
2001-06-20 21:18:04 +00:00
|
|
|
|
2001-06-15 19:38:43 +00:00
|
|
|
#include "qfcc.h"
|
2001-06-20 21:18:04 +00:00
|
|
|
#include "scope.h"
|
|
|
|
#include "qc-parse.h"
|
|
|
|
|
2001-06-25 22:11:20 +00:00
|
|
|
extern function_t *current_func;
|
2001-06-15 19:38:43 +00:00
|
|
|
|
2001-07-14 01:15:40 +00:00
|
|
|
int lineno_base;
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
etype_t qc_types[] = {
|
2001-07-18 06:37:14 +00:00
|
|
|
ev_void, // ex_label
|
|
|
|
ev_void, // ex_block
|
|
|
|
ev_void, // ex_expr
|
|
|
|
ev_void, // ex_uexpr
|
|
|
|
ev_void, // ex_def
|
|
|
|
|
|
|
|
ev_string, // ex_string
|
|
|
|
ev_float, // ex_float
|
|
|
|
ev_vector, // ex_vector
|
|
|
|
ev_entity, // ex_entity
|
|
|
|
ev_field, // ex_field
|
|
|
|
ev_func, // ex_func
|
|
|
|
ev_pointer, // ex_pointer
|
|
|
|
ev_quaternion, // ex_quaternion
|
2001-07-22 06:59:12 +00:00
|
|
|
ev_integer, // ex_integer
|
2001-06-20 07:02:36 +00:00
|
|
|
};
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
type_t *types[] = {
|
2001-06-20 07:02:36 +00:00
|
|
|
&type_void,
|
|
|
|
&type_string,
|
|
|
|
&type_float,
|
|
|
|
&type_vector,
|
|
|
|
&type_entity,
|
|
|
|
&type_field,
|
|
|
|
&type_function,
|
|
|
|
&type_pointer,
|
2001-07-18 06:37:14 +00:00
|
|
|
&type_quaternion,
|
2001-07-22 06:59:12 +00:00
|
|
|
&type_integer,
|
2001-06-20 07:02:36 +00:00
|
|
|
};
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
expr_type expr_types[] = {
|
2001-06-26 07:21:20 +00:00
|
|
|
ex_label, // ev_void (ick)
|
|
|
|
ex_string, // ev_string
|
|
|
|
ex_float, // ev_float
|
|
|
|
ex_vector, // ev_vector
|
2001-07-18 06:37:14 +00:00
|
|
|
ex_entity, // ev_entity
|
|
|
|
ex_field, // ev_field
|
|
|
|
ex_func, // ev_func
|
|
|
|
ex_pointer, // ev_pointer
|
|
|
|
ex_quaternion, // ev_quaternion
|
2001-07-22 06:59:12 +00:00
|
|
|
ex_integer, // ev_integer
|
2001-06-26 07:21:20 +00:00
|
|
|
};
|
|
|
|
|
2001-07-09 18:17:04 +00:00
|
|
|
static const char *type_names[] = {
|
|
|
|
"void",
|
|
|
|
"string",
|
|
|
|
"float",
|
|
|
|
"vector",
|
|
|
|
"entity",
|
|
|
|
"field",
|
|
|
|
"function",
|
|
|
|
"pointer",
|
|
|
|
"quaternion",
|
2001-07-18 06:37:14 +00:00
|
|
|
"int",
|
2001-07-09 18:17:04 +00:00
|
|
|
};
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
etype_t
|
2001-06-20 07:02:36 +00:00
|
|
|
get_type (expr_t *e)
|
|
|
|
{
|
|
|
|
switch (e->type) {
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-06-26 03:33:01 +00:00
|
|
|
case ex_block:
|
2001-07-26 05:08:27 +00:00
|
|
|
return ev_type_count; // something went very wrong
|
2001-06-20 07:02:36 +00:00
|
|
|
case ex_expr:
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_uexpr:
|
2001-06-20 07:02:36 +00:00
|
|
|
return e->e.expr.type->type;
|
|
|
|
case ex_def:
|
|
|
|
return e->e.def->type->type;
|
2001-08-07 16:50:22 +00:00
|
|
|
case ex_integer:
|
|
|
|
if (options.version == PROG_ID_VERSION) {
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.integer_val;
|
|
|
|
}
|
|
|
|
// fall through
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_string:
|
|
|
|
case ex_float:
|
|
|
|
case ex_vector:
|
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
|
|
|
case ex_quaternion:
|
|
|
|
return qc_types[e->type];
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
2001-07-24 19:51:44 +00:00
|
|
|
return ev_type_count;
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
|
|
|
|
2001-06-28 21:26:40 +00:00
|
|
|
expr_t *
|
|
|
|
error (expr_t *e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
string_t file = s_file;
|
|
|
|
int line = pr_source_line;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
if (e) {
|
|
|
|
file = e->file;
|
|
|
|
line = e->line;
|
|
|
|
}
|
|
|
|
fprintf (stderr, "%s:%d: ", strings + file, line);
|
|
|
|
vfprintf (stderr, fmt, args);
|
|
|
|
fputs ("\n", stderr);
|
2001-07-26 05:08:27 +00:00
|
|
|
va_end (args);
|
|
|
|
pr_error_count++;
|
|
|
|
|
2001-06-28 21:26:40 +00:00
|
|
|
if (e) {
|
|
|
|
e = new_expr ();
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-28 21:26:40 +00:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
warning (expr_t *e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
string_t file = s_file;
|
|
|
|
int line = pr_source_line;
|
2001-06-28 23:15:10 +00:00
|
|
|
|
|
|
|
if (options.warn_error) {
|
|
|
|
options.warn_error = 0; // only want to do this once
|
|
|
|
error (e, "warnings treated as errors");
|
|
|
|
}
|
2001-06-28 21:26:40 +00:00
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
if (e) {
|
|
|
|
file = e->file;
|
|
|
|
line = e->line;
|
|
|
|
}
|
2001-07-24 04:45:53 +00:00
|
|
|
fprintf (stderr, "%s:%d: warning: ", strings + file, line);
|
2001-06-28 21:26:40 +00:00
|
|
|
vfprintf (stderr, fmt, args);
|
|
|
|
fputs ("\n", stderr);
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2001-07-23 01:31:22 +00:00
|
|
|
const char *
|
|
|
|
get_op_string (int op)
|
2001-07-09 18:17:04 +00:00
|
|
|
{
|
|
|
|
switch (op) {
|
2001-07-26 05:08:27 +00:00
|
|
|
case OR: return "||";
|
|
|
|
case AND: return "&&";
|
|
|
|
case EQ: return "==";
|
|
|
|
case NE: return "!=";
|
|
|
|
case LE: return "<=";
|
|
|
|
case GE: return ">=";
|
|
|
|
case LT: return "<";
|
|
|
|
case GT: return ">";
|
|
|
|
case '=': return "=";
|
|
|
|
case '+': return "+";
|
|
|
|
case '-': return "-";
|
|
|
|
case '*': return "*";
|
|
|
|
case '/': return "/";
|
|
|
|
case '&': return "&";
|
|
|
|
case '|': return "|";
|
|
|
|
case '!': return "!";
|
|
|
|
case '(': return "(";
|
|
|
|
case '.': return ".";
|
|
|
|
case 'i': return "<if>";
|
|
|
|
case 'n': return "<ifnot>";
|
|
|
|
case 'g': return "<goto>";
|
|
|
|
case 'r': return "<return>";
|
|
|
|
default: return "unknown";
|
2001-07-09 18:17:04 +00:00
|
|
|
}
|
2001-07-23 01:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
type_mismatch (expr_t *e1, expr_t *e2, int op)
|
|
|
|
{
|
|
|
|
etype_t t1, t2;
|
|
|
|
|
|
|
|
t1 = get_type (e1);
|
|
|
|
t2 = get_type (e2);
|
|
|
|
|
2001-07-09 18:17:04 +00:00
|
|
|
return error (e1, "type mismatch: %s %s %s",
|
2001-07-23 01:31:22 +00:00
|
|
|
type_names[t1], get_op_string (op), type_names[t2]);
|
2001-07-09 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
expr_t *
|
2001-06-25 20:52:04 +00:00
|
|
|
new_expr (void)
|
2001-06-20 21:18:04 +00:00
|
|
|
{
|
2001-06-26 16:23:21 +00:00
|
|
|
expr_t *e = calloc (1, sizeof (expr_t));
|
|
|
|
e->line = pr_source_line;
|
|
|
|
e->file = s_file;
|
|
|
|
return e;
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
2001-06-25 20:52:04 +00:00
|
|
|
expr_t *
|
2001-06-26 02:46:02 +00:00
|
|
|
new_label_expr (void)
|
2001-06-25 20:52:04 +00:00
|
|
|
{
|
|
|
|
static int label = 0;
|
2001-06-25 22:11:20 +00:00
|
|
|
int lnum = ++label;
|
|
|
|
const char *fname = current_func->def->name;
|
2001-06-25 20:52:04 +00:00
|
|
|
|
|
|
|
expr_t *l = new_expr ();
|
2001-06-25 22:11:20 +00:00
|
|
|
l->type = ex_label;
|
2001-06-26 03:33:01 +00:00
|
|
|
l->e.label.name = malloc (1 + strlen (fname) + 1 + ceil (log10 (lnum)) + 1);
|
|
|
|
sprintf (l->e.label.name, "$%s_%d", fname, lnum);
|
2001-06-25 20:52:04 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2001-06-26 03:33:01 +00:00
|
|
|
expr_t *
|
|
|
|
new_block_expr (void)
|
|
|
|
{
|
|
|
|
expr_t *b = new_expr ();
|
|
|
|
|
|
|
|
b->type = ex_block;
|
|
|
|
b->e.block.head = 0;
|
|
|
|
b->e.block.tail = &b->e.block.head;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2001-06-26 02:46:02 +00:00
|
|
|
expr_t *
|
|
|
|
new_binary_expr (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
|
|
|
|
e->type = ex_expr;
|
|
|
|
e->e.expr.op = op;
|
|
|
|
e->e.expr.e1 = e1;
|
|
|
|
e->e.expr.e2 = e2;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_unary_expr (int op, expr_t *e1)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
|
|
|
|
e->type = ex_uexpr;
|
|
|
|
e->e.expr.op = op;
|
|
|
|
e->e.expr.e1 = e1;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2001-06-26 03:33:01 +00:00
|
|
|
expr_t *
|
|
|
|
append_expr (expr_t *block, expr_t *e)
|
|
|
|
{
|
|
|
|
if (block->type != ex_block)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
if (!e)
|
|
|
|
return block;
|
|
|
|
|
|
|
|
*block->e.block.tail = e;
|
|
|
|
block->e.block.tail = &e->next;
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2001-06-25 17:15:56 +00:00
|
|
|
void
|
|
|
|
print_expr (expr_t *e)
|
|
|
|
{
|
|
|
|
printf (" ");
|
2001-06-25 20:52:04 +00:00
|
|
|
if (!e) {
|
|
|
|
printf ("(nil)");
|
|
|
|
return;
|
|
|
|
}
|
2001-06-25 17:15:56 +00:00
|
|
|
switch (e->type) {
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-06-26 03:33:01 +00:00
|
|
|
printf ("%s", e->e.label.name);
|
|
|
|
break;
|
|
|
|
case ex_block:
|
|
|
|
printf ("{\n");
|
|
|
|
for (e = e->e.block.head; e; e = e->next) {
|
|
|
|
print_expr (e);
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
printf ("}");
|
2001-06-25 22:11:20 +00:00
|
|
|
break;
|
2001-06-25 17:15:56 +00:00
|
|
|
case ex_expr:
|
|
|
|
print_expr (e->e.expr.e1);
|
|
|
|
if (e->e.expr.op == 'c') {
|
|
|
|
expr_t *p = e->e.expr.e2;
|
|
|
|
printf ("(");
|
|
|
|
while (p) {
|
|
|
|
print_expr (p);
|
|
|
|
if (p->next)
|
|
|
|
printf (",");
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
printf (")");
|
|
|
|
} else {
|
|
|
|
print_expr (e->e.expr.e2);
|
2001-07-23 06:15:33 +00:00
|
|
|
printf (" %s", get_op_string (e->e.expr.op));
|
2001-06-25 17:15:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ex_uexpr:
|
|
|
|
print_expr (e->e.expr.e1);
|
2001-07-23 06:15:33 +00:00
|
|
|
printf (" u%s", get_op_string (e->e.expr.op));
|
2001-06-25 17:15:56 +00:00
|
|
|
break;
|
|
|
|
case ex_def:
|
|
|
|
printf ("%s", e->e.def->name);
|
|
|
|
break;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_string:
|
|
|
|
printf ("\"%s\"", e->e.string_val);
|
2001-06-25 17:15:56 +00:00
|
|
|
break;
|
|
|
|
case ex_float:
|
|
|
|
printf ("%g", e->e.float_val);
|
|
|
|
break;
|
|
|
|
case ex_vector:
|
|
|
|
printf ("'%g", e->e.vector_val[0]);
|
2001-07-26 05:08:27 +00:00
|
|
|
printf (" %g", e->e.vector_val[1]);
|
|
|
|
printf (" %g'", e->e.vector_val[2]);
|
2001-06-25 17:15:56 +00:00
|
|
|
break;
|
|
|
|
case ex_quaternion:
|
|
|
|
printf ("'%g", e->e.quaternion_val[0]);
|
|
|
|
printf (" %g", e->e.quaternion_val[1]);
|
|
|
|
printf (" %g", e->e.quaternion_val[2]);
|
|
|
|
printf (" %g'", e->e.quaternion_val[3]);
|
|
|
|
break;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
2001-07-22 06:59:12 +00:00
|
|
|
case ex_integer:
|
2001-07-23 01:31:22 +00:00
|
|
|
printf ("%d", e->e.integer_val);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
2001-06-25 17:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
static expr_t *
|
|
|
|
do_op_string (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *buf;
|
|
|
|
char *s1, *s2;
|
|
|
|
|
2001-07-24 22:30:31 +00:00
|
|
|
s1 = e1->e.string_val ? e1->e.string_val : "";
|
|
|
|
s2 = e2->e.string_val ? e2->e.string_val : "";
|
2001-06-20 21:18:04 +00:00
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
len = strlen (s1) + strlen (s2) + 1;
|
2001-06-25 22:53:15 +00:00
|
|
|
buf = malloc (len);
|
2001-06-20 21:18:04 +00:00
|
|
|
strcpy (buf, s1);
|
|
|
|
strcat (buf, s2);
|
2001-06-25 22:53:15 +00:00
|
|
|
e1->e.string_val = buf;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case LT:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = strcmp (s1, s2) < 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case GT:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = strcmp (s1, s2) > 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case LE:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = strcmp (s1, s2) <= 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case GE:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = strcmp (s1, s2) >= 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case EQ:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = strcmp (s1, s2) == 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case NE:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = strcmp (s1, s2) != 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-06-28 21:26:40 +00:00
|
|
|
return error (e1, "invalid operand for string");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
return e1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static expr_t *
|
|
|
|
do_op_float (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
float f1, f2;
|
|
|
|
|
|
|
|
f1 = e1->e.float_val;
|
|
|
|
f2 = e2->e.float_val;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
e1->e.float_val += f2;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
e1->e.float_val -= f2;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
e1->e.float_val *= f2;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
e1->e.float_val /= f2;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
e1->e.float_val = (int)f1 & (int)f2;
|
|
|
|
break;
|
|
|
|
case '|':
|
2001-07-19 03:00:35 +00:00
|
|
|
e1->e.float_val = (int)f1 | (int)f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case AND:
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = f1 && f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case OR:
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = f1 || f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case LT:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = f1 < f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case GT:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = f1 > f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case LE:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = f1 <= f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case GE:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = f1 >= f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case EQ:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = f1 == f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
case NE:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = f1 != f2;
|
2001-06-20 21:18:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-07-26 05:08:27 +00:00
|
|
|
return error (e1, "invalid operand for float");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
return e1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static expr_t *
|
|
|
|
do_op_vector (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
float *v1, *v2;
|
|
|
|
|
|
|
|
v1 = e1->e.vector_val;
|
|
|
|
v2 = e2->e.vector_val;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
VectorAdd (v1, v2, v1);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
VectorSubtract (v1, v2, v1);
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
e1->type = ex_float;
|
|
|
|
e1->e.float_val = DotProduct (v1, v2);
|
|
|
|
break;
|
|
|
|
case EQ:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = (v1[0] == v2[0])
|
2001-06-20 21:18:04 +00:00
|
|
|
&& (v1[1] == v2[1])
|
|
|
|
&& (v1[2] == v2[2]);
|
|
|
|
break;
|
|
|
|
case NE:
|
2001-07-22 06:59:12 +00:00
|
|
|
e1->type = ex_integer;
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val = (v1[0] == v2[0])
|
2001-06-20 21:18:04 +00:00
|
|
|
|| (v1[1] != v2[1])
|
|
|
|
|| (v1[2] != v2[2]);
|
|
|
|
break;
|
|
|
|
default:
|
2001-07-26 05:08:27 +00:00
|
|
|
return error (e1, "invalid operand for vector");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
return e1;
|
|
|
|
}
|
|
|
|
|
2001-07-23 01:31:22 +00:00
|
|
|
static expr_t *
|
|
|
|
do_op_integer (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
int i1, i2;
|
|
|
|
|
|
|
|
i1 = e1->e.integer_val;
|
|
|
|
i2 = e2->e.integer_val;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
e1->e.integer_val += i2;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
e1->e.integer_val -= i2;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
e1->e.integer_val *= i2;
|
|
|
|
break;
|
|
|
|
case '/':
|
2001-07-24 04:45:53 +00:00
|
|
|
warning (e2, "%d / %d == %d", i1, i2, i1 / i2);
|
2001-07-23 01:31:22 +00:00
|
|
|
e1->e.integer_val /= i2;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
e1->e.integer_val = i1 & i2;
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
e1->e.integer_val = i1 | i2;
|
|
|
|
break;
|
|
|
|
case AND:
|
|
|
|
e1->e.integer_val = i1 && i2;
|
|
|
|
break;
|
|
|
|
case OR:
|
|
|
|
e1->e.integer_val = i1 || i2;
|
|
|
|
break;
|
|
|
|
case LT:
|
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = i1 < i2;
|
|
|
|
break;
|
|
|
|
case GT:
|
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = i1 > i2;
|
|
|
|
break;
|
|
|
|
case LE:
|
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = i1 <= i2;
|
|
|
|
break;
|
|
|
|
case GE:
|
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = i1 >= i2;
|
|
|
|
break;
|
|
|
|
case EQ:
|
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = i1 == i2;
|
|
|
|
break;
|
|
|
|
case NE:
|
|
|
|
e1->type = ex_integer;
|
|
|
|
e1->e.integer_val = i1 != i2;
|
|
|
|
break;
|
|
|
|
default:
|
2001-07-26 05:08:27 +00:00
|
|
|
return error (e1, "invalid operand for integer");
|
2001-07-23 01:31:22 +00:00
|
|
|
}
|
|
|
|
return e1;
|
|
|
|
}
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
static expr_t *
|
|
|
|
do_op_huh (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
2001-06-28 21:26:40 +00:00
|
|
|
return error (e1, "funny constant");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static expr_t *(*do_op[]) (int op, expr_t *e1, expr_t *e2) = {
|
|
|
|
do_op_huh,
|
|
|
|
do_op_string,
|
|
|
|
do_op_float,
|
|
|
|
do_op_vector,
|
|
|
|
do_op_huh,
|
|
|
|
do_op_huh,
|
|
|
|
do_op_huh,
|
|
|
|
do_op_huh,
|
2001-07-23 01:31:22 +00:00
|
|
|
do_op_huh,
|
|
|
|
do_op_integer,
|
2001-06-20 21:18:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static expr_t *
|
|
|
|
binary_const (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
etype_t t1, t2;
|
|
|
|
//expr_t *e;
|
|
|
|
|
|
|
|
t1 = get_type (e1);
|
|
|
|
t2 = get_type (e2);
|
|
|
|
|
|
|
|
if (t1 == t2) {
|
|
|
|
return do_op[t1](op, e1, e2);
|
|
|
|
} else {
|
2001-07-09 18:17:04 +00:00
|
|
|
return type_mismatch (e1, e2, op);
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static expr_t *
|
|
|
|
field_expr (expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
etype_t t1, t2;
|
|
|
|
expr_t *e;
|
|
|
|
|
|
|
|
t1 = get_type (e1);
|
|
|
|
t2 = get_type (e2);
|
|
|
|
|
2001-06-27 21:43:10 +00:00
|
|
|
if (t1 != ev_entity || t2 != ev_field) {
|
2001-06-28 21:26:40 +00:00
|
|
|
return error (e1, "type missmatch for .");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
2001-06-26 19:48:03 +00:00
|
|
|
e = new_binary_expr ('.', e1, e2);
|
2001-06-20 21:18:04 +00:00
|
|
|
e->e.expr.type = (e2->type == ex_def)
|
|
|
|
? e2->e.def->type->aux_type
|
|
|
|
: e2->e.expr.type;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2001-07-24 22:30:31 +00:00
|
|
|
expr_t *
|
2001-07-18 06:37:14 +00:00
|
|
|
test_expr (expr_t *e, int test)
|
|
|
|
{
|
|
|
|
expr_t *new = 0;
|
|
|
|
|
|
|
|
if (!test)
|
|
|
|
return unary_expr ('!', e);
|
|
|
|
|
|
|
|
switch (get_type (e)) {
|
|
|
|
case ev_type_count:
|
|
|
|
error (e, "internal error");
|
|
|
|
abort ();
|
2001-07-24 19:51:44 +00:00
|
|
|
case ev_void:
|
|
|
|
error (e, "void has no value");
|
|
|
|
break;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ev_string:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_string;
|
|
|
|
break;
|
2001-07-23 01:31:22 +00:00
|
|
|
case ev_integer:
|
2001-07-18 06:37:14 +00:00
|
|
|
return e;
|
2001-07-23 05:31:49 +00:00
|
|
|
case ev_float:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_float;
|
|
|
|
break;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ev_vector:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_vector;
|
|
|
|
break;
|
|
|
|
case ev_entity:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_entity;
|
|
|
|
break;
|
|
|
|
case ev_field:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_field;
|
|
|
|
break;
|
|
|
|
case ev_func:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_func;
|
|
|
|
break;
|
|
|
|
case ev_pointer:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_pointer;
|
|
|
|
break;
|
|
|
|
case ev_quaternion:
|
|
|
|
new = new_expr ();
|
|
|
|
new->type = ex_quaternion;
|
|
|
|
break;
|
|
|
|
}
|
2001-07-24 22:30:31 +00:00
|
|
|
new->line = e->line;
|
|
|
|
new->file = e->file;
|
|
|
|
new = binary_expr (NE, e, new);
|
|
|
|
new->line = e->line;
|
|
|
|
new->file = e->file;
|
|
|
|
return new;
|
2001-07-18 06:37:14 +00:00
|
|
|
}
|
|
|
|
|
2001-07-23 05:31:49 +00:00
|
|
|
void
|
|
|
|
convert_int (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.integer_val;
|
|
|
|
}
|
|
|
|
|
2001-06-20 07:02:36 +00:00
|
|
|
expr_t *
|
|
|
|
binary_expr (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
etype_t t1, t2;
|
2001-07-23 05:31:49 +00:00
|
|
|
type_t *type = 0;
|
|
|
|
expr_t *e;
|
2001-06-20 07:02:36 +00:00
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
if (op == '.')
|
|
|
|
return field_expr (e1, e2);
|
|
|
|
|
2001-07-18 06:37:14 +00:00
|
|
|
if (op == OR || op == AND) {
|
|
|
|
e1 = test_expr (e1, true);
|
|
|
|
e2 = test_expr (e2, true);
|
|
|
|
}
|
|
|
|
|
2001-07-23 05:31:49 +00:00
|
|
|
t1 = get_type (e1);
|
|
|
|
t2 = get_type (e2);
|
2001-07-24 19:51:44 +00:00
|
|
|
if (t1 == ev_type_count || t2 == ev_type_count) {
|
2001-07-23 05:31:49 +00:00
|
|
|
error (e1, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e1->type == ex_integer
|
|
|
|
&& (t2 == ev_float || t2 == ev_vector || t2 == ev_quaternion)) {
|
|
|
|
convert_int (e1);
|
|
|
|
t1 = ev_float;
|
|
|
|
} else if (e2->type == ex_integer
|
|
|
|
&& (t1 == ev_float || t1 == ev_vector || t1 == ev_quaternion)) {
|
|
|
|
convert_int (e2);
|
|
|
|
t2 = ev_float;
|
|
|
|
}
|
|
|
|
|
2001-07-18 06:37:14 +00:00
|
|
|
if (e1->type >= ex_string && e2->type >= ex_string)
|
2001-06-20 07:02:36 +00:00
|
|
|
return binary_const (op, e1, e2);
|
|
|
|
|
2001-06-28 15:20:31 +00:00
|
|
|
if ((op == '&' || op == '|')
|
|
|
|
&& e1->type == ex_uexpr && e1->e.expr.op == '!' && !e1->paren) {
|
2001-06-28 21:26:40 +00:00
|
|
|
warning (e1, "ambiguous logic. Suggest explicit parentheses with expressions involving ! and %c", op);
|
2001-06-28 15:20:31 +00:00
|
|
|
}
|
|
|
|
|
2001-07-23 05:31:49 +00:00
|
|
|
if (t1 != t2) {
|
2001-06-20 21:18:04 +00:00
|
|
|
switch (t1) {
|
|
|
|
case ev_float:
|
|
|
|
if (t2 == ev_vector) {
|
2001-07-23 05:31:49 +00:00
|
|
|
type = &type_vector;
|
2001-06-20 21:18:04 +00:00
|
|
|
} else {
|
|
|
|
goto type_mismatch;
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
break;
|
2001-06-20 21:18:04 +00:00
|
|
|
case ev_vector:
|
|
|
|
if (t2 == ev_float) {
|
2001-07-23 05:31:49 +00:00
|
|
|
type = &type_vector;
|
2001-06-20 21:18:04 +00:00
|
|
|
} else {
|
|
|
|
goto type_mismatch;
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
break;
|
2001-06-20 21:18:04 +00:00
|
|
|
case ev_field:
|
|
|
|
if (e1->e.expr.type->aux_type->type == t2) {
|
2001-07-23 05:31:49 +00:00
|
|
|
type = e1->e.expr.type->aux_type;
|
2001-06-20 21:18:04 +00:00
|
|
|
} else {
|
|
|
|
goto type_mismatch;
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
break;
|
2001-06-20 21:18:04 +00:00
|
|
|
default:
|
|
|
|
type_mismatch:
|
2001-07-09 18:17:04 +00:00
|
|
|
return type_mismatch (e1, e2, op);
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
} else {
|
|
|
|
type = types[t1];
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
2001-08-03 07:47:15 +00:00
|
|
|
if ((op >= OR && op <= GT) || op == '>' || op == '<') {
|
|
|
|
if (options.version > PROG_ID_VERSION)
|
|
|
|
type = &type_integer;
|
|
|
|
else
|
|
|
|
type = &type_float;
|
|
|
|
} else if (op == '*' && t1 == ev_vector && t2 == ev_vector) {
|
2001-07-23 05:31:49 +00:00
|
|
|
type = &type_float;
|
2001-08-03 07:47:15 +00:00
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
if (op == '=' && e1->type == ex_expr && e1->e.expr.op == '.') {
|
|
|
|
e1->e.expr.type = &type_pointer;
|
|
|
|
}
|
|
|
|
if (!type)
|
|
|
|
error (e1, "internal error");
|
|
|
|
|
|
|
|
e = new_binary_expr (op, e1, e2);
|
|
|
|
e->e.expr.type = type;
|
|
|
|
return e;
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
unary_expr (int op, expr_t *e)
|
|
|
|
{
|
2001-06-20 21:18:04 +00:00
|
|
|
switch (op) {
|
|
|
|
case '-':
|
|
|
|
switch (e->type) {
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-06-26 03:33:01 +00:00
|
|
|
case ex_block:
|
2001-07-26 05:08:27 +00:00
|
|
|
error (e, "internal error");
|
2001-06-25 22:11:20 +00:00
|
|
|
abort ();
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_uexpr:
|
|
|
|
if (e->e.expr.op == '-')
|
|
|
|
return e->e.expr.e1;
|
|
|
|
case ex_expr:
|
|
|
|
case ex_def:
|
|
|
|
{
|
2001-06-26 19:48:03 +00:00
|
|
|
expr_t *n = new_unary_expr (op, e);
|
2001-06-20 21:18:04 +00:00
|
|
|
n->e.expr.type = (e->type == ex_def)
|
|
|
|
? e->e.def->type
|
|
|
|
: e->e.expr.type;
|
|
|
|
return n;
|
|
|
|
}
|
2001-07-22 06:59:12 +00:00
|
|
|
case ex_integer:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val *= -1;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_float:
|
|
|
|
e->e.float_val *= -1;
|
|
|
|
return e;
|
|
|
|
case ex_string:
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
2001-06-28 21:26:40 +00:00
|
|
|
return error (e, "invalid type for unary -");
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_vector:
|
|
|
|
e->e.vector_val[0] *= -1;
|
|
|
|
e->e.vector_val[1] *= -1;
|
|
|
|
e->e.vector_val[2] *= -1;
|
|
|
|
return e;
|
|
|
|
case ex_quaternion:
|
|
|
|
e->e.quaternion_val[0] *= -1;
|
|
|
|
e->e.quaternion_val[1] *= -1;
|
|
|
|
e->e.quaternion_val[2] *= -1;
|
|
|
|
e->e.quaternion_val[3] *= -1;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
switch (e->type) {
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-06-26 03:33:01 +00:00
|
|
|
case ex_block:
|
2001-06-25 22:11:20 +00:00
|
|
|
abort ();
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_uexpr:
|
|
|
|
case ex_expr:
|
|
|
|
case ex_def:
|
|
|
|
{
|
2001-06-26 19:48:03 +00:00
|
|
|
expr_t *n = new_unary_expr (op, e);
|
2001-08-03 07:47:15 +00:00
|
|
|
if (options.version > PROG_ID_VERSION)
|
|
|
|
n->e.expr.type = &type_integer;
|
|
|
|
else
|
|
|
|
n->e.expr.type = &type_float;
|
2001-06-20 21:18:04 +00:00
|
|
|
return n;
|
|
|
|
}
|
2001-07-22 06:59:12 +00:00
|
|
|
case ex_integer:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.integer_val;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_float:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.float_val;
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_string:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.string_val || !e->e.string_val[0];
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_vector:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.vector_val[0]
|
2001-06-20 21:18:04 +00:00
|
|
|
&& !e->e.vector_val[1]
|
|
|
|
&& !e->e.vector_val[2];
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_quaternion:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.quaternion_val[0]
|
2001-06-20 21:18:04 +00:00
|
|
|
&& !e->e.quaternion_val[1]
|
|
|
|
&& !e->e.quaternion_val[2]
|
|
|
|
&& !e->e.quaternion_val[3];
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
|
|
|
error (e, "internal error");
|
|
|
|
abort ();
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort ();
|
|
|
|
}
|
2001-06-28 21:26:40 +00:00
|
|
|
error (e, "internal error");
|
2001-06-20 21:18:04 +00:00
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
function_expr (expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
etype_t t1;
|
|
|
|
expr_t *e;
|
|
|
|
int parm_count = 0;
|
|
|
|
type_t *ftype;
|
2001-07-23 01:56:49 +00:00
|
|
|
int i;
|
2001-06-20 21:18:04 +00:00
|
|
|
|
|
|
|
t1 = get_type (e1);
|
|
|
|
|
|
|
|
if (t1 != ev_func) {
|
2001-07-03 20:53:49 +00:00
|
|
|
if (e1->type == ex_def)
|
|
|
|
return error (e1, "Called object \"%s\" is not a function",
|
|
|
|
e1->e.def->name);
|
|
|
|
else
|
|
|
|
return error (e1, "Called object is not a function");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
2001-07-18 17:23:42 +00:00
|
|
|
if (e1->type == ex_def && e2 && e2->type == ex_string) {
|
2001-07-22 06:59:12 +00:00
|
|
|
//FIXME eww, I hate this, but it's needed :(
|
|
|
|
//FIXME make a qc hook? :)
|
2001-07-18 17:23:42 +00:00
|
|
|
def_t *func = e1->e.def;
|
|
|
|
def_t *e = PR_ReuseConstant (e2, 0);
|
|
|
|
|
|
|
|
if (strncmp (func->name, "precache_sound", 14) == 0)
|
|
|
|
PrecacheSound (e, func->name[4]);
|
|
|
|
else if (strncmp (func->name, "precache_model", 14) == 0)
|
|
|
|
PrecacheModel (e, func->name[14]);
|
|
|
|
else if (strncmp (func->name, "precache_file", 13) == 0)
|
|
|
|
PrecacheFile (e, func->name[13]);
|
|
|
|
}
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
ftype = e1->type == ex_def
|
|
|
|
? e1->e.def->type
|
|
|
|
: e1->e.expr.type;
|
|
|
|
|
|
|
|
for (e = e2; e; e = e->next)
|
|
|
|
parm_count++;
|
|
|
|
if (parm_count > 8) {
|
2001-07-06 02:43:39 +00:00
|
|
|
return error (e1, "more than 8 parameters");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
if (ftype->num_parms != -1) {
|
2001-07-23 01:56:49 +00:00
|
|
|
expr_t *err = 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
if (parm_count > ftype->num_parms) {
|
2001-06-28 21:26:40 +00:00
|
|
|
return error (e1, "too many arguments");
|
2001-06-20 21:18:04 +00:00
|
|
|
} else if (parm_count < ftype->num_parms) {
|
2001-06-28 21:26:40 +00:00
|
|
|
return error (e1, "too few arguments");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
for (i = parm_count, e = e2; i > 0; i--, e = e->next) {
|
2001-07-23 01:56:49 +00:00
|
|
|
type_t *t;
|
|
|
|
if (e->type == ex_expr) {
|
|
|
|
t = e->e.expr.type;
|
|
|
|
} else if (e->type == ex_def) {
|
|
|
|
t = e->e.def->type;
|
|
|
|
} else {
|
2001-07-23 05:31:49 +00:00
|
|
|
if (ftype->parm_types[i - 1] == &type_float
|
2001-07-23 01:56:49 +00:00
|
|
|
&& e->type == ex_integer) {
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.integer_val;
|
|
|
|
}
|
|
|
|
t = types[get_type (e)];
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
if (t != ftype->parm_types[i - 1])
|
2001-07-23 01:56:49 +00:00
|
|
|
err = error (e, "type mismatch for parameter %d of %s",
|
2001-07-23 05:31:49 +00:00
|
|
|
i, e1->e.def->name);
|
2001-07-23 01:56:49 +00:00
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
return err;
|
2001-07-24 20:43:18 +00:00
|
|
|
} else {
|
2001-07-24 22:30:31 +00:00
|
|
|
//for (e = e2; e; e = e->next)
|
|
|
|
// if (e->type == ex_integer)
|
|
|
|
// warning (e, "passing integer consant into ... function");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-06-26 19:48:03 +00:00
|
|
|
e = new_binary_expr ('c', e1, e2);
|
2001-06-20 21:18:04 +00:00
|
|
|
e->e.expr.type = ftype->aux_type;
|
|
|
|
return e;
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
|
2001-07-23 02:27:46 +00:00
|
|
|
expr_t *
|
|
|
|
return_expr (function_t *f, expr_t *e)
|
|
|
|
{
|
|
|
|
if (!e) {
|
|
|
|
if (f->def->type->aux_type != &type_void)
|
|
|
|
return error (e, "return from non-void function without a value");
|
|
|
|
} else {
|
|
|
|
type_t *t;
|
|
|
|
if (f->def->type->aux_type == &type_void)
|
|
|
|
return error (e, "returning a value for a void function");
|
|
|
|
if (e->type == ex_expr) {
|
|
|
|
t = e->e.expr.type;
|
|
|
|
} else if (e->type == ex_def) {
|
|
|
|
t = e->e.def->type;
|
|
|
|
} else {
|
|
|
|
if (f->def->type->aux_type == &type_float
|
|
|
|
&& e->type == ex_integer) {
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.integer_val;
|
|
|
|
}
|
|
|
|
t = types[get_type (e)];
|
|
|
|
}
|
|
|
|
if (f->def->type->aux_type != t)
|
|
|
|
return error (e, "type mismatch for return value of %s",
|
2001-07-23 05:46:00 +00:00
|
|
|
f->def->name);
|
2001-07-23 02:27:46 +00:00
|
|
|
}
|
|
|
|
return new_unary_expr ('r', e);
|
|
|
|
}
|