about summary refs log blame commit diff stats
path: root/034exclusive_container.cc
blob: 8247cb42420d47f6cea147e397b6e8b937a601ed (plain) (tree)
69f3fb0a pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
.    if \nF \{
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "RIFLE 1"
.TH RIFLE 1 "rifle-1.7.1" "05/04/2015" "rifle manual"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
rifle \- ranger's file opener
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
\&\fBrifle\fR [\fB\-\-help\fR] [\fB\-f\fR \fI\s-1FLAGS\s0\fR] [\fB\-l\fR] [\fB\-p\fR \fI\s-1KEYWORD\s0\fR]
[\fB\-w\fR \fI\s-1PROGRAM\s0\fR] \fIfiles\fR
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
rifle is a powerful file executor that allows for complex file type checking,
written to meet the needs of the file manager \fIranger\fR.  rifle's strength lies
in automatically determining file types, depending on which programs are
installed on the system, even without any user interaction.
.SH "OPTIONS"
.IX Header "OPTIONS"
.IP "\fB\-f\fR \fI\s-1FLAGS\s0\fR" 14
.IX Item "-f FLAGS"
Specify flags for opening the files.  Flags are letters that changes how the
program is executed.  Any combination of flags will work.  Writing uppercase
flags will negate the effect of all previously used lowercase flags of the same
letter.
.Sp
Table of all flags:
 f   fork program to background
 r   run program as root, using sudo
 t   run program in a separate terminal, as specified by \f(CW$TERMCMD\fR
.IP "\fB\-l\fR" 14
.IX Item "-l"
List all possible ways to open the specified files.  Each line will contain information in the format of \fIid:label:flags:command\fR. \fIid\fR is the identification number. \fIlabel\fR is an arbitrary string that was specified for this command, \fIflags\fR are the flags that are used by default, and \fIcommand\fR is the command that is going to be executed.
.IP "\fB\-p\fR \fI\s-1KEYWORD\s0\fR" 14
.IX Item "-p KEYWORD"
Pick a method to open the files.
.Sp
\&\fI\s-1KEYWORD\s0\fR is either the \s-1ID\s0 number listed by \f(CW\*(C`rifle \-l\*(C'\fR or a string that matches a label in the configuration file.
.IP "\fB\-w\fR \fI\s-1PROGRAM\s0\fR" 14
.IX Item "-w PROGRAM"
Open the files with the program \fI\s-1PROGRAM\s0\fR
.IP "\fB\-h\fR, \fB\-\-help\fR" 14
.IX Item "-h, --help"
Print a list of options and exit.
.SH "FILES"
.IX Header "FILES"
rifle shares configuration files with ranger, though ranger is not required in
order to use rifle.  The configuration file \fIrifle.conf\fR is expected to be at
\&\fI~/.config/ranger/rifle.conf\fR.
.PP
This file specifies patterns for determining the commands to open files with.
The syntax is described in the comments of the default \fIrifle.conf\fR that ships
with ranger.  To obtain it, you need to run: \f(CW\*(C`ranger \-\-copy\-config=rifle\*(C'\fR
.SH "ENVIRONMENT"
.IX Header "ENVIRONMENT"
.IP "\s-1EDITOR\s0" 8
.IX Item "EDITOR"
Determines which editor to use for editing files (in the default \fIrifle.conf\fR).
.IP "\s-1PAGER\s0" 8
.IX Item "PAGER"
Determines which pager to use for displaying files (in the default \fIrifle.conf\fR).
.IP "\s-1TERMCMD\s0" 8
.IX Item "TERMCMD"
Determines the terminal emulator command for use with the \fIt\fR flag.  It is required that the value is the path to an executable file which accepts the \*(L"\-e \s-1COMMAND\*(R"\s0 argument.
.IP "\s-1XDG_CONFIG_HOME\s0" 8
.IX Item "XDG_CONFIG_HOME"
Specifies the directory for configuration files. Defaults to \fI\f(CI$HOME\fI/.config\fR.
.SH "EXAMPLES"
.IX Header "EXAMPLES"
List all the different methods:
.PP
.Vb 4
\& $ rifle \-l helloworld.py
\& 0:editor::"$EDITOR" \-\- "$@"
\& 1:pager::"$PAGER" \-\- "$@"
\& 2:::python \-\- "$1"
.Ve
.PP
Display its content by opening it with \*(L"cat\*(R":
.PP
.Vb 2
\& $ rifle \-w cat helloworld.py
\& print("Hello World!")
.Ve
.PP
Run it by picking the method 2, which calls 'python \*(-- \*(L"$1\*(R"':
.PP
.Vb 2
\& $ rifle \-p 2 helloworld.py
\& Hello World!
.Ve
.PP
Display the file in a pager inside a new terminal, run as root:
.PP
.Vb 1
\& $ rifle \-p 1 \-f tr helloworld.py
.Ve
                                                   


                                 
                                                   




                                             


                                                                             
                                    

                                                                                           


                                               
                                                           







                                                                      
                                                   
                                                                   
                                                                                                                                                  



                                                                               
                                                                                                 




                                   
 
                                                                       








                                                                           
                         
             


                              
                                                                               


                               
                              
             


                              
                                                                               


                              



                                               

                                                
                                                                   
                                          
                                           
                                                      
                                                               
                                                                    
                       
                                                                
                            

        
               
   
                     
                                   

        

















                                                         
//: Exclusive containers contain exactly one of a fixed number of 'variants'
//: of different types.
//:
//: They also implicitly contain a tag describing precisely which variant is
//: currently stored in them.

:(before "End Mu Types Initialization")
//: We'll use this container as a running example, with two number elements.
{
type_number tmp = Type_number["number-or-point"] = Next_type_number++;
Type[tmp].size = 2;
Type[tmp].kind = exclusive_container;
Type[tmp].name = "number-or-point";
//? cout << tmp << ": " << SIZE(Type[tmp].elements) << '\n'; //? 1
vector<type_number> t1;
t1.push_back(number);
Type[tmp].elements.push_back(t1);
//? cout << SIZE(Type[tmp].elements) << '\n'; //? 1
vector<type_number> t2;
t2.push_back(point);
Type[tmp].elements.push_back(t2);
//? cout << SIZE(Type[tmp].elements) << '\n'; //? 1
//? cout << "point: " << point << '\n'; //? 1
Type[tmp].element_names.push_back("i");
Type[tmp].element_names.push_back("p");
}

//: Tests in this layer often explicitly setup memory before reading it as an
//: array. Don't do this in general. I'm tagging exceptions with /raw to
//: avoid warnings.
:(scenario copy_exclusive_container)
# Copying exclusive containers copies all their contents and an extra location for the tag.
recipe main [
  1:number <- copy 1:literal  # 'point' variant
  2:number <- copy 34:literal
  3:number <- copy 35:literal
  4:number-or-point <- copy 1:number-or-point/raw  # unsafe
]
+mem: storing 1 in location 4
+mem: storing 34 in location 5
+mem: storing 35 in location 6

:(before "End size_of(types) Cases")
if (t.kind == exclusive_container) {
  // size of an exclusive container is the size of its largest variant
  // (So like containers, it can't contain arrays.)
//?   cout << "--- " << types.at(0) << ' ' << t.size << '\n'; //? 1
//?   cout << "point: " << Type_number["point"] << " " << Type[Type_number["point"]].name << " " << Type[Type_number["point"]].size << '\n'; //? 1
//?   cout << t.name << ' ' << t.size << ' ' << SIZE(t.elements) << '\n'; //? 1
  long long int result = 0;
  for (long long int i = 0; i < t.size; ++i) {
    long long int tmp = size_of(t.elements.at(i));
//?     cout << i << ": " << t.elements.at(i).at(0) << ' ' << tmp << ' ' << result << '\n'; //? 1
    if (tmp > result) result = tmp;
  }
  // ...+1 for its tag.
  return result+1;
}

//:: To access variants of an exclusive container, use 'maybe-convert'.
//: It always returns an address (so that you can modify it) or null (to
//: signal that the conversion failed (because the container contains a
//: different variant).

//: 'maybe-convert' requires a literal in ingredient 1. We'll use a synonym
//: called 'variant'.
:(before "End Mu Types Initialization")
Type_number["variant"] = 0;

:(scenario maybe_convert)
recipe main [
  12:number <- copy 1:literal
  13:number <- copy 35:literal
  14:number <- copy 36:literal
  20:address:point <- maybe-convert 12:number-or-point/raw, 1:variant  # unsafe
]
+mem: storing 13 in location 20

:(scenario maybe_convert_fail)
recipe main [
  12:number <- copy 1:literal
  13:number <- copy 35:literal
  14:number <- copy 36:literal
  20:address:point <- maybe-convert 12:number-or-point/raw, 0:variant  # unsafe
]
+mem: storing 0 in location 20

:(before "End Primitive Recipe Declarations")
MAYBE_CONVERT,
:(before "End Primitive Recipe Numbers")
Recipe_number["maybe-convert"] = MAYBE_CONVERT;
:(before "End Primitive Recipe Implementations")
case MAYBE_CONVERT: {
  reagent base = canonize(current_instruction().ingredients.at(0));
  long long int base_address = base.value;
  type_number base_type = base.types.at(0);
  assert(Type[base_type].kind == exclusive_container);
  assert(isa_literal(current_instruction().ingredients.at(1)));
  long long int tag = current_instruction().ingredients.at(1).value;
  long long int result;
  if (tag == static_cast<long long int>(Memory[base_address])) {
    result = base_address+1;
  }
  else {
    result = 0;
  }
  products.resize(1);
  products.at(0).push_back(result);
  break;
}

//:: Allow exclusive containers to be defined in mu code.

:(scenario exclusive_container)
exclusive-container foo [
  x:number
  y:number
]
+parse: reading exclusive-container foo
+parse:   element name: x
+parse:   type: 1
+parse:   element name: y
+parse:   type: 1

:(before "End Command Handlers")
else if (command == "exclusive-container") {
  insert_container(command, exclusive_container, in);
}