Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: willrandship on February 25, 2011, 01:17:08 am

Title: Easy way to make a compiler? :P
Post by: willrandship on February 25, 2011, 01:17:08 am
Basically, I need to make a program that takes a word and a 4 digit argument, and turns it into a 6 digit argument with the first two digits corresponding to the word. Ideally this would occur in 2 txt files, reading from one and writing to another.

Example (not definite #s)
If 0103 would be 210103

One way I was thinking was python, since it has the so very lovely dictionaries. However, I'm not so good at python, and it's pretty much a choice between that, C, and maybe C++. No .net (sorry sir :P) since I am on linux and mono is being a pain.

BTW this is for a processor I'm making, so don't expect to recognize anything.
Title: Re: Easy way to make a compiler? :P
Post by: Michael_Lee on February 25, 2011, 01:39:03 am
I would recommend Python, but then again, I'm a Python fan so I'm a bit biased.

If you're guaranteed a space between the word and the four digit number, try using index to find the location of the space, splice the string, then use that string to look up the appropriate value from the dictionary.  Or something.  Just throwing suggestions out there.

I could help out a little if you want, because today I was actually doing something similar to what you seem to be doing.
Title: Re: Easy way to make a compiler? :P
Post by: AngelFish on February 25, 2011, 01:42:14 am

BTW this is for a processor I'm making, so don't expect to recognize anything.

Is it bad that I recognize 0103 as the hex code for BSRF R1 for SH3 processors, then? :P
Title: Re: Easy way to make a compiler? :P
Post by: willrandship on February 25, 2011, 07:29:31 pm
Yes, that's bad :P this isn't hex it's compiling.

@Micheal that would be nice. :) The format can be pretty much anything, nothing's set in stone since the processor doesn't exist yet :P
Title: Re: Easy way to make a compiler? :P
Post by: Michael_Lee on February 25, 2011, 07:54:52 pm
Well, assuming you have a space dividing the word and the number, and assuming follows the format you gave, and assuming that there's only one command per line (wow, I'm assuming a lot of things, aren't I?), here would be a basic script that takes a text file named 'thing.txt' and creates a second text file named 'otherthing.txt' (although you probably would end up wanting this to to automatically generate the output file name).

Code: [Select]
import sys

table = {"If":"21",
         "While":"32",
         "etc...":"25",
         ...
         "For":"91"}


input_path = "C:\\Users\\TheGame\\SomeDirectory\\thing.txt"
output_path = "C:\\Users\\Lobster\\n_example\\otherthing.txt"
# The double slashes are recommended - you don't want to accidentally have a '\n' somewhere.

input_file = open(input_path, "r")
output_file = open(output_path, "w")



for line in input_file:
    cut = line.index(" ")    # This searches for the position of the space
    replacement = table[line[0:cut]]
    output_file.write(replacement + line[cut+1:] + "\n")
    # Just doing line[cut:] would include the space.



input_file.close()
output_file.close()

Print("Done!")



It's a bit simple, but I hope it helps!
Title: Re: Easy way to make a compiler? :P
Post by: willrandship on February 25, 2011, 09:17:48 pm
Wow, that is exactly what I needed! A little shifting for local directory access (from the same folder), and an input, and it's pretty much done, and I can manage those. I'll be sure to put you in the reference code!

I was going to mention the other #s, but then I realized you got them with the + line[cut+1:] bit. :)
Title: Re: Easy way to make a compiler? :P
Post by: Michael_Lee on February 25, 2011, 10:06:44 pm
Yup, no problem!  Glad to help.

Just be aware that most forms of input will automatically turn single slashes into double slashes, so you probably won't have to account for those (although you should test to be certain).
Title: Re: Easy way to make a compiler? :P
Post by: willrandship on February 25, 2011, 10:37:56 pm
Well, I was planning on more of a gcc-style approach, where the file is in the local directory. I don't think I'll bother with arguments though.
Title: Re: Easy way to make a compiler? :P
Post by: willrandship on February 26, 2011, 10:43:51 pm
Graagh, this is giving me a syntax error.....

source.close()

and I dont know why.....
Title: Re: Easy way to make a compiler? :P
Post by: Michael_Lee on February 26, 2011, 11:00:12 pm
If it's syntax, check to make sure you added your colons and correctly closed all your parenthesis, braces, and such?  In IDLE, the brackets look very similar to parenthesis, at least for me...

If it crashes while running the script, make sure that you opened it in the first place?
Title: Re: Easy way to make a compiler? :P
Post by: willrandship on February 26, 2011, 11:22:48 pm
hehe, just figured it out IIRC, it was a ), but thanks! BTW, it's done!