2020-06-01 21:49:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace captn3m0\NandToTetris;
|
|
|
|
|
|
|
|
class CommandType {
|
|
|
|
const PUSH = 1;
|
|
|
|
const POP = 2;
|
2020-06-03 18:20:34 +00:00
|
|
|
|
2020-06-03 12:19:21 +00:00
|
|
|
// Program Flow Commands
|
2020-06-01 21:49:28 +00:00
|
|
|
const LABEL= 3;
|
|
|
|
const GOTO= 4;
|
|
|
|
const IF= 5;
|
2020-06-03 18:20:34 +00:00
|
|
|
|
2020-06-03 12:19:21 +00:00
|
|
|
// Function Calling Commands
|
2020-06-01 21:49:28 +00:00
|
|
|
const FUNC= 6;
|
|
|
|
const RETURN= 7;
|
2020-06-03 12:19:21 +00:00
|
|
|
const CALL = 0;
|
2020-06-01 21:49:28 +00:00
|
|
|
|
2020-06-03 12:19:21 +00:00
|
|
|
const ARITHMETIC= 8;
|
2020-06-01 21:49:28 +00:00
|
|
|
const ARITHMETIC_COMMANDS = ['add', 'sub', 'neg', 'eq', 'gt', 'lt', 'and', 'or', 'not'];
|
|
|
|
|
|
|
|
public static function fromName(String $name) {
|
|
|
|
if(in_array($name, self::ARITHMETIC_COMMANDS)) {
|
|
|
|
return self::ARITHMETIC;
|
|
|
|
} else {
|
|
|
|
$map = [
|
|
|
|
"call",
|
|
|
|
"push",
|
|
|
|
"pop",
|
|
|
|
"label",
|
|
|
|
"goto",
|
2020-06-03 12:19:21 +00:00
|
|
|
"if-goto",
|
|
|
|
"function",
|
2020-06-01 21:49:28 +00:00
|
|
|
"return"
|
|
|
|
];
|
|
|
|
return array_search($name, $map);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|