Skip to content

Commit

Permalink
Переписаны почти все нативные вставки в back-end’е Рефала-5 (#2)
Browse files Browse the repository at this point in the history
Теперь большинство функций платформенно-зависимых модулей написано
на Модульном Рефале и вызывает функции из Platform::Refal5.

Нативные вставки остались там, где они необходимы или без них будет хуже.
  • Loading branch information
Mazdaywik committed Feb 17, 2019
1 parent ef567c5 commit 5ed901d
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 171 deletions.
105 changes: 48 additions & 57 deletions Sources/Libraries/all.r5-b/CoreBE/MFileIO.mref
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
$MODULE CoreBE::MFileIO;

$IMPORT Platform::Refal5;


/*
Функция намеренно оставлена в виде нативной вставки ради лучшей диагностики
ошибок: благодаря условиям и блокам в дампе будет падение именно
CoreBE::FileIO::Open, а не каких-то её «кишок».
*/
$NATIVE Refal5 FUNCTION
$ENTRY Open
@@'
c {
c 'b' s.Mode e.FileName
c , 'rwa'
c : e.X s.Mode e.Y
c , <$Local::CoreBE::MFileIO::Open-CheckHandles$ >
c :
c , <Dg FreeHandles > :
c {
r //Найден свободный дескриптор
Expand All @@ -23,8 +29,6 @@ c };
c s.Mode e.FileName
c , 'rwa'
c : e.X s.Mode e.Y
c , <$Local::CoreBE::MFileIO::Open-CheckHandles$ >
c :
c , <Dg FreeHandles > :
c {
r //Найден свободный дескриптор
Expand All @@ -38,70 +42,57 @@ c };
c }
'@@;

$NATIVE Refal5 FUNCTION
Open-CheckHandles
@@'
c {
c , <Cp FreeHandles > :
c {
c Handles e.Handles = ;
c = <Br FreeHandles '=' Handles 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 >;
c }
c }
'@@;
// Эти алиасы используются только в этом модуле
$NATIVE Refal5 FUNCTION FreeHandles ALIAS FreeHandles;
$NATIVE Refal5 FUNCTION Handles ALIAS Handles;

$INIT
<Platform.Refal5.Br
&FreeHandles '=' &Handles 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
>;

$NATIVE Refal5 FUNCTION
$ENTRY Close
@@'
c {
c s.Handle =
c <Close s.Handle >
c <Br FreeHandles '=' <Dg FreeHandles> s.Handle >;
c }
'@@;
{
s.Handle =
<Platform.Refal5.Close s.Handle >
<Platform.Refal5.Br
&FreeHandles '=' <Platform.Refal5.Dg &FreeHandles> s.Handle
>;
}

$NATIVE Refal5 FUNCTION
$ENTRY ReadLine
@@'
c {
c StdIn = <Card >;
c s.Handle = <Get s.Handle >;
c }
'@@;
{
&StdIn = <Platform.Refal5.Card>;
s.Handle = <Platform.Refal5.Get s.Handle>;
}

$NATIVE Refal5 FUNCTION
$ENTRY StdIn
@@'
c {
c = StdIn ;
c }
'@@;
{
= &StdIn;
}

$NATIVE Refal5 FUNCTION
$ENTRY StdOut
@@'
c {
c = StdOut ;
c }
'@@;
{
= &StdOut;
}

$NATIVE Refal5 FUNCTION
$ENTRY Write
@@'
c {
c StdOut e.Text '\n' = <Prout <Dg CachedLine > e.Text >;
c StdOut e.Text = <Br CachedLine '=' <Dg CachedLine > e.Text >;
c s.Handle e.Text '\n' = <Putout s.Handle e.Text >;
c s.Handle e.Text = <Write s.Handle e.Text >;
c }
'@@;
{
&StdOut e.Text '\n' =
<Platform.Refal5.Prout <Platform.Refal5.Dg CachedLine> e.Text >;

&StdOut e.Text =
<Platform.Refal5.Br
CachedLine '=' <Platform.Refal5.Dg CachedLine> e.Text
>;

s.Handle e.Text '\n' = <Platform.Refal5.Putout s.Handle e.Text>;
s.Handle e.Text = <Platform.Refal5.Write s.Handle e.Text>;
}

$NATIVE Refal5 FUNCTION
$ENTRY FlushIO
@@'
c {
c = <Prout <Dg CachedLine > >;
c }
'@@;
{
= <Platform.Refal5.Prout <Platform.Refal5.Dg CachedLine>>;
}

$END CoreBE::MFileIO.
74 changes: 36 additions & 38 deletions Sources/Libraries/all.r5-b/CoreBE/MOrder.mref
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
$MODULE CoreBE::MOrder;

$NATIVE Refal5 FUNCTION
$ENTRY SymbCompare @@'
c {
c s.Left s.Right
c , <$Local::CoreBE::MOrder::GetType$ s.Left >
c <$Local::CoreBE::MOrder::GetType$ s.Right >:
c {
c s.Type s.LeftVal s.Type s.RightVal
c , <Compare s.LeftVal s.RightVal >:
c {
c '-' = '<';
c '0' = '=';
c '+' = '>';
c };

c Number s.LeftVal s.Type s.RightVal = '>';
c s.Type s.LeftVal Number s.RightVal = '<';
c Char s.LeftVal s.Type s.RightVal = '>';
c s.Type s.LeftVal Char s.RightVal = '<';
c s.L-Type s.L-Val s.R-Type s.R-Val = '=';
c }
c }
'@@;

$NATIVE Refal5 FUNCTION
GetType @@'
c {
c s.Atom, <Type s.Atom >:
c {
c 'N' e.1 = Number s.Atom;
c 'L' e.1 = Char <Ord s.Atom >;
c 'O' e.1 = Char <Ord s.Atom >;
c 'P' e.1 = Char <Ord s.Atom >;
c 'D' e.1 = Char <Ord s.Atom >;
c e.1 = Other 0;
c }
c }
'@@;
$IMPORT Platform::Refal5;


$ENTRY SymbCompare {
s.Left s.Right = <SymbCompare-SwTypes <GetType s.Left> <GetType s.Right>>;
}

SymbCompare-SwTypes {
s.Type s.LeftVal s.Type s.RightVal =
<SymbCompare-SwTypes-Sign <Platform.Refal5.Compare s.LeftVal s.RightVal>>;

Number s.LeftVal s.Type s.RightVal = '>';
s.Type s.LeftVal Number s.RightVal = '<';
Char s.LeftVal s.Type s.RightVal = '>';
s.Type s.LeftVal Char s.RightVal = '<';
s.L-Type s.L-Val s.R-Type s.R-Val = '=';
}

SymbCompare-SwTypes-Sign {
'-' = '<';
'0' = '=';
'+' = '>';
}

GetType {
s.Atom = <GetType-SwType <Platform.Refal5.Type s.Atom>>;
}

GetType-SwType {
'N' s.SybType s.Atom = Number s.Atom;
'L' s.SybType s.Atom = Char <Platform.Refal5.Ord s.Atom>;
'O' s.SybType s.Atom = Char <Platform.Refal5.Ord s.Atom>;
'P' s.SybType s.Atom = Char <Platform.Refal5.Ord s.Atom>;
'D' s.SybType s.Atom = Char <Platform.Refal5.Ord s.Atom>;
s.Type s.SybType s.Atom = Other 0;
}

$END CoreBE::MOrder.
75 changes: 32 additions & 43 deletions Sources/Libraries/all.r5-b/CoreBE/MSelfDiag.mref
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
$MODULE CoreBE::MSelfDiag;

$NATIVE Refal5 FUNCTION
$ENTRY CloseLog @@'
c {
c , <Dg LogHandle>:
c {
c s.Handle = <Close s.Handle >;
c = ;
c }
c }
'@@;
$IMPORT Platform::Refal5;


$ENTRY CloseLog {
= <CloseLog-SwOpened <Platform.Refal5.Dg LogHandle>>;
}

CloseLog-SwOpened {
s.Handle = <Platform.Refal5.Close s.Handle>;
/* пусто */ = ;
}

$NATIVE Refal5 FUNCTION
$ENTRY ExitFailure @@'
Expand All @@ -18,41 +19,29 @@ c s.ExitCode = <$Entry::__Exit$ 1 >;
c }
'@@;

$NATIVE Refal5 FUNCTION
$ENTRY Log @@'
c {
c e.Message =
c <Putout <$Local::CoreBE::MSelfDiag::GetHandle$ > e.Message >;
c }
'@@;
$ENTRY Log {
e.Message = <Platform.Refal5.Putout <GetHandle> e.Message>;
}

$NATIVE Refal5 FUNCTION
$ENTRY Log-T @@'
c {
c e.Message =
c <Put <$Local::CoreBE::MSelfDiag::GetHandle$ > e.Message >;
c }
'@@;
$ENTRY Log-T {
e.Message = <Platform.Refal5.Put <GetHandle> e.Message>;
}

$NATIVE Refal5 FUNCTION
GetHandle @@'
c {
c , <Cp LogHandle >:
c {
c s.Handle = s.Handle;
c =
c <Open 'w' 19 '__log' >
c <Br LogHandle '=' 19 >
c 19;
c };
c }
'@@;
GetHandle {
= <GetHandle-SwOpened <Platform.Refal5.Cp LogHandle>>;
}

$NATIVE Refal5 FUNCTION
$ENTRY PrintMessage @@'
c {
c e.Message = <Write 0 e.Message '\n' >;
c }
'@@;
GetHandle-SwOpened {
s.Handle = s.Handle;

/* пусто */ =
<Platform.Refal5.Open 'w' 19 '__log' >
<Platform.Refal5.Br LogHandle '=' 19 >
19;
}

$ENTRY PrintMessage {
e.Message = <Platform.Refal5.Write 0 e.Message '\n'>;
}

$END CoreBE::MSelfDiag.
46 changes: 24 additions & 22 deletions Sources/Libraries/all.r5-b/CoreBE/Math.mref
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
$MODULE CoreBE::Math;

$NATIVE Refal5 FUNCTION
$ENTRY Binary @@'
c {
c '+' s.Left s.Right , <Add s.Left s.Right > : e.Car s.Last = s.Last;
c '-' s.Left s.Right , <Sub s.Left s.Right > : e.Car s.Last = s.Last;
c '*' s.Left s.Right , <Mul s.Left s.Right > : e.Car s.Last = s.Last;
c '/' s.Left s.Right = <Div s.Left s.Right >;
c '%' s.Left s.Right = <Mod s.Left s.Right >;
c }
'@@;

$NATIVE Refal5 FUNCTION
$ENTRY NumberCompare @@'
c {
c s.Left s.Right, <Compare s.Left s.Right >:
c {
c '-' = '<';
c '0' = '=';
c '+' = '>';
c }
c }
'@@;
$IMPORT Platform::Refal5;


$ENTRY Binary {
'+' s.Left s.Right = <Last <Platform.Refal5.Add s.Left s.Right>>;
'-' s.Left s.Right = <Last <Platform.Refal5.Sub s.Left s.Right>>;
'*' s.Left s.Right = <Last <Platform.Refal5.Mul s.Left s.Right>>;
'/' s.Left s.Right = <Platform.Refal5.Div s.Left s.Right>;
'%' s.Left s.Right = <Platform.Refal5.Mod s.Left s.Right>;
}

Last { e.Car s.Last = s.Last; }


$ENTRY NumberCompare {
s.Left s.Right =
<NumberCompare-SwSign <Platform.Refal5.Compare s.Left s.Right>>;
}

NumberCompare-SwSign {
'-' = '<';
'0' = '=';
'+' = '>';
}

$END CoreBE::Math.
7 changes: 7 additions & 0 deletions Sources/Libraries/all.r5-b/Platform/Refal5.mref
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ $MODULE Platform::Refal5;
$NATIVE Refal5 FUNCTION $ENTRY Add ALIAS Add;
$NATIVE Refal5 FUNCTION $ENTRY Arg ALIAS Arg;
$NATIVE Refal5 FUNCTION $ENTRY Br ALIAS Br;
$NATIVE Refal5 FUNCTION $ENTRY Card ALIAS Card;
$NATIVE Refal5 FUNCTION $ENTRY Chr ALIAS Chr;
$NATIVE Refal5 FUNCTION $ENTRY Close ALIAS Close;
$NATIVE Refal5 FUNCTION $ENTRY Compare ALIAS Compare;
$NATIVE Refal5 FUNCTION $ENTRY Cp ALIAS Cp;
$NATIVE Refal5 FUNCTION $ENTRY Dg ALIAS Dg;
$NATIVE Refal5 FUNCTION $ENTRY Div ALIAS Div;
$NATIVE Refal5 FUNCTION $ENTRY ExistFile ALIAS ExistFile;
$NATIVE Refal5 FUNCTION $ENTRY False ALIAS False;
$NATIVE Refal5 FUNCTION $ENTRY Get ALIAS Get;
$NATIVE Refal5 FUNCTION $ENTRY Mod ALIAS Mod;
$NATIVE Refal5 FUNCTION $ENTRY Mu ALIAS Mu;
$NATIVE Refal5 FUNCTION $ENTRY Mul ALIAS Mul;
$NATIVE Refal5 FUNCTION $ENTRY Numb ALIAS Numb;
$NATIVE Refal5 FUNCTION $ENTRY Open ALIAS Open;
$NATIVE Refal5 FUNCTION $ENTRY Ord ALIAS Ord;
$NATIVE Refal5 FUNCTION $ENTRY Prout ALIAS Prout;
$NATIVE Refal5 FUNCTION $ENTRY Put ALIAS Put;
$NATIVE Refal5 FUNCTION $ENTRY Putout ALIAS Putout;
$NATIVE Refal5 FUNCTION $ENTRY Random ALIAS Random;
$NATIVE Refal5 FUNCTION $ENTRY RemoveFile ALIAS RemoveFile;
$NATIVE Refal5 FUNCTION $ENTRY Sub ALIAS Sub;
$NATIVE Refal5 FUNCTION $ENTRY Symb ALIAS Symb;
$NATIVE Refal5 FUNCTION $ENTRY System ALIAS System;
$NATIVE Refal5 FUNCTION $ENTRY Time ALIAS Time;
Expand Down
Loading

0 comments on commit 5ed901d

Please sign in to comment.