1
0
mirror of https://github.com/dschmenk/PLASMA.git synced 2025-01-10 21:30:04 +00:00

Lambda WIP

This commit is contained in:
David Schmenk 2017-07-30 13:22:24 -07:00
parent 1eed149c1b
commit e65adf2050
2 changed files with 72 additions and 6 deletions

View File

@ -2,9 +2,14 @@
#include "plasm.h" #include "plasm.h"
#define LVALUE 0 #define LVALUE 0
#define RVALUE 1 #define RVALUE 1
#define LAMBDA_CNT 256
int infunc = 0, break_tag = 0, cont_tag = 0, stack_loop = 0; int infunc = 0, break_tag = 0, cont_tag = 0, stack_loop = 0;
long infuncvals = 0; long infuncvals = 0;
t_token prevstmnt; t_token prevstmnt;
int lambda_num = 0;
int lambda_cnt = 0;
*opseq lambda_seq[MAX_LAMBDA];
t_token binary_ops_table[] = { t_token binary_ops_table[] = {
/* Highest precedence */ /* Highest precedence */
@ -338,6 +343,7 @@ int parse_const(long *value)
/* /*
* Normal expression parsing * Normal expression parsing
*/ */
int parse_lambda(void);
t_opseq *parse_expr(t_opseq *codeseq, int *stackdepth); t_opseq *parse_expr(t_opseq *codeseq, int *stackdepth);
t_opseq *parse_list(t_opseq *codeseq, int *stackdepth) t_opseq *parse_list(t_opseq *codeseq, int *stackdepth)
{ {
@ -446,6 +452,12 @@ t_opseq *parse_value(t_opseq *codeseq, int rvalue, int *stackdepth)
cfnvals = funcvals_cnt(type); cfnvals = funcvals_cnt(type);
} }
} }
else if (scantoken == LAMBDA_TOKEN)
{
type |= WPTR_TYPE;
value = parse_lambda();
valseq = gen_gbladr(NULL, value, type);
}
else if (scantoken == OPEN_PAREN_TOKEN) else if (scantoken == OPEN_PAREN_TOKEN)
{ {
if (!(valseq = parse_expr(NULL, stackdepth))) if (!(valseq = parse_expr(NULL, stackdepth)))
@ -1447,6 +1459,58 @@ int parse_mods(void)
emit_moddep(0, 0); emit_moddep(0, 0);
return (0); return (0);
} }
int parse_lambda(void)
{
char lambda_id[16];
if (!infunc)
{
parse_error("Lambda functions only allowed in definitions");
return (0);
}
/*
* Parse parameters and return value count
*/
if (scan() == OPEN_PAREN_TOKEN)
{
do
{
if (scan() == ID_TOKEN)
{
cfnparms++;
idlocal_add(tokenstr, tokenlen, WORD_TYPE, 2);
scan();
}
} while (scantoken == COMMA_TOKEN);
if (scantoken != CLOSE_PAREN_TOKEN)
{
parse_error("Bad function parameter list");
return (0);
}
scan();
}
if (scantoken == OPEN_PAREN_TOKEN)
{
/*
* Function call - parameters generate before call address
*/
valseq = parse_list(NULL, NULL);
if (scantoken != CLOSE_PAREN_TOKEN)
{
parse_error("Missing closing parenthesis");
return (NULL);
}
}
else
{
valseq = parse_expr(NULL, NULL);
}
lambda_seq[lambda_cnt++] = valseq;
func_tag = tag_new(DEF_TYPE);
sprintf(lambda_id, "_LAMBDA%04d", lambda_num++);
idfunc_add(idstr, strlen(idstr), DEF_TYPE | funcparms_type(cfnparms), func_tag);
return (func_tag);
}
int parse_defs(void) int parse_defs(void)
{ {
char c, *idstr; char c, *idstr;
@ -1469,6 +1533,7 @@ int parse_defs(void)
return (0); return (0);
} }
emit_bytecode_seg(); emit_bytecode_seg();
lambda_cnt = 0;
bytecode = 1; bytecode = 1;
cfnparms = 0; cfnparms = 0;
infuncvals = 1; // Defaut to one return value for compatibility infuncvals = 1; // Defaut to one return value for compatibility

View File

@ -91,6 +91,7 @@
#define NEG_TOKEN TOKEN('-') #define NEG_TOKEN TOKEN('-')
#define COMP_TOKEN TOKEN('~') #define COMP_TOKEN TOKEN('~')
#define LOGIC_NOT_TOKEN TOKEN('!') #define LOGIC_NOT_TOKEN TOKEN('!')
#define LAMBDA_TOKEN TOKEN('&')
#define INC_TOKEN TOKEN('P') #define INC_TOKEN TOKEN('P')
#define DEC_TOKEN TOKEN('K') #define DEC_TOKEN TOKEN('K')
#define BPTR_TOKEN TOKEN('^') #define BPTR_TOKEN TOKEN('^')