Skip to content

Commit

Permalink
Assembler in progress!
Browse files Browse the repository at this point in the history
  • Loading branch information
Peppy3 committed Mar 31, 2022
1 parent 9920156 commit d27233e
Show file tree
Hide file tree
Showing 25 changed files with 95 additions and 14 deletions.
Binary file removed Compiler/py compiler/__pycache__/lexer.cpython-38.pyc
Binary file not shown.
Binary file removed Compiler/py compiler/a.bin
Binary file not shown.
Empty file removed Compiler/py compiler/file.code
Empty file.
11 changes: 0 additions & 11 deletions Compiler/py compiler/main.py

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added Compilers/Assembler/__pycache__/lexer.cpython-38.pyc
Binary file not shown.
9 changes: 9 additions & 0 deletions Compilers/Assembler/file.code
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
constant FeM 5
label start
# comment
sio 1 0 10
addi 1 FeM

#blablabla
lio 1 0 9
jmp start
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ def io_instructions(word:str , val1: int, val2:int, address:int):
elif (word == "print"):
return(instructions.create_opcodes("sio", val1, val2, 0, 4))

def loop()
def lexer_script(input:str):
program = open(input, 'r')
identifier:bool = False
number:bool = False
for i in program:
67 changes: 67 additions & 0 deletions Compilers/Assembler/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import instructions
import os

def decode(word:str):
word = word.replace("\n", "")
dec_text :list = (word.split(" "))
return(dec_text)

def is_int(s:str):
try:
int(s)
return(True)
except(ValueError):
pass
return(False)


file_path: str = "file.code"

jump_or_branch :tuple = ("jmp", "b", "jeq",
"beq", "jneq", "bneq", "jl", "bl", "jleq",
"bleq", "jg", "bg", "jgeq", "bgeq", "jeqi",
"beqi", "jneqi", "bneqi", "jli", "bli",
"jleqi", "bleqi", "jgi", "bgi", "jgeqi", "bgeqi")

with open(file_path, 'r') as input:
with open('a.bin', 'w+b') as output:
EOF :bool = False
line_num :int = 0
constants :dict = {}
labels :dict = {}
text :str = ""
dec_text :list = []

while(EOF == False):

text = input.readline()
if (text == ""):
EOF = True
elif (text[0] == "\n" or "#" in text):
continue
else:
dec_text = decode(text)
if (dec_text[0] == "constant" or dec_text[0] == "const"):
constants[dec_text[1]] = dec_text[2]
elif (dec_text[0] == "label"):
labels[dec_text[1]] = line_num
else:
#print(str(dec_text) + str(line_num))
line_num += 1

l :bool = False
line :list = []
for i in dec_text:

if (l == True):
l = False
line.append(i)
elif (i in jump_or_branch):
l = True
line.append(i)
else:
line.append(i)
print(line)

print("constants = " + str(constants))
print("labels = " + str(labels))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions documents/Assembly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# *A Guide to The Computers Assembly*

## Constants
Constants are variables and defined at the start of a file with
`Const:
var_A = 52
var_B = 42`.
Where "var_A" and "var_B" are the variable names.

## Labels
Lebels are defined with `label`.
It labels the next line.
4 changes: 2 additions & 2 deletions documents/hll.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ___
- Declaring a variable works by setting a name and assign a value to it - assignment uses `=` as operator: `foo=5;`
- Between any symbol or qualifier may be an unlimited amount of spaces or linebreaks.
- To use procedures from another file, put `use([file])` at the start of your code. The specified file should contain source code as well.

## Loops and goto
There are `while`, `for` and `loop` loops.

Expand All @@ -25,8 +26,7 @@ Shifting uses `<<` and `>>` as well as rotation uses `<<<` and `>>>`.

## Conditions
Conditions are generally based on unsigned values and use the commonly accepted symbols: `=, !=, <, >, <=, >=`.
Conditional blocks start with `if([condition]) { }` and might be inverted using `ifnot([condition]) { }`
or chained by using `else{ }` or `else([condition]){ }`.
Conditional blocks start with `if([condition]) { }` and might be chained by using `else{ }` or `else([condition]){ }`.
A condition might include arithmetic and logical operations as well as boolean logic using `|| &&`.

## Arrays
Expand Down

0 comments on commit d27233e

Please sign in to comment.