Skip to content

Commit

Permalink
feat(Transcriber)!: Transcribe pseudo to java and c++
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Kenan committed Nov 23, 2022
1 parent 733056b commit 1aa1c1f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 12 deletions.
Binary file modified Build/PseudoBlocksSetUp.exe
Binary file not shown.
20 changes: 19 additions & 1 deletion Build/installerConfig.ifp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Windows 8 = 1
Windows 10 = 1
Windows Server 2016 = 1
DoNotCheckOS = 1
Company name = Daniel Slinda
Company name = ump
Website = https://github.com/Daniel-Kenan/pseudo-blocks
SFA = 0
DFA = 0
Expand Down Expand Up @@ -148,6 +148,24 @@ py
C:\Users\Daniel Slinda\Documents\Development\pseudo-blocks\translation.py
1 KB
py
C:\Users\Daniel Slinda\Documents\Development\pseudo-blocks\clink\
N/A
[Folder]
C:\Users\Daniel Slinda\Documents\Development\pseudo-blocks\Documentation\
N/A
[Folder]
C:\Users\Daniel Slinda\Documents\Development\pseudo-blocks\images\
N/A
[Folder]
C:\Users\Daniel Slinda\Documents\Development\pseudo-blocks\manager\
N/A
[Folder]
C:\Users\Daniel Slinda\Documents\Development\pseudo-blocks\python-3.9.10-embed-amd64\
N/A
[Folder]
C:\Users\Daniel Slinda\Documents\Development\pseudo-blocks\templates\
N/A
[Folder]
[Licence_Begin]
19896
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}
Expand Down
97 changes: 97 additions & 0 deletions Transcriber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import codecs
import sys
import ast

arr = sys.argv[1]
arr = codecs.decode(arr, "hex").decode('utf-8')
arr= arr.split("**")

variables = ast.literal_eval(codecs.decode(sys.argv[2], "hex").decode('utf-8'))

# print(type(variables))
java = {"Declare.inputVar":"","print":"System.out.println"}
cpp = {"Declare.inputVar":"cin<<","print":"cout << ","none":"none"}

languages = {"cpp":cpp,"java":java}

def transcribe(language:str):
repr = language
language = languages[language]
syntax = []
scan = None
for i in arr:
func,param = i[:i.index("(")] , i[i.index("("):]

if func == "Declare.inputVar":
scan = True
try:
float(param[1:-1])
if repr == "java":
syntax.append("= input.nextDouble();")
elif repr == "cpp":
syntax.append("cin << " + param)
except:
if repr == "java":
syntax.append(param[2:-2]+" = input.nextDouble()")
elif repr == "cpp":
syntax.append("cin >> " + param[2:-2])
#type String
continue

elif func == "print":
syntax.append(language["print"]+param)
syntax = list(map(lambda elem: elem +"; \n",syntax))

if repr == "java" :
syntax.insert(0,'public class Main {\n')
syntax.insert(1,"public static void main(String[] args) {\n")
if scan:
syntax.insert(1,"import java.util.Scanner;\n")
syntax.insert(3,"Scanner input = new Scanner(System.in);\n ")
syntax.append("\t};")
for identifier,typeof in variables.items():
if typeof == "num":
syntax.insert(3,f"double {identifier};")
continue
elif typeof=="string" :
syntax.insert(syntax.index("public static void main(String[] args) {\n")+1,f"String {identifier};\n")
continue

elif repr == "cpp":
syntax.insert(0,"#include <stdio.h>\n")
syntax.insert(1,'using namespace std;\n')
syntax.insert(2,'int main(){\n')

for identifier,typeof in variables.items():
if typeof == "num":
syntax.insert(3,f"double {identifier};")
continue
elif typeof=="string" :
syntax.insert(3,f"{typeof} {identifier};")
continue

syntax.append("return (0);")

syntax.append("}")
return syntax


print("-----------------Java---------------------")
print()

with open("./Main.java","w") as file:
for line in transcribe("java"):
print(line)
file.write(line)

print()
print("*"*43)
print()
print("------------------CPP----------------------")
print()
with open("./Main.cpp","w") as file:
for line in transcribe("cpp"):
print(line)
file.write(line)
print()
print("*"*43)
8 changes: 7 additions & 1 deletion compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from Logger import *
from functions import *
from pathlib import Path
import subprocess

sys.dont_write_bytecode = True
__testcase__ = False
Expand Down Expand Up @@ -140,7 +141,12 @@ def while_loop(condition:str,commands:list):
line = syntax.contents[index_declaration]

if sys.argv[-1] == "java" or sys.argv[-1] == "cpp":
print(syntax.contents)
# print(syntax.contents,Declare.declared_variables)
arg1 = "**".join(syntax.contents).encode('utf-8').hex()
arg2 = str(Declare.declared_variables)
arg2 = arg2.encode('utf-8').hex()
output=str(subprocess.getoutput(f'%python3% {sys.path[3]}\\\\Transcriber.py \"{arg1}\" {arg2}'))
print(output)
sys.exit()

for line in syntax.contents:
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ C:\Users\Daniel\Desktop\playground>
#### >> To translate pseudocode to cpp

```powershell
C:\Users\Daniel\Desktop\playground> pseudo translate <filename> cpp
C:\Users\Daniel\Desktop\playground> pseudo --translate <filename> cpp
C:\Users\Daniel\Desktop\playground>
Expand Down
1 change: 1 addition & 0 deletions pseudo.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ goto terminate
:version
TYPE "%~sp0\CHANGELOG.md"
goto terminate

:translate
%python3% "%~sp0\compile.py" "%~2" "cpp"
goto terminate
Expand Down
13 changes: 4 additions & 9 deletions translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
templateDir = './pseudo-blocks/templates/Main.'

class Translation:

def __init__(self,syntax:list) -> None:
self.contents = syntax

Expand All @@ -15,20 +16,14 @@ def template(programming_language):
with open(templateDir+programming_language) as template:
contents = template.readlines()
return contents

def gen(self):
for i in range(10000):
yield i

def increment(self):next(self.gen())

def toJava(self):
template = Translation.template('java')
if 'input' in self.contents:
template.insert(self.increment() ,"import java.util.Scanner;\n\n")
template.insert(0 ,"import java.util.Scanner;\n\n")

for i in range(self.contents):
if i == "string":pass
for syntax in range(self.contents):
if syntax == "string":pass


# template.insert(4,"\t Scanner input = new Scanner(System.in);")
Expand Down

0 comments on commit 1aa1c1f

Please sign in to comment.