about summary refs log tree commit diff stats
path: root/edit/002-typing.mu
Commit message (Collapse)AuthorAgeFilesLines
* 4262 - literal 'null'Kartik Agaram2018-06-171-5/+5
|
* 4261 - start using literals for 'true' and 'false'Kartik Agaram2018-06-171-25/+25
| | | | | | | | | They uncovered one bug: in edit/003-shortcuts.mu <scroll-down> was returning 0 for an address in one place where I thought it was returning 0 for a boolean. Now we've eliminated this bad interaction between tangling and punning literals.
* 4258 - undo 4257Kartik Agaram2018-06-151-4/+0
|
* 4257 - abortive attempt at safe fat pointersKartik Agaram2018-06-151-0/+4
| | | | | | | | | | | | | | | | I've been working on this slowly over several weeks, but it's too hard to support 0 as the null value for addresses. I constantly have to add exceptions for scalar value corresponding to an address type (now occupying 2 locations). The final straw is the test for 'reload': x:num <- reload text 'reload' returns an address. But there's no way to know that for arbitrary instructions. New plan: let's put this off for a bit and first create support for literals. Then use 'null' instead of '0' for addresses everywhere. Then it'll be easy to just change what 'null' means.
* 4206 - edit/ app: consistent cursor positioningKartik K. Agaram2018-02-151-2/+0
|
* 4134 - 'input' = 'ingredient'Kartik K. Agaram2017-12-031-14/+14
|
* 3980Kartik K. Agaram2017-08-221-6/+6
|
* 3943Kartik K. Agaram2017-06-231-9/+0
| | | | Undo commit 3938 and almost everything after. Let's do this right.
* 3942Kartik K. Agaram2017-06-231-9/+9
| | | | | | | | No, my conclusion in the previous commit was wrong. When you print a character on the right margin, the cursor coordinates always wrap around to the left margin on the next row. It's just that if you're at the bottom of the screen, scrolling gives the impression that the row didn't change.
* 3941Kartik K. Agaram2017-06-231-0/+9
| | | | | | | | | | | | | | | | | Even though the bug of commit 3938 is now fixed, I'm still trying to track down why the failure looked different on the fake screen than on the real one. Snapshot as I try to track down the difference. One key lesson is that the approach of commit 3860 -- updating the cursor before rather than after printing each character -- turns out to be untenable. A sequence of `print` followed by `cursor-position` needs to behave the same as the real screen. But it's still not clear how the real screen. When you get to the end of a line the cursor position wraps after print to the left margin (column 0) on the next row. When you get to the bottom right the cursor position wraps to the *bottom left* margin. How the heck does it know to scroll on the next print, then? Is there some hidden state in the terminal?
* 3928Kartik K. Agaram2017-06-191-4/+5
|
* 3926Kartik K. Agaram2017-06-191-2/+57
| | | | | | Bugfix: when you hit `enter`, the cursor-row does not increment in *one* special situation: when the line wraps and the cursor is right at the start of one of the wrapped lines.
* 3924Kartik K. Agaram2017-06-181-1/+1
|
* 3919Kartik K. Agaram2017-06-161-1/+1
| | | | Bugfix: up-arrow in combination with wrapped lines.
* 3904Kartik K. Agaram2017-06-091-4/+4
| | | | Standardize functions to put the main object being modified first.
* 3861 - screen untouched when entering console modeKartik K. Agaram2017-05-181-0/+1
|
* 3860 - stop buffering the screen in termboxKartik K. Agaram2017-05-181-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | To achieve this we have to switch to a model of the screen in termbox that is closer to the underlying terminal. Before: a screen is a grid of characters writing out of bounds does nothing After: a screen is a scrolling raster of characters writing out of bounds wraps to next line and scrolls if necessary To move to the new model, it was essential that I migrate my fake screen at the same time to mimic it. This is why the first attempt (commit 3824) failed (commit 3858). This is also why this commit can't be split into smaller pieces. The fake screen now 'scrolls' by rotating screen lines from top to bottom. There's still no notion of a scrollback buffer. The newer model is richer; it permits repl-like apps that upstream termbox can't do easily. It also permits us to simply use `printf` or `cout` to write to the screen, and everything mostly works as you would expect. Exceptions: a) '\n' won't do what you expect. You need to explicitly print both '\n' and '\r'. b) backspace won't do what you expect. It only moves the cursor back, without erasing the previous character. It does not wrap. Both behaviors exactly mimic my existing terminal's emulation of vt100. The catch: it's easy to accidentally scroll in apps. Out-of-bounds prints didn't matter before, but they're bugs now. To help track them down, use the `save-top-idx`, `assert-no-scroll` pair of helpers. An important trick is to wrap the cursor before rather after printing a character. Otherwise we end up scrolling every time we print to the bottom-right character. This means that the cursor position can be invalid at the start of a print, and we need to handle that. In the process we also lose the ability to hide and show the screen. We have to show the prints happening. Seems apt for a "white-box" platform like Mu.
* 3859Kartik K. Agaram2017-05-131-0/+1
|
* 3854Kartik K. Agaram2017-05-131-7/+0
| | | | Revert commits 3824, 3850 and 3852. We'll redo them more carefully.
* 3850Kartik K. Agaram2017-05-101-0/+7
| | | | | Bugfix: writes out of bounds used to be skipped, but started clobbering the screen on commit 3824.
* 3700Kartik K. Agaram2016-11-281-30/+18
| | | | | | Reorder products of some functions in the edit/ and sandbox/ apps. My recent realization: always return 'real' products before ones that just indicate an ingredient is mutable.
* 3696Kartik K. Agaram2016-11-271-30/+33
| | | | | | Decouple editor initialization from rendering to screen. This hugely simplifies the header of 'new-editor' and makes clear that it was only using the screen for rendering.
* 3565Kartik K. Agaram2016-10-231-1/+1
| | | | | | Cleaning up the console interfaces before we start changing the socket interfaces to look like them. Reading from sockets need to be non-blocking just like reading from the console.
* 3552Kartik K. Agaram2016-10-221-3/+3
| | | | | | | | | | | | | | | | | | Stop requiring jump instructions to explicitly provide a ':label' type for jump targets. This has been a source of repeated confusion for my students: a) They'd add the ':label' to the label definition rather than the jump target (label use) b) They'd spend time thinking about whether the initial '+' prefix was part of the label name. In the process I cleaned up a couple of things: - the space of names is more cleanly partitioned into labels and non-labels (clarifying that '_' and '-' are non-label prefixes) - you can't use label names as regular variables anymore - you can infer the type of a label just from its name
* 3489Kartik K. Agaram2016-10-081-20/+20
| | | | | | | | | | Revert commit 3457, where I switched the unicode characters used in the edit/ app to something that doesn't render double-wide in html. It turns out that the new unicode characters made iTerm2 sluggish in alt-tabbing between windows. (Commit 3488 only fixed the screen-clearing issue.) I haven't reverted the html files. I'm going to redo commit 3457 next so the html files continue to render like they do now.
* 3457Kartik K. Agaram2016-10-061-20/+20
| | | | | Switch around some unicode characters in the edit/ app so that it renders more cleanly in html (with monospace fonts).
* 3445Kartik K. Agaram2016-10-061-51/+51
| | | | | | | Ugly that we didn't need 'screen' to provide a type in scenarios (because assume-screen expands to a definition of 'screen') but we did need a type for 'console'. Just never require types for special names in scenarios.
* 3429 - standardize Mu scenariosKartik K. Agaram2016-09-281-114/+122
| | | | | | | | | | | | | A long-standing problem has been that I couldn't spread code across 'run' blocks because they were separate scopes, so I've ended up making them effectively comments. Running code inside a 'run' block is identical in every way to simply running the code directly. The 'run' block is merely a visual aid to separate setup from the component under test. In the process I've also standardized all Mu scenarios to always run in a local scope, and only use (raw) numeric addresses for values they want to check later.
* 3428Kartik K. Agaram2016-09-281-2/+2
|
* 3396Kartik K. Agaram2016-09-171-98/+98
|
* 3391 - type abbreviations everywhereKartik K. Agaram2016-09-171-208/+208
| | | | | | | | | Well, almost. I can't use them in some places in C++ where I'm just creating a temporary reagent without passing it through transforms. Like in some unit tests. I can't use them in memory-should-contain. And there's one remaining bug: I can't use abbreviations in a couple of places in 075channel.mu.
* 3341Kartik K. Agaram2016-09-121-1/+1
| | | | | | | Process type abbreviations in function headers. Still a couple of places where doing this causes strange errors. We'll track those down next.
* 3337 - first use of type abbreviations: textKartik K. Agaram2016-09-121-49/+49
| | | | | In the process I've uncover a couple of situations we don't support type abbreviations yet. They're next.
* 3038 - track down a long-standing bugKartik K. Agaram2016-06-081-25/+56
| | | | | | | | | | | | | | | | | | | | | | | | | | | In some rare situations the editor would join a line with the next when it should simply wrap to the next screen row. Thanks Caleb and Ella Couch for finally running into a situation that was easy to reproduce. The scenario diffs are misleading on this commit. I had to: a) delete the obsolete 'editor-wraps-cursor-after-inserting-characters' because it was written back when a line just large enough to fit in a single line would not wrap: | | <-- screen boundary abcde These days it will wrap after making room for the wrap indicator: | | <-- screen boundary abcd↩ e b) rename editor-wraps-cursor-after-inserting-characters-2 to editor-wraps-cursor-after-inserting-characters-in-middle-of-line c) create a new scenario demonstrating the bug: editor-wraps-cursor-after-inserting-characters-at-end-of-line
* 2975Kartik K. Agaram2016-05-191-1/+1
| | | | Clean up this helper before we start redoing sandbox menubars.
* 2864 - replace all address:shared with just addressKartik K. Agaram2016-04-241-136/+136
| | | | | | | Now that we no longer have non-shared addresses, we can just always track refcounts for all addresses. Phew!
* 2861 - 'maybe-convert' no longer returns addressKartik K. Agaram2016-04-231-13/+13
|
* 2853 - purge get-address from edit/ appKartik K. Agaram2016-04-221-55/+66
| | | | Phew!
* 2735 - define recipes using 'def'Kartik K. Agaram2016-03-081-31/+31
| | | | | | | | | | | | I'm dropping all mention of 'recipe' terminology from the Readme. That way I hope to avoid further bike-shedding discussions while I very slowly decide on the right terminology with my students. I could be smarter in my error messages and use 'recipe' when code uses it and 'function' otherwise. But what about other words like ingredient? It would all add complexity that I'm not yet sure is worthwhile. But I do want separate experiences for veteran programmers reading about Mu on github and for people learning programming using Mu.
* 2576 - distinguish allocated addresses from othersKartik K. Agaram2016-01-191-136/+136
| | | | | | | | | | | | | | | | This is the one major refinement on the C programming model I'm planning to introduce in mu. Instead of Rust's menagerie of pointer types and static checking, I want to introduce just one new type, and use it to perform ref-counting at runtime. So far all we're doing is updating new's interface. The actual ref-counting implementation is next. One implication: I might sometimes need duplicate implementations for a recipe with allocated vs vanilla addresses of the same type. So far it seems I can get away with just always passing in allocated addresses; the situations when you want to pass an unallocated address to a recipe should be few and far between.
* layers 1 and 2 of edit/ now workingKartik K. Agaram2015-12-151-4/+4
|
* 2468 - overload print-character as just 'print'Kartik K. Agaram2015-11-211-3/+3
|
* 2446 - drop '-duplex' namespacing in recipesKartik K. Agaram2015-11-151-14/+14
| | | | Great that it just worked after the previous commit.
* 2417 - support mutable ingredients in headersKartik K. Agaram2015-11-101-1/+1
| | | | | | If a name repeats between ingredients, we raise an error. If a name repeats across ingredients and products, every call should share the same name across the corresponding ingredients and products.
* 2370 - layers 1-4 of edit are backKartik K. Agaram2015-11-051-69/+58
| | | | | | | | | | | | One nice consequence of all my deduction of reply ingredients is that I can insert the same fragment into recipes with different headers, and everything works as long as reply instructions are implicitly deduced. One thing I had to fix to make this work was to move reply-deduction out of rewrite rules and turn it into a first-class transform, so that it happens after tangling. I'm glad to see the back of that hack inside <scroll-down>.
* 2309Kartik K. Agaram2015-10-281-63/+63
|
* 2173 - 'main' for 'mu edit' running layers 1 and 2Kartik K. Agaram2015-09-061-1/+42
| | | | | | Layer 2 provides an almost fully functioning interactive editor: $ ./mu edit/00[12]* -- abcdef
* 2156 - split edit.mu into multiple filesKartik K. Agaram2015-09-051-0/+1012
Now you can bring up the programming environment by saying: $ mu edit The files under edit aren't yet *layers*, though, they have a few dependencies that we need to clean up.
rtik K. Agaram <vc@akkartik.com> 2015-02-21 11:20:55 -0800 806 - start parsing reagent metadata' href='/akkartik/mu/commit/cpp/010vm?h=main&id=0060093e9ed53a32570fab1c18da8074926520d1'>0060093e ^
dcf7436e ^







0060093e ^
dcf7436e ^





979403c6 ^





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                
                                               






                                                   
                                                                            

                
              
                            
                      

  




                                                                                              
                                                    


                                    

                                                  






                                                    



                                                                             
                
                                                    
              
            
                   
                            
                    
            
                                                          










                                                                          

                     

                
                                                                          











                                                                             



                                     
                            
                       
                             
                                                            
                         
                                 

                                                            
                                 

                                                            
                                 
                                 


                     


                                                                                
                                                                                



                                                                            
                  
              
              

                 
                                                              
                                                     
                                                   
                          
                                                            

  


                                                                 






                                                                              
                         
                               

                                     
                              
                               
                                     
                       
                                  
 


                     

 

           
       
                                                         

                                                                                                                     
 
                                                                              
                                                            
                        

                       








                                                                        

                                                              
     
   
                                                     

                               
                                                                       



                                               
                              
                               
                                                      



                                                                  
         

                                                 


               







                                             
                       





                   





                                                                                      
:(after "Types")
// A program is a book of 'recipes' (functions)
typedef int recipe_number;
:(before "End Globals")
unordered_map<string, recipe_number> Recipe_number;
unordered_map<recipe_number, recipe> Recipe;
int Next_recipe_number = 1;

:(before "End Types")
// Recipes are lists of instructions. To run a recipe, the computer runs its
// instructions.
struct recipe {
  string name;
  vector<instruction> steps;
  // End Recipe Fields
};

:(before "struct recipe")
// Each instruction is either of the form:
//   product1, product2, product3, ... <- operation ingredient1, ingredient2, ingredient3, ...
// or just a single 'label' followed by a colon
//   label:
// Labels don't do anything, they're just waypoints.
struct instruction {
  bool is_label;
  string label;  // only if is_label
  string name;  // only if !is_label
  recipe_number operation;  // Recipe_number[name]
  vector<reagent> ingredients;  // only if !is_label
  vector<reagent> products;  // only if !is_label
  instruction();
  void clear();
};

:(before "struct instruction")
// Ingredients and products are a single species -- a reagent. Reagents refer
// either to numbers or to locations in memory along with 'type' tags telling
// us how to interpret them. They also can contain arbitrary other lists of
// properties besides types, but we're getting ahead of ourselves.
struct reagent {
  vector<pair<string, vector<string> > > properties;
  string name;
  int value;
  bool initialized;
  vector<type_number> types;
  reagent(string s);
  reagent();
  void set_value(int v) { value = v; initialized = true; }
  string to_string();
};

:(before "struct reagent")
struct property {
  vector<string> values;
};

:(before "End Globals")
// Locations refer to a common 'memory'. Each location can store a number.
unordered_map<int, int> Memory;
:(before "End Setup")
  Memory.clear();

:(after "Types")
// Mu types encode how the numbers stored in different parts of memory are
// interpreted. A location tagged as a 'character' type will interpret the
// number 97 as the letter 'a', while a different location of type 'integer'
// would not.
//
// Unlike most computers today, mu stores types in a single big table, shared
// by all the mu programs on the computer. This is useful in providing a
// seamless experience to help understand arbitrary mu programs.
typedef int type_number;
:(before "End Globals")
unordered_map<string, type_number> Type_number;
unordered_map<type_number, type_info> Type;
int Next_type_number = 1;
:(code)
void setup_types() {
  Type.clear();  Type_number.clear();
  Type_number["literal"] = 0;
  Type_number["offset"] = 0;
  Next_type_number = 1;
  // Mu Types Initialization.
  int integer = Type_number["integer"] = Next_type_number++;
  Type[integer].size = 1;
  Type[integer].name = "integer";
  int address = Type_number["address"] = Next_type_number++;
  Type[address].size = 1;
  Type[address].name = "address";
  int boolean = Type_number["boolean"] = Next_type_number++;
  Type[boolean].size = 1;
  Type[boolean].name = "boolean";
  // End Mu Types Initialization.
}
:(before "End Setup")
  setup_types();

:(before "End Types")
// You can construct arbitrary new types. Types are either 'records', containing
// 'fields' of other types, or 'array's of a single type repeated over and over.
//
// For example:
//  storing bank balance next to a person's name might require a record, and
//  high scores in a game might need an array of numbers.
struct type_info {
  string name;
  size_t size;
  bool is_record;
  bool is_array;
  vector<vector<type_number> > elements;  // only if is_record
  vector<string> element_names;  // only if is_record
  vector<type_number> element;  // only if is_array
  // End type_info Fields.
  type_info() :size(0), is_record(false), is_array(false) {}
};

:(before "End Globals")
const int IDLE = 0;  // always the first entry in the recipe book
const int COPY = 1;
:(code)
// It's all very well to construct recipes out of other recipes, but we need
// to know how to do *something* out of the box. For the following
// recipes there are only codes, no entries in the book, because mu just knows
// what to do for them.
void setup_recipes() {
  Recipe.clear();  Recipe_number.clear();
  Next_recipe_number = 0;
  Recipe_number["idle"] = IDLE;
  assert(Next_recipe_number == IDLE);
  Next_recipe_number++;
  // Primitive Recipe Numbers.
  Recipe_number["copy"] = COPY;
  assert(Next_recipe_number == COPY);
  Next_recipe_number++;
  // End Primitive Recipe Numbers.
}
:(before "End Setup")
  setup_recipes();



//: Helpers

:(code)
// indent members to avoid generating prototypes for them
  instruction::instruction() :is_label(false), operation(IDLE) {}
  void instruction::clear() { is_label=false; label.clear(); operation=IDLE; ingredients.clear(); products.clear(); }

  // Reagents have the form <name>:<type>:<type>:.../<property>/<property>/...
  reagent::reagent(string s) :value(0), initialized(false) {
    istringstream in(s);
    // properties
    while (!in.eof()) {
      istringstream row(slurp_until(in, '/'));
      string name = slurp_until(row, ':');
      vector<string> values;
      while (!row.eof())
        values.push_back(slurp_until(row, ':'));
      properties.push_back(pair<string, vector<string> >(name, values));
    }
    // structures for the first row of properties
    name = properties[0].first;
    for (size_t i = 0; i < properties[0].second.size(); ++i) {
      types.push_back(Type_number[properties[0].second[i]]);
    }
  }
  reagent::reagent() :value(0), initialized(false) {}
  string reagent::to_string() {
    ostringstream out;
    out << "{name: \"" << name << "\", value: " << value << ", type: ";
    for (size_t i = 0; i < types.size(); ++i) {
      out << types[i];
      if (i < types.size()-1) out << "-";
    }
    if (!properties.empty()) {
      out << ", properties: [";
      for (size_t i = 0; i < properties.size(); ++i) {
        out << properties[i].first << ": ";
        for (size_t j = 0; j < properties[i].second.size(); ++j) {
          out << properties[i].second[j];
          if (j < properties[i].second.size()-1) out << ":";
        }
        if (i < properties.size()-1) out << ", ";
        else out << "]";
      }
    }
    out << "}";
    return out.str();
  }

string slurp_until(istream& in, char delim) {
  ostringstream out;
  char c;
  while (in >> c) {
    if (c == delim) {
      // drop the delim
      break;
    }
    out << c;
  }
  return out.str();
}

void dump_memory() {
  for (unordered_map<int, int>::iterator p = Memory.begin(); p != Memory.end(); ++p) {
    cout << p->first << ": " << p->second << '\n';
  }
}