-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.mar
559 lines (522 loc) · 19.3 KB
/
calc.mar
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
import stdlib.mar
| A unit consists of multiple dimensions with exponents. For example, m²/s has
| the exponents m = 2, s = -1.
struct Unit { exponents: Map[String, Int] }
struct Amount { src: Range[Int], number: Float, unit: Unit }
enum Term {
amount: Amount,
add: Operands,
subtract: Operands,
multiply: Operands,
divide: Operands,
}
struct Operands { left: &Term, right: &Term }
fun no_unit(): Unit { Unit { exponents = map[String, Int]() } }
fun unit(string: String): Unit { Unit { exponents = map(string -> 1) } }
fun write_debug[W](writer: W, unit: Unit) { writer."{unit}" }
fun write_debug[W](writer: W, term: Term) { writer."{term}" }
fun is_none(unit: Unit): Bool { unit.exponents.is_empty() }
fun ==(a: Unit, b: Unit): Bool {
a.exponents.size == b.exponents.size or return false
for entry in a.exponents do
{b.exponents.get_maybe(entry.key) or return false} == entry.value or return false
true
}
fun *(a: Unit, b: Unit): Unit {
var exponents = map[String, Int]()
for entry in a.exponents do exponents.&.get_ref_or_put_default(entry.key, 0) += entry.value
for entry in b.exponents do exponents.&.get_ref_or_put_default(entry.key, 0) += entry.value
var final = map[String, Int]()
for entry in exponents do if entry.value != 0 then final.&.put(entry.key, entry.value)
Unit { exponents = final }
}
fun inverse(unit: Unit): Unit {
var exponents = map[String, Int]()
for entry in unit.exponents do exponents.&.put(entry.key, 0 - entry.value)
Unit { exponents }
}
fun /(a: Unit, b: Unit): Unit { a * b.inverse() }
fun ops(left: Term, right: Term): Operands {
Operands { left = left.put_on_heap(), right = right.put_on_heap() }
}
| Parsing
struct Parser { line: String, cursor: Int }
var null_char = 0.lower_byte().to_char()
fun parser(line: String): Parser {
Parser { line = "{line}", cursor = 0 }
}
fun generate(static: Static[Parser], random: &Random, complexity: Int): Parser {
Parser { line = static[String]().generate(random, complexity), cursor = 0 }
}
fun current(parser: Parser): Char { parser.line.get(parser.cursor) }
fun rest(parser: Parser): String {
parser.line.substr(parser.cursor..parser.line.len)
}
fun advance(parser: &Parser) { parser.cursor = parser.cursor + 1 }
fun advance_by(parser: &Parser, n: Int) { parser.cursor = parser.cursor + n }
fun consume(parser: &Parser, prefix: String): Bool {
parser.consume_whitespace()
var start = parser.cursor
parser.rest().starts_with(prefix) or return false
parser.advance_by(prefix.len)
var end = parser.cursor
true
}
fun bad_input[T](error: String): Result[Maybe[T], String] {
error[Maybe[T], String](error)
}
fun no_match[T](): Result[Maybe[T], String] {
ok[Maybe[T], String](none[T]())
}
fun parsed[T](val: T): Result[Maybe[T], String] {
ok[Maybe[T], String](some(val))
}
fun consume_whitespace(parser: &Parser) {
loop {
var char = parser.current()
if char.is_whitespace() then {
parser.cursor = parser.cursor + 1
}
else break
}
}
fun parse_digits(parser: &Parser): Result[Maybe[Int], String] {
var start = parser.cursor
var num = 0
loop {
var char = parser.current()
{#0..=#9}.contains(char) or break
num = num * 10 + {char - #0}.to_int()
parser.advance()
}
if parser.cursor == start then return no_match[Int]()
parsed(num)
}
fun parse_float(parser: &Parser): Result[Maybe[Float], String] {
parser.consume_whitespace()
var sign = 1 | if parser.consume("-") then -1 else 1
var integer_part = parser.parse_digits()? or
return
if sign == 1
then no_match[Float]()
else bad_input[Float]("Expected number after the minus sign.")
var integer_part = integer_part.to_float()
var number =
if parser.consume(".") then {
var start_of_decimals = parser.cursor
var decimals = parser.parse_digits()? or 0
var num_decimals = parser.cursor - start_of_decimals
if num_decimals > 18 then return bad_input[Float]("Too many decimals.")
var decimal_part = decimals.to_float() / {10 ** num_decimals}.to_float()
sign.to_float() * {integer_part + decimal_part}
} else
sign.to_float() * integer_part
parsed(number)
}
fun parse_unit(parser: &Parser): Maybe[String] {
parser.consume_whitespace()
var start = parser.cursor
loop {
var char = parser.current()
if char.is_letter() then parser.advance() else break
}
var end = parser.cursor
if start == end then return none[String]()
some(parser.line.substr(start..end))
}
enum Precedence { atom, neighbors, parenthesize, multiply_divide, add_subtract }
fun parse_term(parser: &Parser, precedence: Precedence): Result[Maybe[Term], String] {
switch precedence
case atom {
parser.consume_whitespace()
var start = parser.cursor
if parser.parse_float()? is some(number) then {
var end = parser.cursor
return parsed(Term.amount(Amount { src = start..end, number, unit = no_unit() }))
}
if parser.parse_unit() is some(unit) then {
var end = parser.cursor
return parsed(Term.amount(Amount { src = start..end, number = 1.0, unit = unit(unit) }))
}
return no_match[Term]()
}
case neighbors {
var term = parser.parse_term(Precedence.atom)? or return no_match[Term]()
loop
term = Term.multiply(ops(term, parser.parse_term(Precedence.atom)? or break))
parsed(term)
}
case parenthesize {
if parser.consume("(") then {
var term = parser.parse_term(Precedence.add_subtract)? or return bad_input[Term]("Expected parenthesized term.")
parser.consume(")") or return bad_input[Term]("Expected closing parenthesis.")
parsed(term)
} else
parser.parse_term(Precedence.neighbors)
}
case multiply_divide {
var term = parser.parse_term(Precedence.parenthesize)?
or return no_match[Term]()
loop {
if parser.consume("*") then
term = Term.multiply(ops(term, parser.parse_term(Precedence.parenthesize)? or return bad_input[Term]("Expected right factor.")))
else if parser.consume("/") then
term = Term.divide(ops(term, parser.parse_term(Precedence.parenthesize)? or return bad_input[Term]("Expected divisor.")))
else break
}
parsed(term)
}
case add_subtract {
var term = parser.parse_term(Precedence.multiply_divide)?
or return no_match[Term]()
loop {
if parser.consume("+") then
term = Term.add(ops(term, parser.parse_term(Precedence.multiply_divide)? or return bad_input[Term]("Expected right addend.")))
else if parser.consume("-") then
term = Term.subtract(ops(term, parser.parse_term(Precedence.multiply_divide)? or return bad_input[Term]("Expected subtrahend.")))
else break
}
parsed(term)
}
}
struct ParseError { cursor: Int, msg: String }
enum Line {
term: Term,
definition: Tuple2[Term, Term],
quit,
}
fun parse_line(line: String): Result[Line, ParseError] {
if line == "quit" then return ok[Line, ParseError](Line.quit)
var parser = parser(line)
var term =
switch parser.&.parse_term(Precedence.add_subtract)
case error(msg) return error[Line, ParseError](ParseError { cursor = parser.cursor, msg })
case ok(term) term or return error[Line, ParseError](ParseError { cursor = parser.cursor, msg = "Expected term." })
parser.&.consume_whitespace()
if parser.current() == null_char then
return ok[Line, ParseError](Line.term(term))
parser.&.consume("=") or return error[Line, ParseError](ParseError { cursor = parser.cursor, msg = "Weird stuff." })
var second_term =
switch parser.&.parse_term(Precedence.add_subtract)
case error(msg) return error[Line, ParseError](ParseError { cursor = parser.cursor, msg })
case ok(term) term or return error[Line, ParseError](ParseError { cursor = parser.cursor, msg = "Expected term." })
if parser.current() != null_char then return error[Line, ParseError](ParseError { cursor = parser.cursor, msg = "Weird stuff." })
ok[Line, ParseError](Line.definition(tuple(term, second_term)))
}
fun write[W](writer: W, unit: Unit) {
if unit.is_none() then return {}
var positive = list[MapEntry[String, Int]]()
var negative = list[MapEntry[String, Int]]()
for entry in unit.exponents do
{if entry.value > 0 then positive.& else negative.&}.push(entry)
{ | Positive part
if positive.is_empty() then writer."1"
if positive.len > 1 then writer."("
var first = true
for entry in positive do {
if first then first = false else writer." "
writer."{entry.key}"
if entry.value != 1 then writer."^{entry.value}"
}
if positive.len > 1 then writer.")"
}
{ | Negative part
if negative.is_not_empty() then writer."/"
if negative.len > 1 then writer."("
var first = true
for entry in negative do {
writer."{entry.key}"
if entry.value != -1 then writer."^{0 - entry.value}"
}
if negative.len > 1 then writer.")"
}
| writer." ({unit.exponents.debug()})"
}
fun write[W](writer: W, amount: Amount) {
writer."{amount.number}{if amount.unit.is_none() then "" else " {amount.unit}"}"
}
fun as_tree[T](value: T, indent: Int): AsTree[T] { AsTree { value, indent } }
struct AsTree[T] { value: T, indent: Int }
fun write[W](writer: W, term: AsTree[Term]) {
var indent = term.indent
for i in 0..indent do writer." "
switch term.value
case amount(amount) writer."{amount}"
case add(ops) writer."+\n{ops.left.*.as_tree(indent + 1)}\n{ops.right.*.as_tree(indent + 1)}"
case subtract(ops) writer."-\n{ops.left.*.as_tree(indent + 1)}\n{ops.right.*.as_tree(indent + 1)}"
case multiply(ops) writer."*\n{ops.left.*.as_tree(indent + 1)}\n{ops.right.*.as_tree(indent + 1)}"
case divide(ops) writer."/\n{ops.left.*.as_tree(indent + 1)}\n{ops.right.*.as_tree(indent + 1)}"
}
fun write[W](writer: W, term: Term) {
switch term
case amount(amount) writer."amount"
case add(ops) writer."{ops.left} + {ops.right}"
case subtract(ops) writer."{ops.left} - {{
var parens =
switch ops.right.*
case amount false case add true case subtract true case multiply false case divide false
if parens then "({ops.right})" else "{ops.right}"
}}"
case multiply(ops) writer."{{
var parens =
switch ops.left.*
case amount false case add true case subtract true case multiply false case divide false
if parens then "({ops.left})" else "{ops.left}"
}} * {{
var parens =
switch ops.right.*
case amount false case add true case subtract true case multiply false case divide false
if parens then "({ops.right})" else "{ops.right}"
}}"
case divide(ops) writer."{{
var parens =
switch ops.left.*
case amount false case add true case subtract true case multiply false case divide false
if parens then "({ops.left})" else "{ops.left}"
}} / {{
var parens =
switch ops.right.*
case amount false case add true case subtract true case multiply true case divide true
if parens then "({ops.right})" else "{ops.right}"
}}"
}
| Calculating
fun simplify_leaves(term: Term): Result[Term, String] {
ok[Term, String](
switch term
case amount term
case add(ops) {
if ops.left.* is amount(a) then if ops.right.* is amount(b) then {
a.unit == b.unit or return error[Term, String]("Not the same unit: {a.unit} and {b.unit}")
return ok[Term, String](Term.amount(Amount {
src = a.src.start..b.src.end, number = a.number + b.number, unit = a.unit
}))
}
Term.add(ops(ops.left.simplify_leaves()?, ops.right.simplify_leaves()?))
}
case subtract(ops) {
if ops.left.* is amount(a) then if ops.right.* is amount(b) then {
a.unit == b.unit or return error[Term, String]("Not the same unit: {a.unit} and {b.unit}")
return ok[Term, String](Term.amount(Amount {
src = a.src.start..b.src.end, number = a.number - b.number, unit = a.unit
}))
}
Term.subtract(ops(ops.left.simplify_leaves()?, ops.right.simplify_leaves()?))
}
case multiply(ops) {
if ops.left.* is amount(a) then if ops.right.* is amount(b) then {
return ok[Term, String](Term.amount(Amount {
src = a.src.start..b.src.end, number = a.number * b.number, unit = a.unit * b.unit
}))
}
Term.multiply(ops(ops.left.simplify_leaves()?, ops.right.simplify_leaves()?))
}
case divide(ops) {
if ops.left.* is amount(a) then if ops.right.* is amount(b) then {
if b.number == 0.0 then return error[Term, String]("This is zero.")
return ok[Term, String](Term.amount(Amount {
src = a.src.start..b.src.end, number = a.number / b.number, unit = a.unit / b.unit
}))
}
Term.divide(ops(ops.left.simplify_leaves()?, ops.right.simplify_leaves()?))
}
)
}
struct EvalError { inputs: List[Amount], msg: String }
fun eval(term: Term, dict: Dictionary): Result[Amount, EvalError] {
ok[Amount, EvalError](
switch term
case amount(amount) amount
case add(ops) {
var a = ops.left.eval(dict)?
var b = ops.right.eval(dict)?
if a.unit != b.unit then {
a = dict.canonicalize(a)
b = dict.canonicalize(b)
}
a.unit == b.unit or return error[Amount, EvalError](EvalError {
inputs = list(a, b),
msg = "Plus operands {a} and {b} don't have the same unit."
})
Amount {
src = a.src.start..b.src.end, number = a.number + b.number, unit = a.unit
}
}
case subtract(ops) {
var a = ops.left.eval(dict)?
var b = ops.right.eval(dict)?
if a.unit != b.unit then {
a = dict.canonicalize(a)
b = dict.canonicalize(b)
}
a.unit == b.unit or return error[Amount, EvalError](EvalError {
inputs = list(a, b),
msg = "Minus operands {a} and {b} don't have the same unit."
})
Amount {
src = a.src.start..b.src.end, number = a.number - b.number, unit = a.unit
}
}
case multiply(ops) {
var a = ops.left.eval(dict)?
var b = ops.right.eval(dict)?
Amount {
src = a.src.start..b.src.end, number = a.number * b.number, unit = a.unit * b.unit
}
}
case divide(ops) {
var a = ops.left.eval(dict)?
var b = ops.right.eval(dict)?
if b.number == 0.0 then return error[Amount, EvalError](EvalError {
inputs = list(b),
msg = "Dividend {b} is zero."
})
Amount {
src = a.src.start..b.src.end, number = a.number / b.number, unit = a.unit / b.unit
}
}
)
}
struct Dictionary {
defs: Map[String, Amount],
}
fun define(dict: &Dictionary, left: Amount, right: Amount): Result[Nothing, String] {
left.number != 0.0 or return error[Nothing, String]("Left side is zero.")
left.unit.exponents.size == 1 or return error[Nothing, String]("Left side doesn't not have an atomic unit.")
left.unit.exponents.iter().&.get(0).value == 1 or return error[Nothing, String]("Left unit has exponent.")
var unit = left.unit.exponents.iter().&.get(0).key
not(dict.defs.contains(unit)) or return error[Nothing, String]("Already defined.")
dict.defs.&.put(unit, Amount { src = 0..0, number = right.number / left.number, unit = right.unit })
ok[Nothing, String]({})
}
fun canonicalize(dict: Dictionary, amount: Amount): Amount {
loop {
var something_changed = false
for entry in amount.unit.exponents do
if dict.defs.get_maybe(entry.key) is some(more_fundamental) then {
| println("{entry.key} -> {more_fundamental}")
if entry.value > 0 then {
amount.number = amount.number * more_fundamental.number
amount.unit = amount.unit / unit(entry.key) * more_fundamental.unit
} else {
amount.number = amount.number / more_fundamental.number
amount.unit = amount.unit * unit(entry.key) / more_fundamental.unit
}
something_changed = true
break
}
if not(something_changed) then break
| println("canonicalizing... {amount}")
}
amount
}
fun predef(dict: &Dictionary, line: String) {
var line = line.parse_line() or panic("predef line {line.debug()} is invalid")
var def = line.definition or panic("predef line {line.debug()} is not a definition")
var left = def.a.eval(dict.*) or panic("predef {line.debug()}: left eval failed")
var right = def.b.eval(dict.*) or panic("predef {line.debug()}: right eval failed")
dict.define(left, right) or panic("predef {line.debug()} failed")
}
fun main(): Never {
var dict = Dictionary { defs = map[String, Amount]() }
dict.&.predef("1000 fempto = pico")
dict.&.predef("1000 pico = nano")
dict.&.predef("1000 nano = micro")
dict.&.predef("1000 micro = milli")
dict.&.predef("1000 milli = 1")
dict.&.predef("100 centi = 1")
dict.&.predef("10 dezi = 1")
dict.&.predef("kilo = 1000")
dict.&.predef("mega = 1000 kilo")
dict.&.predef("giga = 1000 mega")
dict.&.predef("tera = 1000 giga")
dict.&.predef("kibi = 1024")
dict.&.predef("mibi = 1024 kibi")
dict.&.predef("gibi = 1024 mibi")
dict.&.predef("tibi = 1024 gibi")
dict.&.predef("8 bits = byte")
dict.&.predef("KB = kilo byte")
dict.&.predef("MB = mega byte")
dict.&.predef("GB = giga byte")
dict.&.predef("TB = tera byte")
dict.&.predef("KiB = kibi byte")
dict.&.predef("MiB = mibi byte")
dict.&.predef("GiB = gibi byte")
dict.&.predef("TiB = tibi byte")
dict.&.predef("mm = milli m")
dict.&.predef("cm = centi m")
dict.&.predef("km = kilo m")
dict.&.predef("lightspeed = 299792458 m/s")
dict.&.predef("gravity = 9.8 m/s/s")
dict.&.predef("ms = milli s")
dict.&.predef("min = 60 s")
dict.&.predef("h = 60 min")
dict.&.predef("day = 60 h")
dict.&.predef("week = 4 day")
dict.&.predef("year = 356 day")
loop {
print(">> ")
var line = {stdin.read_line() or panic("Couldn't read line")} or break
if line.trim().is_empty() then continue
var line = line.parse_line() or(err) {
stderr." "
for i in 0..err.cursor do stderr." "
stderr."^\n"
stderr."{err.msg}\n"
continue
}
switch line
case quit break
case term(term) {
var amount = term.eval(dict) or(err) {
if err.inputs.len == 1 then {
var a = err.inputs.get(0)
stderr." "
for i in 0..a.src.start do stderr." "
for i in a.src do stderr."-"
stderr."\n"
}
if err.inputs.len == 2 then {
var a = err.inputs.get(0)
var b = err.inputs.get(1)
stderr." "
for i in 0..a.src.start do stderr." "
for i in a.src do stderr."-"
for i in a.src.end..b.src.start do stderr." "
for i in b.src do stderr."-"
stderr."\n"
}
stderr."{err.msg}\n"
continue
}
var amount = dict.canonicalize(amount)
println(" = {amount}")
if amount.unit.is_none() then continue
for def in dict.defs do {
var canonicalized = dict.canonicalize(Amount { src = 0..0, number = 1.0, unit = unit(def.key) })
if canonicalized.unit == amount.unit then {
var alternative = Amount { src = 0..0, number = amount.number / canonicalized.number, unit = unit(def.key) }
println(" = {alternative}")
}
}
println()
}
case definition(def) {
var left = def.a.eval(dict) or(err) {
stderr."Couldn't eval left: {err.msg}\n"
continue
}
var right = def.b.eval(dict) or(err) {
stderr."Couldn't eval right: {err.msg}\n"
continue
}
dict.&.define(left, right) or(err) {
stderr."{err}\n"
continue
}
stdout."Defined {left.unit}.\n"
}
}
println()
exit(0)
}