Browse Source

портируем TRisc из ББ в dart

https://bitbucket.org/petryxa/trisc
kpmy 10 years ago
parent
commit
1b66a4b7d2
9 changed files with 520 additions and 0 deletions
  1. 13 0
      LICENSE
  2. 1 0
      README.md
  3. 82 0
      lib/bits.dart
  4. 181 0
      lib/int.dart
  5. 90 0
      lib/mathe.dart
  6. 97 0
      lib/tri.dart
  7. 8 0
      pubspec.yaml
  8. 12 0
      web/index.html
  9. 36 0
      web/main.dart

+ 13 - 0
LICENSE

@@ -0,0 +1,13 @@
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+                   Version 2, December 2004
+
+Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.

+ 1 - 0
README.md

@@ -0,0 +1 @@
+# -0+

+ 82 - 0
lib/bits.dart

@@ -0,0 +1,82 @@
+part of tri;
+
+class Bits {
+
+  List<Tril> _trits;
+
+  void clear() {
+    _trits = new List(27);
+    for (int i = 0; i < 27; i++) _trits[i] = NULL;
+  }
+
+  void _fill(int x, int mx){
+    clear();
+    List<int> m = new List(mx);
+    int sign = x.toInt().sign;
+    int r = x.abs();
+    int i = 0;
+    if (sign != 0) {
+      do {
+        int s = r % 3;
+        r = r ~/ 3;
+        if (s > 1) {
+          r++;
+          m[i] = s - 3;
+        } else {
+          m[i] = s;
+        }
+        i++;
+      } while (!(r < 3));
+      if (r > 1) {
+        m[i] = r - 3;
+        m[i + 1] = 1;
+        i = i + 2;
+      } else {
+        m[i] = r;
+        i++;
+      }
+      do {
+        i--;
+        _trits[i] = new Tril.fromInt(sign * m[i]);
+      } while (i != 0);
+    }
+  }
+
+  int _toInt(){
+    int ret = _trits[0].toInt;
+    for (int i=1; i<_trits.length; i++){
+      ret = ret+(_trits[i].toInt*pow(3, i));
+    }
+    return ret;
+  }
+
+  int27 toInt27(){
+    return new int27(_toInt());
+  }
+
+  Bits.fromTryte(tryte x) {
+    _fill(x.toInt(), 9);
+  }
+
+  Bits.fromInt27(int27 x) {
+    _fill(x.toInt(), 27);
+  }
+
+  @override
+  String toString(){
+    String ret = "]";
+    _trits.forEach((t){
+      if(t.True)
+        ret = "+" + ret;
+      else if(t.False)
+        ret = "-" + ret;
+      else
+        ret = "0" + ret;
+    });
+    return '['+ret;
+  }
+
+  factory Bits(tri_num x) {
+    if (x is tryte) return new Bits.fromTryte(x); else if (x is int27) return new Bits.fromInt27(x);
+  }
+}

+ 181 - 0
lib/int.dart

@@ -0,0 +1,181 @@
+part of tri;
+
+/*
+ * трайт из девяти тритов
+ * целое из 27 тритов
+ * длинное целое из 81 трита, пока не реализуем
+ */
+
+const tryte_trit_num = 9;
+final tryte_limit = pow(tryte_trit_num * 3, 3);
+
+const int27_trit_num = 27;
+final int27_limit = pow(pow(tryte_trit_num * 3, 3), 3);
+
+abstract class tri_num{
+  int toInt();
+}
+
+class tryte extends tri_num {
+  static final tryte max = new tryte._new(tryte_limit ~/ 2);
+  static final tryte min = new tryte._new(-(tryte_limit ~/ 2));
+  static final _one = new tryte._new(1);
+  static final _zero = new tryte._new(0);
+
+  int _code = 0;
+
+  factory tryte(int x) {
+    if (x > max.toInt() || x < min.toInt()) throw new ArgumentError();
+    switch(x){
+      case 0: return _zero;
+      case 1: return _one;
+      default: return new tryte._new(x);
+    }
+  }
+
+  factory tryte.one(){
+    return _one;
+  }
+
+  factory tryte.zero(){
+    return _zero;
+  }
+  tryte._new(this._code);
+
+  @override
+  int toInt() => _code;
+
+  bool operator ==(tryte that) {
+    return this._code == that._code;
+  }
+
+  @override
+  int get hashCode => _code;
+
+  @override
+  String toString() {
+    return _code.toString();
+  }
+
+  tryte operator +(tryte that) {
+    return new tryte(this._code + that._code);
+  }
+
+  tryte operator -(tryte that) {
+    return new tryte(this._code - that._code);
+  }
+
+  bool operator >(tryte that) {
+    return (this._code > that._code);
+  }
+
+  bool operator <(tryte that) {
+    return (this._code < that._code);
+  }
+
+  bool operator <=(tryte that) {
+    return (this._code <= that._code);
+  }
+
+  bool operator >=(tryte that) {
+    return (this._code >= that._code);
+  }
+
+  tryte operator ~/(tryte that) {
+    return new tryte(this._code ~/ that._code);
+  }
+
+  tryte operator *(tryte that) {
+    return new tryte(this._code * that._code);
+  }
+
+  tryte operator %(tryte that) {
+    return new tryte(this._code % that._code);
+  }
+
+  tryte operator -(){
+    return new tryte(-this._code);
+  }
+}
+
+class int27 extends tri_num{
+  static final int27 max = new int27._new(int27_limit ~/ 2);
+  static final int27 min = new int27._new(-(int27_limit ~/ 2));
+  static final _one = new int27._new(1);
+  static final _zero = new int27._new(0);
+
+  int _code = 0;
+
+  factory int27(int x) {
+    if (x > max.toInt() || x < min.toInt()) throw new ArgumentError();
+    switch(x){
+      case 0: return _zero;
+      case 1: return _one;
+      default: return new int27._new(x);
+    }
+  }
+
+  factory int27.one(){
+    return _one;
+  }
+
+  factory int27.zero(){
+    return _zero;
+  }
+  int27._new(this._code);
+
+  @override
+  int toInt() => _code;
+
+  bool operator ==(int27 that) {
+    return this._code == that._code;
+  }
+
+  @override
+  int get hashCode => _code;
+
+  @override
+  String toString() {
+    return _code.toString();
+  }
+
+  int27 operator +(int27 that) {
+    return new int27(this._code + that._code);
+  }
+
+  int27 operator -(int27 that) {
+    return new int27(this._code - that._code);
+  }
+
+  bool operator >(int27 that) {
+    return (this._code > that._code);
+  }
+
+  bool operator <(int27 that) {
+    return (this._code < that._code);
+  }
+
+  bool operator <=(int27 that) {
+    return (this._code <= that._code);
+  }
+
+  bool operator >=(int27 that) {
+    return (this._code >= that._code);
+  }
+
+  int27 operator ~/(int27 that) {
+    return new int27(this._code ~/ that._code);
+  }
+
+  int27 operator *(int27 that) {
+    return new int27(this._code * that._code);
+  }
+
+  int27 operator %(int27 that) {
+    return new int27(this._code % that._code);
+  }
+
+  int27 operator -(){
+    return new int27(-this._code);
+  }
+}

+ 90 - 0
lib/mathe.dart

@@ -0,0 +1,90 @@
+library tri;
+
+import 'dart:math';
+
+part 'tri.dart';
+part 'int.dart';
+part 'bits.dart';
+
+/*
+ * циклическое отрицание x + 1 (по модулю 3)
+ */
+Tril cnot(Tril x) {
+  if (x.True) return FALSE; else if (x.False) return NULL; else return TRUE;
+}
+
+/*
+ * сложение по модулю 3 в абсолютной системе
+ */
+Tril sum3(Tril p, Tril q) {
+  if (p.False) return q; else if (p.Null) return q.False ? NULL : (q.Null ? TRUE : FALSE); else if (p.True) return q.False ? TRUE : (q.Null ? FALSE : NULL);
+  throw new ArgumentError();
+}
+
+/*
+ * сложение по модулю 3 в относительной системе
+ */
+Tril sum3r(Tril p, Tril q) {
+  return cnot(cnot(sum3(p, q)));
+}
+
+/*
+ * перенос при сложении в абсолютной системе
+ */
+Tril scarry3(Tril p, Tril q) {
+  if (p.False) return FALSE; else if (p.Null) return q.True ? NULL : FALSE; else if (p.True) return q.False ? FALSE : NULL;
+  throw new ArgumentError();
+}
+
+/*
+ * перенос при сложении в относительной системе
+ */
+Tril scarry3r(Tril p, Tril q) {
+  if (p.False && q.False) return FALSE; else if (p.True && q.True) return TRUE; else return NULL;
+}
+
+/*
+ * умножение по модулю 3 в абсолютной системе
+ */
+Tril mul3(Tril p, Tril q) {
+  if (p.False) return FALSE; else if (p.Null) return q; else if (p.True) return q.False ? FALSE : (q.Null ? TRUE : NULL);
+  throw new ArgumentError();
+}
+
+/*
+ * умножение по модулю 3 в относительной системе
+ */
+Tril mul3r(Tril p, Tril q) {
+  if (p.Null || q.Null) return NULL; else return p == q ? TRUE : FALSE;
+}
+
+/*
+ * перенос при умножении в абсолютной системе, при умножении в относительной системе переноса нет
+ */
+Tril mcarry(Tril p, Tril q) {
+  if (p.True && q.True) return NULL; else return FALSE;
+}
+
+/*
+ * функция вебба
+ */
+Tril webb(Tril p, Tril q) {
+  return cnot(p | q);
+}
+
+/*
+ * модуль
+ */
+Tril mod(Tril x) {
+  return x | ~x;
+}
+
+tryte short(int27 i){
+  if(i.toInt()<tryte.min.toInt() || i.toInt()>tryte.max.toInt())
+      throw new ArgumentError();
+  return new tryte(i.toInt());
+}
+
+int27 long(tryte t){
+  return new int27(t.toInt());
+}

+ 97 - 0
lib/tri.dart

@@ -0,0 +1,97 @@
+part of tri;
+/*
+ * пример троичной логики
+ */
+final Tril TRUE = new Tril._constant(true);
+final Tril FALSE = new Tril._constant(false);
+final Tril NULL = new Tril._constant(null);
+
+
+/*
+ * аксиома лукасевича про импликацию
+ */
+Tril impl(Tril x, Tril y) {
+  if (x.False && y.False) return TRUE; else if (x.False && y.True) return TRUE; else if (x.True && y.False) return FALSE; else if (x.True && y.True) return TRUE; else if (x.True && y.Null) return NULL; else if (x.Null && y.False) return NULL; else if (x.False && y.Null) return TRUE; else if (x.Null && y.Null) return TRUE; else if (x.Null && y.True) return TRUE;
+  throw new Exception("unexpected :(");
+}
+
+Tril eq(Tril x, Tril y) {
+  return impl(x, y) & impl(y, x);
+}
+
+class Tril {
+  bool _code;
+
+  static final Tril _true = new Tril._constant(true);
+  static final Tril _false = new Tril._constant(false);
+  static final Tril _null = new Tril._constant(null);
+
+  Tril._constant(this._code);
+
+  factory Tril(bool code) {
+    if (code == null) return _null; else if (code) return _true; else return _false;
+  }
+
+  factory Tril.fromInt(int code) {
+    switch (code) {
+      case 0:
+        return _null;
+      case 1:
+        return _true;
+      case -1:
+        return _false;
+      default:
+        throw new ArgumentError();
+    }
+  }
+
+  bool operator ==(Tril that) {
+    return this._code == that._code;
+  }
+
+  @override
+  int get hashCode {
+    if (_code == null) return 0; else if (_code) return 1; else return -1;
+  }
+
+  int get toInt => hashCode;
+
+  bool get Null => (_code == null);
+
+  bool get True {
+    if (_code == null) return false;
+    return _code;
+  }
+
+  bool get False {
+    if (_code == null) return false;
+    return _code;
+  }
+
+  @override
+  String toString() {
+    if (_code == null) return "null"; else if (_code) return "true"; else return "false";
+  }
+
+  /*
+   * аксиома лукасевича про отрицание
+   */
+  Tril operator ~() {
+    if (_code == null) return NULL; else if (_code) return FALSE; else return TRUE;
+  }
+
+  /*
+   * OR
+   */
+  Tril operator |(Tril that) {
+    return impl(impl(this, that), that);
+  }
+
+  /*
+   * AND
+   */
+  Tril operator &(Tril that) {
+    return ~(~this | ~that);
+  }
+
+}

+ 8 - 0
pubspec.yaml

@@ -0,0 +1,8 @@
+name: tri
+version: 0.0.1
+description: trinary stuff
+environment:
+  sdk: '>=1.0.0 <2.0.0'
+dependencies:
+  browser: any
+  unittest: any

+ 12 - 0
web/index.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="utf-8">
+    <title>tri</title>
+</head>
+<body>
+TRI
+<script type="application/dart" src="main.dart"></script>
+<script data-pub-inline src="packages/browser/dart.js"></script>
+</body>
+</html>

+ 36 - 0
web/main.dart

@@ -0,0 +1,36 @@
+import 'package:unittest/unittest.dart';
+import 'package:tri/mathe.dart';
+
+void main() {
+  test("basics", (){
+    expect(TRUE, equals(TRUE));
+    expect(FALSE, equals(FALSE));
+    expect(NULL, isNot(equals(TRUE)));
+    print("$TRUE, $NULL, $FALSE");
+    expect(~NULL, equals(NULL));
+    expect(~TRUE, equals(FALSE));
+    expect(~FALSE, equals(TRUE));
+    expect(new Tril.fromInt(0), equals(NULL));
+  });
+  test("logic", (){
+    expect(TRUE | TRUE, equals(TRUE));
+    expect(FALSE & FALSE, equals(FALSE));
+  });
+
+  print(tryte.min);
+  print(tryte.max);
+  print(int27.min);
+  print(int27.max);
+
+  test("arithmetics", (){
+    expect(new tryte(5)+new tryte.one(), equals(new tryte(6)));
+    expect(short(new int27(40)), equals(new tryte(40)));
+  });
+
+  test("bits", (){
+    expect(new Bits(int27.max).toString(), equals("[+++++++++++++++++++++++++++]"));
+    expect(new Bits(tryte.max).toString(), equals("[000000000000000000+++++++++]"));
+    expect(tryte.max, equals(short(new Bits(tryte.max).toInt27())));
+    expect(int27.max, equals(new Bits(int27.max).toInt27()));
+  });
+}