Skip to content

Commit

Permalink
Дескрипторы функций полностью реализованы (close #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazdaywik committed Sep 25, 2018
1 parent 570b72e commit 5dc0c8e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 60 deletions.
12 changes: 5 additions & 7 deletions lib/Library.ref
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ $ENTRY Explode {
}

r05_reset_allocator();
r05_alloc_string(ident->info.function.name);
r05_alloc_string(ident->info.function->name);
r05_splice_from_freelist(arg_begin);
r05_splice_to_freelist(arg_begin, arg_end);

Expand Down Expand Up @@ -437,7 +437,7 @@ $ENTRY Open {
}
mode_str[0] = mode;
} else {
mode = sMode->info.function.name;
mode = sMode->info.function->name;
}

if (! r05_empty_seq(eFileName_b, eFileName_e)) {
Expand Down Expand Up @@ -568,7 +568,7 @@ static enum r05_fnresult output_func(
break;

case R05_DATATAG_FUNCTION:
CHECK_PRINTF(fprintf(output, "%s ", p->info.function.name));
CHECK_PRINTF(fprintf(output, "%s ", p->info.function->name));
break;

case R05_DATATAG_NUMBER:
Expand Down Expand Up @@ -999,15 +999,13 @@ $ENTRY ExistFile {

arg_begin->tag = R05_DATATAG_FUNCTION;
if (file != NULL) {
arg_begin->info.function.ptr = r05c_True;
arg_begin->info.function.name = "True";
arg_begin->info.function = &r05f_True;
if (fclose(file) == EOF) {
perror("fclose error");
r05_builtin_error("fclose error");
}
} else {
arg_begin->info.function.ptr = r05c_False;
arg_begin->info.function.name = "False";
arg_begin->info.function = &r05f_False;
}

r05_splice_to_freelist(callee, arg_end);
Expand Down
41 changes: 18 additions & 23 deletions lib/refalrts.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ int r05_empty_seq(struct r05_node *first, struct r05_node *last) {


int r05_function_left(
r05_function_ptr fn, struct r05_node **first, struct r05_node **last
struct r05_function *fn, struct r05_node **first, struct r05_node **last
) {
assert((*first == 0) == (*last == 0));

if (r05_empty_seq(*first, *last)) {
return 0;
} else if ((*first)->tag != R05_DATATAG_FUNCTION) {
return 0;
} else if ((*first)->info.function.ptr != fn) {
} else if ((*first)->info.function != fn) {
return 0;
} else {
r05_move_left(first, last);
Expand All @@ -88,15 +88,15 @@ int r05_function_left(


int r05_function_right(
r05_function_ptr fn, struct r05_node **first, struct r05_node **last
struct r05_function *fn, struct r05_node **first, struct r05_node **last
) {
assert((*first == 0) == (*last == 0));

if (r05_empty_seq(*first, *last)) {
return 0;
} else if (R05_DATATAG_FUNCTION != (*last)->tag) {
return 0;
} else if ((*last)->info.function.ptr != fn) {
} else if ((*last)->info.function != fn) {
return 0;
} else {
r05_move_right(first, last);
Expand Down Expand Up @@ -335,7 +335,7 @@ static int equal_nodes(struct r05_node *node1, struct r05_node *node2) {
return (node1->info.number == node2->info.number);

case R05_DATATAG_FUNCTION:
return (node1->info.function.ptr == node2->info.function.ptr);
return (node1->info.function == node2->info.function);

/*
Сведения о связях между скобками нужны для других целей, здесь
Expand Down Expand Up @@ -817,14 +817,6 @@ void r05_alloc_chars(const char buffer[], size_t len) {
}


struct r05_function r05_make_function(r05_function_ptr func, const char *name) {
struct r05_function res;
res.ptr = func;
res.name = name;
return res;
}


void r05_alloc_tvar(struct r05_node *sample) {
if (is_open_bracket(sample)) {
struct r05_node *end_of_sample = sample->info.link;
Expand Down Expand Up @@ -894,6 +886,15 @@ void r05_splice_from_freelist(struct r05_node *pos) {
}


enum r05_fnresult r05_enum_function_code(
struct r05_node *arg_begin, struct r05_node *arg_end
) {
(void) arg_begin;
(void) arg_end;
return R05_RECOGNITION_IMPOSSIBLE;
}


/*==============================================================================
Внутренний профилировщик
==============================================================================*/
Expand Down Expand Up @@ -1120,14 +1121,14 @@ static int empty_stack(void) {
}


extern enum r05_fnresult r05c_Go(struct r05_node *begin, struct r05_node *end);
extern struct r05_function r05f_Go;

static void init_view_field(void) {
struct r05_node *open, *close;

r05_reset_allocator();
r05_alloc_open_call(open);
r05_alloc_function(r05c_Go, "Go");
r05_alloc_function(&r05f_Go);
r05_alloc_close_call(close);
r05_push_stack(close);
r05_push_stack(open);
Expand All @@ -1154,9 +1155,7 @@ static void main_loop(void) {

function = s_arg_begin->next;
if (R05_DATATAG_FUNCTION == function->tag) {
res = (enum r05_fnresult)(
(function->info.function.ptr)(s_arg_begin, s_arg_end) & 0xFFU
);
res = (function->info.function->ptr)(s_arg_begin, s_arg_end);
} else {
res = R05_RECOGNITION_IMPOSSIBLE;
}
Expand Down Expand Up @@ -1250,11 +1249,7 @@ static void print_seq(
continue;

case R05_DATATAG_FUNCTION:
if (begin->info.function.name[0] != 0) {
fprintf(output, "&%s ", begin->info.function.name);
} else {
fprintf(output, "&%p ", begin->info.function.ptr);
}
fprintf(output, "&%s ", begin->info.function->name);
r05_move_left(&begin, &end);
continue;

Expand Down
18 changes: 10 additions & 8 deletions lib/refalrts.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct r05_node {
union {
char char_;
void *file;
struct r05_function function;
struct r05_function *function;
r05_number number;
struct r05_node *link;
} info;
Expand All @@ -66,10 +66,10 @@ void r05_move_right(struct r05_node **begin, struct r05_node **end);
int r05_empty_seq(struct r05_node *begin, struct r05_node *end);

int r05_function_left(
r05_function_ptr func, struct r05_node **first, struct r05_node **last
struct r05_function *func, struct r05_node **first, struct r05_node **last
);
int r05_function_right(
r05_function_ptr func, struct r05_node **first, struct r05_node **last
struct r05_function *func, struct r05_node **first, struct r05_node **last
);

int r05_char_left(char ch, struct r05_node **first, struct r05_node **last);
Expand Down Expand Up @@ -167,11 +167,8 @@ void r05_alloc_chars(const char buffer[], size_t len);
#define r05_alloc_number(num) \
(r05_alloc_node(R05_DATATAG_NUMBER)->info.number = (num))

#define r05_alloc_function(func, name) \
(r05_alloc_node(R05_DATATAG_FUNCTION)->info.function =\
r05_make_function(func, name))

struct r05_function r05_make_function(r05_function_ptr func, const char *name);
#define r05_alloc_function(func) \
(r05_alloc_node(R05_DATATAG_FUNCTION)->info.function = func)

#define r05_alloc_open_bracket(pos) \
((pos) = r05_alloc_node(R05_DATATAG_OPEN_BRACKET))
Expand All @@ -195,6 +192,11 @@ void r05_alloc_evar(struct r05_node *sample_b, struct r05_node *sample_e);
void r05_alloc_string(const char *string);


enum r05_fnresult r05_enum_function_code(
struct r05_node *arg_begin, struct r05_node *arg_end
);


/* Профилирование */

void r05_this_is_generated_function(void);
Expand Down
38 changes: 16 additions & 22 deletions src/Generator.ref
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@ $EXTERN EscapeChar;

*$EENUM GN-Local, GN-Entry

FuncArguments {
= 'struct r05_node *arg_begin, struct r05_node *arg_end';
}

TextFromMemoryClass {
GN-Local = 'static '; GN-Entry = ;
}

$ENTRY GenFnStart {
s.MemoryClass e.Name =
(
<TextFromMemoryClass s.MemoryClass>
'enum r05_fnresult r05c_' e.Name '(' <FuncArguments> ') {'
'static enum r05_fnresult r05c_' e.Name
'(struct r05_node *arg_begin, struct r05_node *arg_end) {'
);
}

Expand All @@ -37,33 +33,32 @@ $ENTRY GenThisIsGeneratedFunction {
}

$ENTRY GenFail {
=
(' return (enum r05_fnresult)(')
(' R05_RECOGNITION_IMPOSSIBLE | (__LINE__ << 8)')
(' );')
= (' return R05_RECOGNITION_IMPOSSIBLE;')
}

$ENTRY GenFnEnd {
s.MemoryClass e.Name =
('}')
(
<TextFromMemoryClass s.MemoryClass>
'struct r05_function r05f_' e.Name ' = { r05c_' e.Name ', "' e.Name '" };'
)
();
}

$ENTRY GenEnum {
s.ScopeClass e.Name =
<GenFnStart s.ScopeClass e.Name>
('(void) arg_begin;')
('(void) arg_end;')
<GenFail>
<GenFnEnd s.ScopeClass e.Name>;
(
<TextFromMemoryClass s.ScopeClass>
'struct r05_function r05f_' e.Name ' = '
'{ r05_enum_function_code, "' e.Name '" };'
)
();
}

$ENTRY GenDeclaration {
(GN-Entry e.Name) =
('extern enum r05_fnresult r05c_' e.Name '(' <FuncArguments> ');');

(GN-Local e.Name) =
('static enum r05_fnresult r05c_' e.Name '(' <FuncArguments> ');');
(GN-Entry e.Name) = ('extern struct r05_function r05f_' e.Name ';');
(GN-Local e.Name) = ('static struct r05_function r05f_' e.Name ';');
}

GenCommonHeaders {
Expand Down Expand Up @@ -292,7 +287,7 @@ SymbolFunc {
SymbolTextRep {
Char s.Char = '\'' <EscapeChar s.Char> '\'';
Number s.Number = <Symb s.Number> 'UL';
Name e.Name = 'r05c_' e.Name;
Name e.Name = '&r05f_' e.Name;
}

PrintMatchBrackets {
Expand Down Expand Up @@ -386,7 +381,6 @@ SwAllocator {
}

SwInfo {
ElSymbol Name e.Name = 'r05c_' e.Name ', "' e.Name '"';
ElSymbol s.Type e.Value = <SymbolTextRep s.Type e.Value>;
ElString s.Len e.String = '"' <EscapeString e.String> '", ' <Symb s.Len>;

Expand Down

0 comments on commit 5dc0c8e

Please sign in to comment.