Author Topic: Easy way to make a compiler? :P  (Read 10166 times)

0 Members and 1 Guest are viewing this topic.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Easy way to make a compiler? :P
« 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.

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #1 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.
« Last Edit: February 25, 2011, 01:41:22 am by Michael_Lee »
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #2 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
« Last Edit: February 25, 2011, 01:42:25 am by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #3 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

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #4 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!
« Last Edit: February 25, 2011, 07:56:53 pm by Michael_Lee »
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #5 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. :)

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #6 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).
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #7 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.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #8 on: February 26, 2011, 10:43:51 pm »
Graagh, this is giving me a syntax error.....

source.close()

and I dont know why.....

Offline Michael_Lee

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1019
  • Rating: +124/-9
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #9 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?
My website: Currently boring.

Projects:
Axe Interpreter
   > Core: Done
   > Memory: Need write code to add constants.
   > Graphics: Rewritten.  Needs to integrate sprites with constants.
   > IO: GetKey done.  Need to add mostly homescreen IO stuff.
Croquette:
   > Stomping bugs
   > Internet version: On hold until I can make my website less boring/broken.

Offline willrandship

  • Omnimagus of the Multi-Base.
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2953
  • Rating: +98/-13
  • Insert sugar to begin programming subroutine.
    • View Profile
Re: Easy way to make a compiler? :P
« Reply #10 on: February 26, 2011, 11:22:48 pm »
hehe, just figured it out IIRC, it was a ), but thanks! BTW, it's done!