about summary refs log blame commit diff stats
path: root/README.md
blob: b8b7442778e43f8191bd9b9ac8b45e5388196071 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
                 
 
                                                                                                                                          








                                                                                                                                                   
                                                                                                                                                                                                     





                                                                                                            
                                                                                                                              







                                                                                                                                     
                                                                                                                                                                                            

   
                                                                                                             


                                     
                                                                                                                



                                                                                             
                                                                                              
 
                                                                         
 
                                                                     
 
                                                                                                                                      









                                                                                                                                                                                                              



                                                                                                                                              






                   
# Numericx Readme

A command-line interface (CLI) program that converts a number (or text) from one numerical system into another different numerical system.

## Compiling

This program create an executable (or many) that will convert a number from one numerical system into another one.

For example, you may want to convert a hexadecimal number into a ternary number. You may use this program.

You first need to define the proprieties of the numerical systems. These proprieties are defined in the compilation process (as compilation flags).

For example, let's make the hexdecimal numerical system with numerals from `0-f`, with a infinite 0 and it start counting on 1. So, our hexadecimal system will need the following compilation flags:
```
-DFROM_NUMERICALS=\"0123456789abcdef\" -DFROM_FIRST_NUMBER_VOID -DFROM_INFINITE_BASE
```

if you want the units place of this numerical system to be on the right side, add `-DFROM_UNITS_ON_THE_END`.

Now, for the ternary system let's make with numerals from `1-3`, 1 is not infinite and it start counting on 1. Use this flags:
```
-DTO_NUMERICALS=\"123\"
```

if you want the units place of this numerical system to be on the right side, use `-DTO_UNITS_ON_THE_END`.

Now, to create the executable to translate a number for these two numerical system, join all of this together and compile using this:
```
$ cc -DFROM_NUMERICALS=\"0123456789abcdef\" -DFROM_FIRST_NUMBER_VOID -DFROM_INFINITE_BASE -DFROM_UNITS_ON_THE_END -DTO_NUMERICALS=\"123\" -DTO_UNITS_ON_THE_END numericx.c -o hex-to-ternary
```

This creates the `hex-to-ternary` executable with can be used to convert numbers from hexadecimal to ternary!

## Pre-defined executable compilation

There are many pre-defined compilation of executables on the Makefile. Take a look at the Makefile to know them.
Or you can run `make` to get all of the pre-defined executables. `make clean` to delete them.

## Compilation flags meanings

There are two categories for the compilation flags: the `FROM` category and the `TO` category.

`FROM` flags are the numerical system definitions of the argument number.

`TO` flags are the numerical system definitions of the number result.

Below, are the meanings of the `FROM` flags. The `TO` flags are the same as the `FROM` flags, with a switch of the `FROM_` to a `TO_`.

|Flag|Meaning|
|---|---|
|FROM_NUMERALS|Define the numerical system numerals of the 'from'|
|FROM_UNITS_ON_THE_END|Defines the units case to be on the end (on the right) for the 'from'|
|FROM_FIRST_NUMBER_VOID|Defines the first number as a "not counting" for the 'from'|
|FROM_INFINITE_BASE|Defines the first number to be infinite, for the 'from' (for example, if the first numeral is 0, then 0 == 00 == 000 == 0000 ... . Or if the first numeral is 1, then 1 == 11 == 111 ...)|

There is also the `DEBUG` flag, which will show all the steps that you number walked through. Remember: you use `-DDEBUG` as an compiler argument.

## Documentation

See [numericx documentation page](https://dacs-git.codeberg.page/numericx/). Look at `Files > File List` in the documentation menubar website.

## License

MIT

## Author

Daniel A. C. Santos
r Kartik K. Agaram <vc@akkartik.com> 2015-11-01 19:38:30 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2015-11-01 19:38:30 -0800 2341 - fill in gaps in generic containers' href='/akkartik/mu/commit/058generic_container.cc?h=main&id=a09697d0acadb7297415c49cb2abcc763eafb3f9'>a09697d0 ^
a9a23371 ^
5d4a1d3b ^
a09697d0 ^

5d4a1d3b ^
b954ac38 ^
5d4a1d3b ^

a9a23371 ^



a09697d0 ^
a9a23371 ^
a09697d0 ^

a9a23371 ^
a09697d0 ^
a9a23371 ^
a09697d0 ^


a9a23371 ^
5d4a1d3b ^





17b90929 ^
5d4a1d3b ^












17b90929 ^
a4f12265 ^










b954ac38 ^





a4f12265 ^


17b90929 ^
a4f12265 ^












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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

                                                       


                                                                             
                                                   
 
                                            






















                                                                              
                                                              



                                                  










                                                                             
                                                                        
                                                 
                                                                 
                                                           
                        




                                                                                                      
                                                               


   
                                              
                                    

                                                                        



                                         
                                                                                                   



                                                        
                                                                       
                                                                              

                                                                                                                           
                 



           
                                    


                                                                                                        
                      
                                     

                            
                        


                                        
                                                 
                                                                                              




                                            
 
                                           





                                 
                                       



                               





                                                                                                                                 


           
                                             










                                      
                                             









                                                      

                                          

                                                                                                                                          
                                                                    












                                                                                       
                                                                                       
                                                                          

                                                      
                                                                                   
                                                                                                                                      

             



                                                                                                       
   
                                                              

 
                                                                   
                 
                          


                                    
                                                           





                                        
                                                 












                                                                         
                                                   










                                                        





                                                                                                                           


           
                                                                           












                                 
//:: Container definitions can contain type parameters.
//:
//: Extremely hacky initial implementation. We still don't support the full
//: complexity of type trees inside container definitions. So for example you
//: can't have a container element with this type:
//:   (map (array address character) (list number))

:(scenario size_of_shape_shifting_container)
container foo:_t [
  x:_t
  y:number
]
recipe main [
  1:foo:number <- merge 12, 13
  3:foo:point <- merge 14, 15, 16
]
+mem: storing 12 in location 1
+mem: storing 13 in location 2
+mem: storing 14 in location 3
+mem: storing 15 in location 4
+mem: storing 16 in location 5

:(before "End Globals")
// We'll use large type ordinals to mean "the following type of the variable".
const int START_TYPE_INGREDIENTS = 2000;
:(before "End Test Run Initialization")
assert(Next_type_ordinal < START_TYPE_INGREDIENTS);

:(before "End type_info Fields")
map<string, type_ordinal> type_ingredient_names;

//: Suppress unknown type checks in shape-shifting containers.

:(before "Check Container Field Types(info)")
if (!info.type_ingredient_names.empty()) continue;

:(before "End container Name Refinements")
if (name.find(':') != string::npos) {
  trace(9999, "parse") << "container has type ingredients; parsing" << end();
  read_type_ingredients(name);
}

:(code)
void read_type_ingredients(string& name) {
  string save_name = name;
  istringstream in(save_name);
  name = slurp_until(in, ':');
  if (!contains_key(Type_ordinal, name) || get(Type_ordinal, name) == 0)
    put(Type_ordinal, name, Next_type_ordinal++);
  type_info& info = get_or_insert(Type, get(Type_ordinal, name));
  long long int next_type_ordinal = START_TYPE_INGREDIENTS;
  while (has_data(in)) {
    string curr = slurp_until(in, ':');
    if (info.type_ingredient_names.find(curr) != info.type_ingredient_names.end()) {
      raise_error << "can't repeat type ingredient names in a single container definition\n" << end();
      return;
    }
    put(info.type_ingredient_names, curr, next_type_ordinal++);
  }
}

:(before "End insert_container Special-cases")
// check for use of type ingredients
else if (!properties->value.empty() && properties->value.at(0) == '_') {
  result->value = get(info.type_ingredient_names, properties->value);
}

:(before "End Container Type Checks")
if (type->value >= START_TYPE_INGREDIENTS
    && (type->value - START_TYPE_INGREDIENTS) < SIZE(get(Type, type->value).type_ingredient_names))
  return;

:(before "End size_of(type) Container Cases")
if (t.elements.at(i)->value >= START_TYPE_INGREDIENTS) {
  trace(9999, "type") << "checking size of type ingredient\n" << end();
  long long int size = size_of_type_ingredient(t.elements.at(i), type->right);
  if (!size)
    raise_error << "illegal type '" << debug_string(type) << "' seems to be missing a type ingredient or three\n" << end();
  result += size;
  continue;
}

:(code)
// shape-shifting version of size_of
long long int size_of_type_ingredient(const type_tree* element_template, const type_tree* rest_of_use) {
  long long int type_ingredient_index = element_template->value - START_TYPE_INGREDIENTS;
  const type_tree* curr = rest_of_use;
  if (!curr) return 0;
  while (type_ingredient_index > 0) {
    --type_ingredient_index;
    curr = curr->right;
    if (!curr) return 0;
  }
  assert(curr);
  assert(!curr->left);  // unimplemented
  if (!contains_key(Type, curr->value)) return 0;
  trace(9999, "type") << "type deduced to be " << get(Type, curr->value).name << "$" << end();
  type_tree tmp(curr->value);
  if (curr->right)
    tmp.right = new type_tree(*curr->right);
  return size_of(&tmp);
}

:(scenario get_on_shape_shifting_container)
container foo:_t [
  x:_t
  y:number
]
recipe main [
  1:foo:point <- merge 14, 15, 16
  2:number <- get 1:foo:point, y:offset
]
+mem: storing 16 in location 2

:(before "End GET field Cases")
const type_tree* type = get(Type, base_type).elements.at(i);
if (type->value >= START_TYPE_INGREDIENTS) {
  long long int size = size_of_type_ingredient(type, base.type->right);
  if (!size)
    raise_error << "illegal field type '" << debug_string(type) << "' seems to be missing a type ingredient or three\n" << end();
  src += size;
  continue;
}

:(scenario get_on_shape_shifting_container_2)
container foo:_t [
  x:_t
  y:number
]
recipe main [
  1:foo:point <- merge 14, 15, 16
  2:point <- get 1:foo:point, x:offset
]
+mem: storing 14 in location 2
+mem: storing 15 in location 3

:(scenario get_on_shape_shifting_container_3)
container foo:_t [
  x:_t
  y:number
]
recipe main [
  1:foo:address:point <- merge 34, 48  # unsafe
  2:address:point <- get 1:foo:address:point, x:offset
]
+mem: storing 34 in location 2

:(before "End element_type Special-cases")
if (contains_type_ingredient(element)) {
  if (!canonized_base.type->right)
    raise_error << "illegal type '" << debug_string(canonized_base.type) << "' seems to be missing a type ingredient or three\n" << end();
  replace_type_ingredient(element.type, canonized_base.type->right);
}

:(code)
bool contains_type_ingredient(const reagent& x) {
  return contains_type_ingredient(x.type);
}

bool contains_type_ingredient(const type_tree* type) {
  if (!type) return false;
  if (type->value >= START_TYPE_INGREDIENTS) return true;
  return contains_type_ingredient(type->left) || contains_type_ingredient(type->right);
}

void replace_type_ingredient(type_tree* element_type, const type_tree* callsite_type) {
  if (!callsite_type) return;  // error but it's already been raised above
  if (!element_type) return;
  if (element_type->value >= START_TYPE_INGREDIENTS) {
    if (!has_nth_type(callsite_type, element_type->value-START_TYPE_INGREDIENTS)) {
      raise_error << "illegal type '" << debug_string(callsite_type) << "' seems to be missing a type ingredient or three\n" << end();
      return;
    }
    const type_tree* replacement = nth_type(callsite_type, element_type->value-START_TYPE_INGREDIENTS);
    element_type->value = replacement->value;
    element_type->left = replacement->left ? new type_tree(*replacement->left) : NULL;
    element_type->right = replacement->right ? new type_tree(*replacement->right) : NULL;
  }
  replace_type_ingredient(element_type->right, callsite_type);
}

const type_tree* nth_type(const type_tree* base, long long int n) {
  assert(n >= 0);
  if (n == 0) return base;
  return nth_type(base->right, n-1);
}

bool has_nth_type(const type_tree* base, long long int n) {
  assert(n >= 0);
  if (base == NULL) return false;
  if (n == 0) return true;
  return has_nth_type(base->right, n-1);
}

:(scenario get_on_shape_shifting_container_error)
% Hide_errors = true;
container foo:_t [
  x:_t
  y:number
]
recipe main [
  10:foo:point <- merge 14, 15, 16
  1:number <- get 10:foo, 1:offset
]
+error: illegal type 'foo' seems to be missing a type ingredient or three

//: get-address similarly

:(scenario get_address_on_shape_shifting_container)
container foo:_t [
  x:_t
  y:number
]
recipe main [
  10:foo:point <- merge 14, 15, 16
  1:address:number <- get-address 10:foo:point, 1:offset
]
+mem: storing 12 in location 1

:(before "End GET_ADDRESS field Cases")
const type_tree* type = get(Type, base_type).elements.at(i);
if (type->value >= START_TYPE_INGREDIENTS) {
  long long int size = size_of_type_ingredient(type, base.type->right);
  if (!size)
    raise_error << "illegal type '" << debug_string(type) << "' seems to be missing a type ingredient or three\n" << end();
  result += size;
  continue;
}

:(scenario get_on_shape_shifting_container_inside_shape_shifting_container)
container foo:_t [
  x:_t
  y:number
]
container bar [
  x:foo:point
  y:number
]
recipe main [
  1:bar <- merge 14, 15, 16, 17
  2:number <- get 1:bar, 1:offset
]
+mem: storing 17 in location 2