Created with <3 with dartpad.dev.
Last active
October 27, 2022 09:17
-
-
Save halka9000stg/5d2e3a2d0572a5f53a1d6ef1abbeb6aa to your computer and use it in GitHub Desktop.
仮想処理装置 (尺寸符号化データ&微細指令)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:typed_data"; | |
import "dart:math"; | |
//仮想処理装置 (尺寸符号化データ&微細指令) | |
//Virtual Processor (Aligned Binary Data & Microcode) | |
typedef TrailOperandFmt = List<OperandUnitFmt>; | |
void main() { | |
} | |
class VProcessor{ | |
final Uint8List magic; | |
final List<MicroCode> codes; | |
VProcessor(this.codes, [Uint8List? magic]): this.magic = (magic != null) ? magic : Uint8List(0); | |
List<ProcUnit> decode(List<int> binData){ | |
return []; | |
} | |
List<int> encode(List<ProcUnit> procs){ | |
return []; | |
} | |
List<ProcUnit> assemble(List<String> prog){ | |
return []; | |
} | |
void proc(List<ProcUnit> binData){} | |
void run<T>(List<T> data){ | |
if(data is List<ProcUnit>){ | |
this.proc(data); | |
}else if(data is List<String>){ | |
this.proc(this.assemble(data)); | |
}else if(data is List<int>){ | |
this.proc(this.decode(data)); | |
}else{ | |
throw Error(); | |
} | |
} | |
} | |
abstract class ProcUnit{} | |
abstract class MicroCode extends ProcUnit{ | |
final Symbol mnemonic; | |
//if < 0, lazy defined(trail count-integer additionally) | |
final int trails; | |
final Uint8List opcode; | |
MicroCode(this.mnemonic, this.trails, this.opcode); | |
} | |
abstract class MemUnit extends ProcUnit{ | |
final int bits; | |
final Endian endian; | |
MemUnit(this.bits, [this.endian = Endian.big]); | |
void setData({int? data, List<int>? list}){} | |
} | |
abstract class OverOctetMemUnit extends MemUnit { | |
final int octets; | |
final Uint8List data; | |
OverOctetMemUnit(int octets, [Endian endian = Endian.big]) | |
: this.octets = octets, | |
this.data = Uint8List(octets), | |
super(octets * 8, endian); | |
} | |
class Byte extends OverOctetMemUnit { | |
Byte([Endian endian = Endian.big]) : super(8, endian); | |
} | |
class Half extends OverOctetMemUnit { | |
Half([Endian endian = Endian.big]) : super(16, endian); | |
} | |
class Word extends OverOctetMemUnit { | |
Word([Endian endian = Endian.big]) : super(32, endian); | |
} | |
extension MoveUp on int{ | |
List<int> moveUp8()=>this.moveUp(8); | |
List<int> moveUp16()=>this.moveUp(16); | |
List<int> moveUp32()=>this.moveUp(32); | |
List<int> moveUp(int radix)=>this._moveUp(radix, []); | |
List<int> _moveUp(int radix, List<int> data){ | |
num base = pow(radix, data.length); | |
if(this < radix){ | |
data.add(this); | |
return data; | |
}else{ | |
int rem = this % base as int; | |
int qu = this ~/ base; | |
data.add(rem); | |
return qu._moveUp(radix, data); | |
} | |
} | |
} | |
class OperandUnitFmt{ | |
final Symbol name; | |
final OperandCondition cond; | |
const OperandUnitFmt(this.name, [this.cond = const OperandCondition.voided()]); | |
} | |
bool anyway(List<String> _) => true; | |
class OperandCondition{ | |
final List<Symbol> useOperand; | |
final bool Function(List<String>) condition; | |
const OperandCondition(this.useOperand, this.condition); | |
const OperandCondition.voided(): | |
this.useOperand = const <Symbol>[], | |
this.condition = anyway; | |
} | |
extension on TrailOperandFmt{ | |
bool _validate(List<Symbol> knownOperand, int loc){ | |
if(loc >= this.length){ | |
return true; | |
}else{ | |
OperandUnitFmt current = this[loc]; | |
List<Symbol> use = current.cond.useOperand; | |
if(use.isNotEmpty){ | |
if(!use.every((Symbol s) => knownOperand.contains(s))){ | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
bool validate(){ | |
List<Symbol> knownOperand = []; | |
return this._validate(knownOperand, 0); | |
} | |
} | |
enum BinSizeUnit{ | |
bit, | |
octet, | |
} | |
class ListOr<T>{ | |
final List<T> _il; | |
ListOr._(this._il); | |
factory ListOr({T? data, List<T>? list}){ | |
if(data != null && list != null){ | |
return ListOr<T>._(list); | |
}else if(data != null){ | |
return ListOr<T>._([data]); | |
}else if(list != null){ | |
return ListOr<T>._(list); | |
}else{ | |
throw Error(); | |
} | |
} | |
List<T> asList() => this._il; | |
T asSingle([int index = 0])=>this._il[index]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment