Skip to content

Commit

Permalink
Интерпретируемая функция как особый дескриптор (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mazdaywik committed Aug 12, 2016
1 parent cef60c6 commit d5c16ab
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 53 deletions.
32 changes: 15 additions & 17 deletions src/compiler/Generator.sref
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ GenCommand {
(e.Indent)
('extern refalrts::RefalFunction ' <CName e.Name> ';');

(e.Indent) (#CmdExternInterpret e.Name) =
(e.Indent)
('extern refalrts::RASLFunction ' <CName e.Name> ';');

(e.Indent) (#CmdFuncDescr e.Name) =
(e.Indent)
(
Expand All @@ -129,6 +133,17 @@ GenCommand {
)
();

(e.Indent) (#CmdInterpretFuncDescr e.Name) =
(e.Indent)
('refalrts::RASLFunction ' <CName e.Name> '(')
(' "' <DisplayCName e.Name> '",')
(' scope_' <CName e.Name> '::raa,')
(' scope_' <CName e.Name> '::functions,')
(' scope_' <CName e.Name> '::idents,')
(' scope_' <CName e.Name> '::numbers,')
(' scope_' <CName e.Name> '::strings')
(');')
();

(e.Indent) (#CmdSwapDescr e.Name) =
(e.Indent)
Expand Down Expand Up @@ -539,15 +554,6 @@ GenCommand {
// ---------------------------------------------------------------------------
// Команды режима интерпретации

(e.Indent) (#CmdAcceptNamespace e.Name) =
(e.Indent)
(e.Indent 'using scope_' <CName e.Name> '::functions;')
(e.Indent 'using scope_' <CName e.Name> '::idents;')
(e.Indent 'using scope_' <CName e.Name> '::numbers;')
(e.Indent 'using scope_' <CName e.Name> '::strings;')
(e.Indent 'using scope_' <CName e.Name> '::raa;')
();

(e.Indent) (#CmdiFuncArray) =
(e.Indent)
(e.Indent 'using refalrts::functions;');
Expand Down Expand Up @@ -621,14 +627,6 @@ GenCommand {
(e.Indent ' {refalrts::icEnd, 0, 0, 0}')
(e.Indent '};');

(e.Indent) (#CmdiRunInterpret) =
(e.Indent)
(e.Indent 'refalrts::FnResult res = refalrts::interpret_array(')
(e.Indent ' raa, arg_begin, arg_end,')
(e.Indent ' functions, idents, numbers, strings')
(e.Indent ');')
(e.Indent 'return res;');

(e.Indent) (#CmdiProfileFunction) =
(e.Indent)
(e.Indent '{refalrts::icProfileFunction, 0, 0, 0},');
Expand Down
62 changes: 42 additions & 20 deletions src/compiler/LowLevelRASL.sref
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ $ENTRY LowLevelRASL {
(#Function s.ScopeClass (e.Name) e.Commands) =
(
e.Declarations
<InUnnamedNamespace s.ScopeClass (#CmdExtern e.Name)>
<Function-Extern s.GenMode s.ScopeClass e.Name>
)
<Function
s.GenMode s.ScopeClass (e.Name)
Expand Down Expand Up @@ -479,6 +479,25 @@ Freeze {
<Map { s.Number = (s.Number #Frozen); } e.BracketNumbers>;
}

Function-Extern {
#Both s.ScopeClass e.Name =
// Интерпретируемый код
(#CmdIfDef 'INTERPRET')
<Function-Extern #OnlyInterpret s.ScopeClass e.Name>
(#CmdElse)
// Компилируемый код
<Function-Extern #OnlyDirect s.ScopeClass e.Name>
(#CmdEndIf);

#OnlyDirect s.ScopeClass e.Name =
// Компилируемый код
<InUnnamedNamespace s.ScopeClass (#CmdExtern e.Name)>;

#OnlyInterpret s.ScopeClass e.Name =
// Интерпретируемый код
<InUnnamedNamespace s.ScopeClass (#CmdExternInterpret e.Name)>;
}

Function {
#Both s.ScopeClass (e.Name) e.Commands =
// Интерпретируемый код
Expand All @@ -491,19 +510,15 @@ Function {

#OnlyDirect s.ScopeClass (e.Name) e.Commands =
// Компилируемый код
(#CmdFnStart e.Name)
<Function-Direct e.Commands>
(#CmdFnEnd)
<InUnnamedNamespace s.ScopeClass (#CmdFuncDescr e.Name)>;
<Function-Direct s.ScopeClass (e.Name) e.Commands>;

#OnlyInterpret s.ScopeClass (e.Name) e.Commands =
// Интерпретируемый код
<Function-Interpret (e.Name) e.Commands>
<InUnnamedNamespace s.ScopeClass (#CmdFuncDescr e.Name)>;
<Function-Interpret s.ScopeClass (e.Name) e.Commands>;
}

Function-Interpret {
(e.Name) (#CmdProfileFunction) (#CmdIssueMem s.Memory) e.FunctionBody =
s.ScopeClass (e.Name) e.FunctionBody =
<Fetch
e.FunctionBody
<Seq
Expand All @@ -512,8 +527,6 @@ Function-Interpret {
{
e.FunctionBody^ =
(#CmdInitRAA)
(#CmdiProfileFunction)
(#CmdiIssueMem s.Memory)
e.FunctionBody
(#CmdFinRAA);
}
Expand All @@ -525,14 +538,15 @@ Function-Interpret {
)
{
e.FunctionBody^ =
(#CmdNamespaceStart)
(#CmdNamespaceStart e.Name)
e.FunctionBody
(#CmdNamespaceEnd e.Name)

(#CmdFnStart e.Name)
(#CmdAcceptNamespace e.Name)
(#CmdiRunInterpret)
(#CmdFnEnd);
(#CmdNamespaceEnd)
<InUnnamedNamespace
s.ScopeClass
(#CmdInterpretFuncDescr e.Name)
>;
}
>
>;
Expand Down Expand Up @@ -1024,19 +1038,27 @@ PrepareOpenEStack {
}

Function-Direct {
s.ScopeClass (e.Name) e.Body =
(#CmdFnStart e.Name)
<Function-DirectRec e.Body>
(#CmdFnEnd)
<InUnnamedNamespace s.ScopeClass (#CmdFuncDescr e.Name)>;
}

Function-DirectRec {
(#CmdSentence e.SubCommands) e.Tail =
(#CmdStartSentence)
<Function-Direct e.SubCommands>
<Function-DirectRec e.SubCommands>
(#CmdEndSentence)
<Function-Direct e.Tail>;
<Function-DirectRec e.Tail>;

(#CmdOpenELoop #AlgLeft s.BracketNum s.VarNumber e.SubCommands) e.Tail =
(#CmdOpenedE-Start #AlgLeft s.BracketNum s.VarNumber)
<Function-Direct e.SubCommands>
<Function-DirectRec e.SubCommands>
(#CmdOpenedE-End #AlgLeft s.BracketNum s.VarNumber)
<Function-Direct e.Tail>;
<Function-DirectRec e.Tail>;

t.Command e.Tail = t.Command <Function-Direct e.Tail>;
t.Command e.Tail = t.Command <Function-DirectRec e.Tail>;

/* пусто */ = /* пусто */;
}
22 changes: 15 additions & 7 deletions src/srlib/refalrts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2908,14 +2908,22 @@ void refalrts::vm::free_view_field() {
//==============================================================================

#ifndef MODULE_REFAL
refalrts::FnResult refalrts::interpret_array(
const refalrts::RASLCommand raa[],
refalrts::Iter begin, refalrts::Iter end,
RefalFunction *functions[],
const RefalIdentifier idents[],
const RefalNumber numbers[],
const StringItem strings[]
refalrts::FnResult refalrts::RASLFunction::run(
refalrts::Iter begin, refalrts::Iter end
) {
Iter info_b = 0;
Iter info_e = 0;
Iter func_name = call_left(info_b, info_e, begin, end);

assert( RASLFunction::run == func_name->function_info->ptr );
RASLFunction *descr = static_cast<RASLFunction*>(func_name->function_info);

const RASLCommand *raa = descr->raa;
RefalFunction **functions = descr->functions;
const RefalIdentifier *idents = descr->idents;
const RefalNumber *numbers = descr->numbers;
const StringItem *strings = descr->strings;

refalrts::vm::Stack<int>& open_e_stack = refalrts::vm::g_open_e_stack;
refalrts::vm::Stack<Iter>& context = refalrts::vm::g_context;

Expand Down
36 changes: 27 additions & 9 deletions src/srlib/refalrts.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,33 @@ void debug_print_expr(void *file, Iter first, Iter last);
// Интерпретатор

#ifndef MODULE_REFAL
extern FnResult interpret_array(
const RASLCommand raa[],
Iter begin,
Iter end,
RefalFunction *functions[],
const RefalIdentifier idents[],
const RefalNumber numbers[],
const StringItem strings[]
);
struct RASLFunction: public RefalFunction {
const RASLCommand *raa;
RefalFunction **functions;
const RefalIdentifier *idents;
const RefalNumber *numbers;
const StringItem *strings;

RASLFunction(
RefalFuncName name,
const RASLCommand raa[],
RefalFunction *functions[],
const RefalIdentifier idents[],
const RefalNumber numbers[],
const StringItem strings[]
)
: RefalFunction(run, name)
, raa(raa)
, functions(functions)
, idents(idents)
, numbers(numbers)
, strings(strings)
{
/* пусто */
}

static FnResult run(Iter begin, Iter end);
};
#endif

extern RefalFunction *functions[];
Expand Down

0 comments on commit d5c16ab

Please sign in to comment.