slipcompiler/SlipcodeCompilationTest.ipynb

164 lines
6.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"instruction_opcodes = {\n",
" \"IDK\": 0x00,\n",
" \"LIT\": 0x01,\n",
" \"SWAP\": 0x02,\n",
" \"DEL\": 0x03,\n",
" \"COPY\": 0x04,\n",
" \"DEF\": 0x05,\n",
" \"END\": 0x06,\n",
" \"ADD\": 0x10,\n",
" \"SUB\": 0x11,\n",
" \"MUL\": 0x12,\n",
" \"DIV\": 0x13,\n",
" \"POW\": 0x14,\n",
" \"SQRT\": 0x15,\n",
" \"ONEMIN\": 0x16,\n",
" \"ROUND\": 0x17,\n",
" \"CEIL\": 0x18,\n",
" \"FLOOR\": 0x19,\n",
" \"MOD\": 0x1a,\n",
" \"FRACT\": 0x1b,\n",
" \"COMP\": 0x1c,\n",
" \"LERP\": 0x1d,\n",
" \"MIN\": 0x1e,\n",
" \"MAX\": 0x1f\n",
"}\n",
"\n",
"type_opcodes = {\n",
" \"BOOL\": {\"code\": 0x00, \"length\": 1},\n",
" \"INT\": {\"code\": 0x01, \"length\": 4},\n",
" \"FLT\": {\"code\": 0x02, \"length\": 4},\n",
" \"VEC\": {\"code\": 0x03, \"length\": 12}\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"source_file = open(\"Test.slc\")\n",
"source_code = source_file.read()\n",
"source_code = source_code.replace(\" \", \"\\n\")\n",
"source_code = source_code.split(\"\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LIT, \n",
"Parsing generic command...\n",
"FLT, lit_type\n",
"Parsing literal type...\n",
"-1.0, literal\n",
"Parsing literal value...\n",
"LIT, literal\n",
"Parsing literal value...\n"
]
},
{
"ename": "ValueError",
"evalue": "could not convert string to float: 'LIT'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_20464/1959865281.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[1;31m# Handle float literals\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 24\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlit_type\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"FLT\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 25\u001b[1;33m \u001b[0mlit_bytes\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbytearray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstruct\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\">f\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mword\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 26\u001b[0m \u001b[0mbyte_code\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlit_bytes\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 27\u001b[0m \u001b[1;31m# Handle vector literals\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mValueError\u001b[0m: could not convert string to float: 'LIT'"
]
}
],
"source": [
"import struct\n",
"\n",
"byte_code = bytearray()\n",
"code_type = \"\"\n",
"lit_type = \"\"\n",
"for word in source_code:\n",
" print(f\"{word}, {code_type}\")\n",
" # Handle literal types\n",
" if code_type == \"lit_type\":\n",
" print(\"Parsing literal type...\")\n",
"\n",
" # Consume a literal of this word's type next\n",
" code_type = \"literal\"\n",
" lit_type = word\n",
"\n",
" # Push the type opcode onto the instruction stack\n",
" byte_code.append(type_opcodes[word][\"code\"])\n",
" continue\n",
" # Handle literal values\n",
" if code_type == \"literal\":\n",
" print(\"Parsing literal value...\")\n",
"\n",
" # Consume a generic command next\n",
" code_type = \"\"\n",
"\n",
" # Handle float literals\n",
" if lit_type == \"FLT\":\n",
" lit_bytes = bytearray(struct.pack(\">f\", float(word)))\n",
" byte_code.extend(lit_bytes)\n",
" # Handle vector literals\n",
" if lit_type == \"VEC\":\n",
" # Creates an array of floats from a string like \"-1.0,7.3,10.5\"\n",
" vector_components = word.split(\",\")\n",
" for component in vector_components:\n",
" lit_bytes = bytearray(struct.pack(\">f\", float(component)))\n",
" byte_code.extend(lit_bytes)\n",
" continue\n",
" # Handle generic commands\n",
" else:\n",
" print(\"Parsing generic command...\")\n",
" if word == \"LIT\":\n",
" code_type = \"lit_type\"\n",
" byte_code.append(instruction_opcodes[word])\n",
" continue\n",
"\n",
"pure_bytes = bytes(byte_code)\n",
"with open(\"Test.slb\", \"wb\") as binary_file:\n",
" binary_file.write(pure_bytes)"
]
}
],
"metadata": {
"interpreter": {
"hash": "57baa5815c940fdaff4d14510622de9616cae602444507ba5d0b6727c008cbd6"
},
"kernelspec": {
"display_name": "Python 3.7.8 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.8"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}