Position Independent Source Code

Artifact Content
Login
Wiki page [PISC in Y Minutes] by yumaikas 2017-07-02 15:04:35.

Artifact d709f19f2ed7b52fd460e52414552eeacf62a67e:


     1  D 2017-07-02T15:04:35.932
     2  L PISC\sin\sY\sMinutes
     3  P 389481ef3751446c6ca81018bd5d4dbd45b437a4
     4  U yumaikas
     5  W 6032
     6  <nowiki>
     7  
     8  <h2> PISC in Y minutes </h2>
     9      <pre class="prettyprint">
    10  # PISC has 2 types of comments: line comments like this one, and
    11  /*
    12  Block comments like this.
    13  */
    14  
    15  "Hello, world!" println
    16  # PISC is a stack based language, which means that PISC generally 
    17  # works rather like Reverse Polish Notation (RPN)
    18  # This means that the arguments for a command come before the command itself.
    19  # And that commands generally run backwards commpared to most languages
    20  
    21  # So if we have this stack, what can we do with it?
    22  1 2 swap    # Stack: 2 1
    23  dup         # Stack: 2 1 1 
    24  drop        # Stack: 2 1 
    25  2dup        # Stack: 2 1 2 1
    26  nip         # Stack 2 1 1 ( Get rid of the second ) 
    27  
    28  # And we'll clear the stack for further examples
    29  drop drop drop 
    30  
    31  # Basic math looks like this:
    32  1 2 + # Stack: 3
    33  # If you're running PISC on your local machine, you'll notice that after each 
    34  # line of input, it shows you the stack, and the types of the variables on the stack.
    35  
    36  drop        # We're done with that 3 for now.
    37  
    38  
    39  # PISC currently has two types of numbers: Integers and Double Precision floating point numbers.
    40  1 2.0 +     # 3 <Double>
    41  # And when a Double and int mix, the int is converted into a double
    42  3.2 *       # 9.600000000000001 <Double>
    43  drop
    44  
    45  
    46  # Booleans are also handled in an RPN fashion, true and false are represented by `t` and `f`
    47  t f and     # f <Boolean>
    48  t or        # t <Boolean> No surprises here
    49  
    50  # clear-stack will get rid of anything left on the stack
    51  clear-stack
    52  
    53  # In PISC, since all words take arguments off the stack, 
    54  # control flow tends to work in a bit of a backwards way
    55  
    56  # This is an if statement.
    57  # the bits with `[` and `]` are called quotations, and they are kinda special 
    58  # This puts a boolean value and 2 quotations on the stack, and the checks the 
    59  # the boolean value to decide which quotation to execute
    60  t [ "true" println ] [ "false" println ] if   
    61  
    62  # Note also that you can use the stack to send 
    63  # Information to another command
    64  # This will print "Different"
    65  2 1 = [ "The same" ] [ "Different" ] if println
    66  
    67  # They are how you pass code to words that can be executed at a later time
    68  # If you want to execute a quotation, you use the `call` word
    69  [ "This is a quotation" ] [ "But this is the one that will be called" ] call
    70  
    71  # Quotations live on the stack before they are used, so you'll seem them printed out
    72  # For example you could type the above example in this fashion:
    73  [ "This is a quotation" ] 
    74  [ "But this is the one that will be called" ] call
    75  # And if you do that, you'll see the stack get built up for each quotation. 
    76  clear-stack
    77  
    78  # Now that we have quoations a little explained we can cover vectors, which are PISC's array-like collections
    79  # The basic syntax for them is:
    80  { 1 2 3 }       # Stack: [1 2 3] <Vector>
    81  dup 0 vec-at    # stack: [1 2 3] <Vector>, 1 <Integer>
    82  
    83  # One thing to note about { } syntax for vectors is that the words inside the { } are treated as code
    84  # Such that the words are executed, and then anything they put on the stack is sliced into vector.
    85  { 10 [ 1 ] times }
    86  # Stack: [1 1 1 1 1 1 1 1 1 1] <Vector>
    87  
    88  
    89  # Those are some of the stack shufflers. If you want to build your own stack shufflers
    90  # run `"pick" help` to look at how my current set is built.
    91  
    92  # Though some stack-based languages make them hard to use, PISC has decently fully featured local variables
    93  # so that you don't have to track way too many stack entries in your head when dealing with larger code.
    94  # You use `:varname` to take 1 value from the stack and store it in a local variable.  
    95  "yumaikas" :name 3 :num
    96  # Is the same as:
    97  "yumaikas" 3 :num :name
    98  # Though I do not recommend using the second style
    99  
   100  # $varname places the value from varname onto the stack
   101  $name println     # Prints "yumaikas"
   102  
   103  { $name $num }  # Stack: [yumaikas 3] <Vector>
   104  clear-stack
   105  
   106  
   107  # There are roughly two ways to define words (what most langauges call functions) in PISC:
   108  # The most bang/buck is to use a `:` `;` combo
   109  # Words defined this way have the rough syntax of `: name ( invals -- outvals )  definition ;`
   110  # You need every part, though the stack effect comment (From `(` to `)`) isn't currently checked.
   111  : sum-vec ( vector -- result ) 0 swap [ + ] vec-each ;
   112  
   113  # Each time you define a word like this, it recieves its own locals frame.
   114  # (Which means that it has it's own set of local variables) 
   115  
   116  # This doesn't apply to the second way to define a word:
   117  [ 0 swap [ + ] vec-each ] :sum-vec
   118  # If a quotation is stored into a local variable, 
   119  # you can now use the name of the local variable as a word
   120  { 1 2 3 4 } sum-vec
   121  # Stack: 10 <Integer>
   122  
   123  # Loops
   124  # Right now PISC has 3 main types of loops
   125  # 1) Counted loops based on the `times` word
   126  0 :i
   127  4 [ $i print ++i ] times
   128  # This prints 
   129  # 0
   130  # 1 
   131  # 2
   132  # 3
   133  
   134  # 2) Condition based loops, using the `while` word
   135  [ $i 0 = not ] [ $i print --i ] while
   136  # Prints:
   137  # 4
   138  # 3
   139  # 2
   140  # 1 
   141  
   142  
   143  # 3) Each collection type, Vectors, Dictionaries and Strings currently several words that iterate over them
   144  { 1 2 3 4 } [ println ] vec-each 
   145  # Prints
   146  # 1
   147  # 2
   148  # 3
   149  # 4
   150  
   151  "abcd" [ println ] each-char 
   152  /* Prints 
   153  a
   154  b
   155  c
   156  d
   157  */
   158  
   159  # Dictionaries use strings for keys, and anything for values
   160  <dict> dup :funnyNumbers
   161      # value <<-key
   162      # <<- takes the value, stores it at "key" in the dict, and leaves the dict on the stack
   163      "42" <<-answertolife
   164      "1337" <<-leet 
   165      # <- works similarly to <<-, but doesn't leave the dict on the stack
   166      "DEADBEEF" <-debug
   167  
   168  # $dict ->key
   169  # takes the value at "key" from $dict, and puts it on the stack 
   170  $funnyNumbers ->leet println 
   171  # Prints 
   172  
   173  # $dict ->>key
   174  # Takes the value at key, puts it on the stack,
   175  # and leaves the dictionary at the top of the stack
   176  #
   177  # $dict ->key
   178  # Same as ->>, but does not leave the dictionary 
   179  # at the top of the stack.
   180  ${ $funnyNumbers ->>answertolife ->debug } println
   181  
   182  # Prints:
   183  # "42DEADBEEF"
   184  
   185  
   186  
   187  
   188  # TODO: More examples
   189  </pre>
   190  </nowiki>
   191  Z 9b4959c03f7a84fde78031c2e5b64c34