tiamat 1.0.0-rc1 copy "tiamat: ^1.0.0-rc1" to clipboard
tiamat: ^1.0.0-rc1 copied to clipboard

This is an emulator of the COMET2 and a compiler of the CASL2.

example/main.dart

import 'package:tiamat/tiamat.dart';

Future<void> main() async {
  const asm = '''
; サンプルコード
DO      START
; GR1     AND     GR1,GR1       ; ラベルエラー
LOOP    IN      IBUF,31       ; マクロ
        OUT     OUT,38        ; マクロ
        LAD     GR1,0
        LD      GR0,IBUF,GR1  コメント
        CPL     GR0,EXIT,GR1  ; コメント
        ; LAD     GR0,0
        JNZ     LOOP
        LAD     GR1,1,GR1
        LD      GR0,IBUF,GR1
        CPL     GR0,EXIT,GR1
        JNZ     LOOP
        LAD     GR1,1,GR1
        LD      GR0,IBUF,GR1
        CPL     GR0,EXIT,GR1
        JNZ     LOOP
        LAD     GR1,1,GR1
        LD      GR0,IBUF,GR1
        CPL     GR0,EXIT,GR1
        JNZ     LOOP
        LAD     GR1,1,GR1
        LD      GR0,IBUF,GR1
        CPL     GR0,EXIT,GR1
        JNZ     LOOP
END     OUT     MSG,32        ; マクロ
        RET
EXIT    DC      'exit',-1
OUT     DC      'input:'
IBUF    DS      31
EOF     DC      #FFFF
MSG     DC      'goodbye!',-1
        END

MAIN    START
        CALL DO
        RET
        END
''';
  print('casl2:');
  syntaxHighlight(asm);

  final casl2 = Casl2.fromString(asm);

  final mod = switch (casl2.build()) {
    Ok<Module, dynamic> ok => ok.unwrap,
    Err<dynamic, List<Casl2Error>> err => throw Exception(err.unwrap),
  };

  final comet2 = Comet2(mod.bin);

  final start = mod.labels['MAIN'] ?? 0;
  await comet2.run(start);
}

void syntaxHighlight(final String asm) {
  const white = 255;
  const yellow = 220;
  const orange = 208;
  const green = 120;
  const red = 196;
  const gray = 240;

  final lexer = Lexer(asm.runes);

  var str = '';
  while (true) {
    final result = lexer.nextToken();
    if (result.isErr) {
      print(result);
      break;
    }
    final token = result.ok;
    if (token == tokenEOF) {
      break;
    }

    switch (token.type) {
      case Type.label:
        str += '\u001b[38;5;${yellow}m${token.string}';
        break;
      case Type.hex:
      case Type.dec:
        str += '\u001b[38;5;92m${token.string}';
        break;
      case Type.ref:
        str += '\u001b[38;5;${yellow}m${token.string}';
        break;
      case Type.gr:
        str += '\u001b[38;5;${orange}m${token.string}';
        break;
      case Type.text:
        str += '\u001b[38;5;${green}m${token.string}';
        break;
      case Type.unexpected:
        str += '\u001b[38;5;${red}m${token.string}';
        break;
      case Type.comment:
        str += '\u001b[38;5;${gray}m${token.string}';
        break;
      default:
        str += '\u001b[38;5;${white}m${token.string}';
    }
  }
  print(str);
}
copied to clipboard
0
likes
160
points
67
downloads

Publisher

unverified uploader

Weekly Downloads

2024.10.08 - 2025.04.22

This is an emulator of the COMET2 and a compiler of the CASL2.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

More

Packages that depend on tiamat