Skip to content

Commit

Permalink
$INIT и $FINAL: подготовка к внедрению нового синтаксиса (#2)
Browse files Browse the repository at this point in the history
1. Теперь функции Init и Final, чтобы стать инициализатором и финализатором,
   могут оставаться локальными.
2. Поля инициализатора и финализатора в IModule заполняются не при первом
   появлении функций Go, Init или Final, а после того, как анализ дошёл
   до директивы $END. Таким образом, если внедрить обработку $INIT и $FINAL
   в тело модуля, функции Init и Final не перезапишут его собой.
  • Loading branch information
Mazdaywik committed May 3, 2016
1 parent 9663056 commit 4ecd147
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 84 deletions.
86 changes: 4 additions & 82 deletions Sources/Compiler/FE-MRefal/MParser_Common.mref
Original file line number Diff line number Diff line change
Expand Up @@ -107,68 +107,13 @@ AddNewFunction-SwRedeclaration {

t.SrcPos t.NewName (e.Attribs) t.hErrorList
t.hIModule NoName =
<AddNewFunction-SwEntryPoint
t.SrcPos
( t.NewName <MQualifiedName::Parse t.NewName> ) e.Attribs
<UpdateEntity-GuardInvariant
t.hErrorList
<MIModule::UpdateFunction
t.hIModule t.NewName e.Attribs (Position t.SrcPos)
>
t.SrcPos t.NewName
>
>;
}

AddNewFunction-SwEntryPoint {
t.SrcPos ( t.FuncName e.ModuleName (e.LastSegment) )
e.Attribs-B (ScopeClass Entry) e.Attribs-E
t.hIModule t.hErrorList =
<AddNewFunction-SwEntryPoint-AbsolutName
t.SrcPos ( e.LastSegment )
<MIModule::AbsolutName t.hIModule t.FuncName>
<UpdateEntity-GuardInvariant
t.hErrorList
>;

t.SrcPos ( t.FuncName e.Parsed ) e.Attribs t.hIModule t.hErrorList =
t.hIModule t.hErrorList;
}

AddNewFunction-SwEntryPoint-AbsolutName
t.SrcPos ( e.LastSegment )
t.hIModule t.FuncName t.hErrorList =
<AddNewFunction-SwEntryPoint-Aux
t.SrcPos ( t.FuncName e.LastSegment )
<MIModule::GetModuleAttribute
t.hIModule ModuleType
<MIModule::UpdateFunction
t.hIModule t.NewName e.Attribs (Position t.SrcPos)
>
t.hErrorList
t.SrcPos t.NewName
>;

AddNewFunction-SwEntryPoint-Aux {
t.SrcPos ( t.FuncName 'Go' ) t.hIModule Found Main t.hErrorList =
<MIModule::SetGoEntryPoint
t.hIModule
(Function (Entry t.SrcPos t.FuncName))
>
t.hErrorList;

t.SrcPos ( t.FuncName 'Init' ) t.hIModule Found Regular t.hErrorList =
<MIModule::SetInitEntryPoint
t.hIModule
(Function (Entry t.SrcPos t.FuncName))
>
t.hErrorList;

t.SrcPos ( t.FuncName 'Final' ) t.hIModule Found Regular t.hErrorList =
<MIModule::SetFinalEntryPoint
t.hIModule
(Function (Entry t.SrcPos t.FuncName))
>
t.hErrorList;

t.SrcPos ( t.FuncName e.Name ) t.hIModule Found s.Type t.hErrorList =
t.hIModule t.hErrorList;
}

$ENTRY AddNewADT
Expand Down Expand Up @@ -437,34 +382,11 @@ NModuleHeader-ExpectEndSent {
*/
$ENTRY NModuleTailer {
t.hIModule t.hErrorList t.hLexer (Directive t.SrcPos End) =
<NModuleTailer-CheckGo
<MIModule::GetEntryPoint t.hIModule>
t.SrcPos t.hErrorList t.hLexer
>;
}

NModuleTailer-CheckGo {
t.hIModule Regular t.InitSeq t.FinalSeq
t.SrcPos t.hErrorList t.hLexer =
<NModuleTailer-ModuleName
t.hIModule t.hErrorList t.hLexer
>;

t.hIModule Go (Result e.Result)
t.SrcPos t.hErrorList t.hLexer =
<NModuleTailer-ModuleName
t.hIModule t.hErrorList t.hLexer
>;

t.hIModule Go None t.SrcPos t.hErrorList t.hLexer =
<NModuleTailer-ModuleName
t.hIModule
<MError::AddGoNotFound t.hErrorList t.SrcPos>
t.hLexer
>;
}


NModuleTailer-ModuleName {
t.hIModule t.hErrorList t.hLexer =
<NModuleTailer-ModuleName-Aux
Expand Down
133 changes: 131 additions & 2 deletions Sources/Compiler/FE-MRefal/MParser_MRefal.mref
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
$MODULE FE-MRefal::MParser_MRefal;

$IMPORT MError;
$IMPORT MIModule;
$IMPORT MLambda;
$IMPORT MLexer = FE-MRefal::MLexer;
$IMPORT MParser_Common = FE-MRefal::MParser_Common;
$IMPORT MQualifiedName;
Expand Down Expand Up @@ -101,8 +103,8 @@ NModuleBody {
t.IModule
t.hErrorList t.Lexer (Directive t.SrcPos End) =
<MParser_Common::NModuleTailer
t.IModule
t.hErrorList t.Lexer (Directive t.SrcPos End)
<FixupEntryPoints t.IModule t.hErrorList t.SrcPos>
t.Lexer (Directive t.SrcPos End)
>;

/* При обнаружении конца файла завершаем анализ */
Expand Down Expand Up @@ -131,6 +133,133 @@ NModuleBody {
>;
}

FixupEntryPoints {
t.hIModule t.hErrorList t.SrcPos =
<FixupEntryPoints-SwModuleType
<MIModule::GetModuleAttribute t.hIModule ModuleType>
t.hErrorList t.SrcPos
>;
}

FixupEntryPoints-SwModuleType {
t.hIModule Found Main t.hErrorList t.SrcPos =
<FixupEntryPoints-Go
<MIModule::AbsolutName
t.hIModule <MQualifiedName::Create ('Go')>
>
<MQualifiedName::Create ('Go')>
t.hErrorList t.SrcPos
>;

t.hIModule Found Regular t.hErrorList t.SrcPos =
<FixupEntryPoints-InitFinal
<MIModule::GetEntryPoint t.hIModule>
>
t.hErrorList;
}

FixupEntryPoints-Go {
t.hIModule t.GoName t.RelativeName t.hErrorList t.SrcPos =
<FixupEntryPoints-Go-SwExist
<MIModule::GetKind t.hIModule t.GoName>
t.GoName t.RelativeName t.hErrorList t.SrcPos
>;
}

FixupEntryPoints-Go-SwExist {
t.hIModule Success Function t.GoName t.RelativeName t.hErrorList t.SrcPos =
<FixupEntryPoints-Go-SetEntryPoint
<MLambda::Apply
<MLambda::Parallel-H
<MLambda::BindLeft-H
&MIModule.GetFunctionAttribute ScopeClass t.RelativeName
>
<MLambda::BindLeft-H
&MIModule.GetFunctionAttribute Position t.RelativeName
>
>
t.hIModule
>
t.GoName t.hErrorList t.SrcPos
>;

t.hIModule e.OtherKindOrFails t.GoName t.RelativeName t.hErrorList t.SrcPos =
t.hIModule
<MError::AddGoNotFound t.hErrorList t.SrcPos>;
}

FixupEntryPoints-Go-SetEntryPoint {
t.hIModule Found Entry Found t.GoSrcPos t.GoName t.hErrorList t.EndSrcPos =
<MIModule::SetGoEntryPoint
t.hIModule
(Function (Entry t.GoSrcPos t.GoName))
>
t.hErrorList;

t.hIModule Found s.Other Found t.GoSrcPos t.GoName t.hErrorList t.EndSrcPos =
t.hIModule
<MError::AddGoNotEntry t.hErrorList t.GoSrcPos>;
}

FixupEntryPoints-InitFinal {
t.hIModule Regular t.InitSeq t.FinalSeq =
<MLambda::Reduce
&FixupInitFinal
t.hIModule
(&MIModule.SetInitEntryPoint t.InitSeq 'Init')
(&MIModule.SetFinalEntryPoint t.FinalSeq 'Final')
>;
}

FixupInitFinal {
t.hIModule (s.FnChanger None e.FuncName) =
<FixupInitFinal-AbsolutName
s.FnChanger t.hIModule <MQualifiedName::Create (e.FuncName)>
>;

t.hIModule (s.FnChanger (Result e.Result) e.FuncName) =
// Уже точка входа задана
t.hIModule;
}

FixupInitFinal-AbsolutName {
s.FnChanger t.hIModule t.RelativeName =
<FixupInitFinal-GetScopeClass
s.FnChanger t.RelativeName
<MIModule::AbsolutName t.hIModule t.RelativeName>
>;
}

FixupInitFinal-GetScopeClass {
s.FnChanger t.RelativeName t.hIModule t.FuncName =
<FixupInitFinal-SetEntryCode
s.FnChanger t.FuncName
<MLambda::Apply
<MLambda::Parallel-H
<MLambda::BindLeft-H
&MIModule.GetFunctionAttribute ScopeClass t.RelativeName
>
<MLambda::BindLeft-H
&MIModule.GetFunctionAttribute Position t.RelativeName
>
>
t.hIModule
>
>;
}

FixupInitFinal-SetEntryCode {
s.FnChanger t.FuncName t.hIModule Found s.ScopeClass Found t.SrcPos =
<s.FnChanger
t.hIModule
(Function (s.ScopeClass t.SrcPos t.FuncName))
>;

s.FnChanger t.FuncName t.hIModule NoName NoName =
/* Нет такой функции */
t.hIModule;
}

//------------------------------------------------------------------------------

NSwapBlock {
Expand Down
14 changes: 14 additions & 0 deletions Sources/Compiler/MError.mref
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,20 @@ $ENTRY AddGoNotFound {

//------------------------------------------------------------------------------

/**
<AddGoNotEntry t.hErrorList t.SrcPos>
== t.hErrorList
*/
$ENTRY AddGoNotEntry {
t.hErrorList t.SrcPos =
<AddToList
t.hErrorList
( Semantics t.SrcPos 'Go function must be $ENTRY' )
>;
}

//------------------------------------------------------------------------------

ErrorFile = '__errors';

/**
Expand Down

0 comments on commit 4ecd147

Please sign in to comment.