Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
21 May, 2013, 09:07:06 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Easy way to make a compiler? :P -  (Read 775 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
willrandship
Omnimagus of the Multi-Base.
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 22:45:51
Date Registered: 11 April, 2010, 03:08:32
Location: Between Venus and Mars
Posts: 2638


Topic starter
Total Post Ratings: +66

View Profile
« on: 25 February, 2011, 08:17:08 »
0

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 Tongue) 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.
Logged

Michael_Lee
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 09 August, 2012, 18:48:39
Date Registered: 05 August, 2010, 01:00:06
Posts: 1020

Total Post Ratings: +115

View Profile
« Reply #1 on: 25 February, 2011, 08:39:03 »
0

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: 25 February, 2011, 08:41:22 by Michael_Lee » Logged

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.
AngelFish
This is my custom title
Administrator
LV12 Extreme Poster (Next: 5000)
*
Offline Offline

Gender: Male
Last Login: 18 May, 2013, 00:41:29
Date Registered: 15 August, 2010, 09:18:54
Posts: 3187


Total Post Ratings: +218

View Profile
« Reply #2 on: 25 February, 2011, 08:42:14 »
0


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? Tongue
« Last Edit: 25 February, 2011, 08:42:25 by Qwerty.55 » Logged

∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ
willrandship
Omnimagus of the Multi-Base.
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 22:45:51
Date Registered: 11 April, 2010, 03:08:32
Location: Between Venus and Mars
Posts: 2638


Topic starter
Total Post Ratings: +66

View Profile
« Reply #3 on: 26 February, 2011, 02:29:31 »
0

Yes, that's bad Tongue this isn't hex it's compiling.

@Micheal that would be nice. Smiley The format can be pretty much anything, nothing's set in stone since the processor doesn't exist yet Tongue
Logged

Michael_Lee
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 09 August, 2012, 18:48:39
Date Registered: 05 August, 2010, 01:00:06
Posts: 1020

Total Post Ratings: +115

View Profile
« Reply #4 on: 26 February, 2011, 02:54:52 »
0

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).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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: 26 February, 2011, 02:56:53 by Michael_Lee » Logged

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.
willrandship
Omnimagus of the Multi-Base.
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 22:45:51
Date Registered: 11 April, 2010, 03:08:32
Location: Between Venus and Mars
Posts: 2638


Topic starter
Total Post Ratings: +66

View Profile
« Reply #5 on: 26 February, 2011, 04:17:48 »
0

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. Smiley
Logged

Michael_Lee
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 09 August, 2012, 18:48:39
Date Registered: 05 August, 2010, 01:00:06
Posts: 1020

Total Post Ratings: +115

View Profile
« Reply #6 on: 26 February, 2011, 05:06:44 »
0

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).
Logged

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.
willrandship
Omnimagus of the Multi-Base.
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 22:45:51
Date Registered: 11 April, 2010, 03:08:32
Location: Between Venus and Mars
Posts: 2638


Topic starter
Total Post Ratings: +66

View Profile
« Reply #7 on: 26 February, 2011, 05:37:56 »
0

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.
Logged

willrandship
Omnimagus of the Multi-Base.
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 22:45:51
Date Registered: 11 April, 2010, 03:08:32
Location: Between Venus and Mars
Posts: 2638


Topic starter
Total Post Ratings: +66

View Profile
« Reply #8 on: 27 February, 2011, 05:43:51 »
0

Graagh, this is giving me a syntax error.....

source.close()

and I dont know why.....
Logged

Michael_Lee
LV9 Veteran (Next: 1337)
*********
Offline Offline

Gender: Male
Last Login: 09 August, 2012, 18:48:39
Date Registered: 05 August, 2010, 01:00:06
Posts: 1020

Total Post Ratings: +115

View Profile
« Reply #9 on: 27 February, 2011, 06:00:12 »
0

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?
Logged

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.
willrandship
Omnimagus of the Multi-Base.
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 19 May, 2013, 22:45:51
Date Registered: 11 April, 2010, 03:08:32
Location: Between Venus and Mars
Posts: 2638


Topic starter
Total Post Ratings: +66

View Profile
« Reply #10 on: 27 February, 2011, 06:22:48 »
0

hehe, just figured it out IIRC, it was a ), but thanks! BTW, it's done!
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.279 seconds with 32 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.