-4

Scratch is made from flash. It isn't coding. But python is (or at least I think it is ). So I was wondering if it was possible to

a) Make a coding language in python for the user to make

b) Make the User's code executable.

P.S. Is there a way for variables when changed by the user, becomes changed in your code? I need to know this for something.

yolo
  • 127
  • 6
  • Do you mean [`eval`](https://docs.python.org/3/library/functions.html#eval)? In theory you can write a compiler/interpreter for whatever language you want (even a custom new one), but that's probably too much. – Norrius Feb 21 '18 at 19:59
  • 1
    Of course you can implement a programming language in Python, but it is a major undertaking – John Coleman Feb 21 '18 at 19:59
  • I've never heard of it – yolo Feb 21 '18 at 20:00
  • And John, has it been done? – yolo Feb 21 '18 at 20:00
  • You can implement a programming language in Scratch if you try hard enough. But yes, it is possible. – Splinxyy Feb 21 '18 at 20:00
  • I was talking about python. I was using scratch as an example because it is a language in flash – yolo Feb 21 '18 at 20:01
  • Yes, it has been done. Research "Domain Specific Languages" for ideas on how to implement mini-languages. You have to learn about parsing theory (among other things), which is non-trivial. – John Coleman Feb 21 '18 at 20:01
  • John, do you mind putting this in an answer (to be ticked) because I feel I'll receive more -1s if I keep letting this drag on. – yolo Feb 21 '18 at 20:02
  • I'm not exactly sure what you are asking. It is possible to implement a programming language in Python. I'm not sure what you mean by "for a user to make". – juanpa.arrivillaga Feb 21 '18 at 20:02
  • I think if you describe your immediate goals more clearly we'll be able to help you better. Building a full-featured language is an enormous undertaking that takes teams of experts many years. But there might be much simpler ways to do what you want, if you tell us what that is. – Personman Feb 21 '18 at 20:03
  • I am voting to close the question as too broad, since, well, it is. – John Coleman Feb 21 '18 at 20:03
  • 1
    @Personman or one person about a week if you want Javascript :) – juanpa.arrivillaga Feb 21 '18 at 20:04
  • I'm not planning on taking this on. I was just curious if it was possible, although could you help me out on the last question – yolo Feb 21 '18 at 20:04
  • @yolo your last question is very unclear. Please see [ask] – juanpa.arrivillaga Feb 21 '18 at 20:06
  • 6
    Just because Scratch is implemented using Flash doesn't mean that a) it is not a programming language or b) that is *has* to be implemented in Flash. What distinction do you draw between typing `while` in the correct place and dragging a pre-typed "while" block into place? – chepner Feb 21 '18 at 20:08
  • Your code says x=5 - next line - the answer to my question . The user types in 3, and when you return to edit your code you see x=3 instead of x=5 – yolo Feb 21 '18 at 20:08
  • Try this: `x=int(input())` – whackamadoodle3000 Feb 21 '18 at 20:09
  • @yolo please elaborate *in the question itself*. Regardless, I'm not sure why you cannot accomplish this using `input` – juanpa.arrivillaga Feb 21 '18 at 20:09
  • Simple, you don't have to remember the important things like putting the colon at the end or being limited by a drop-down menu on what is the condition. – yolo Feb 21 '18 at 20:10
  • Or you could open the file for reading and writing, and then write the variable – whackamadoodle3000 Feb 21 '18 at 20:10
  • Basically, imagine creating a code for your computer to look for prime numbers. Because there is no formula for it your code cannot do this without looking through testing every number. What I'm looking for is a way for the code to 'remember', in an array, previously found prime numbers the last time you opened the file so that instead of testing it can see if that number exists in an array. The irony is that when I explain things like this on mathematics people get confused – yolo Feb 21 '18 at 20:13
  • Look below... You can write this array to the same file in a variable – whackamadoodle3000 Feb 21 '18 at 20:14
  • 3
    @yolo that doesn't sound like writing a language at all. You are looking for data persistence. There are many ways to do this, from a simple text-file to more robust serialization methods(JSON, `pickle` etc) to full-fledge database management systems. – juanpa.arrivillaga Feb 21 '18 at 20:18
  • Won't work because you can't search through a text file in above situation to find previously found prime numbers – yolo Feb 21 '18 at 20:21
  • Why not? Just save the previously found prime number to a file, and then read the file at the beginning of your code. One helpful library for this would be `pickle` – whackamadoodle3000 Feb 21 '18 at 20:22
  • 1
    @yolo of course you can. Why do you believe you wouldn't be able to? – juanpa.arrivillaga Feb 21 '18 at 20:23
  • Any simpler ways???. I was just thinking it would be saimpler if it just appended an extra slot in an array every time it found a new one. I'm not exactly pro-python dude. I only know the import randint library – yolo Feb 21 '18 at 20:24
  • 1
    @yolo you *can* append to a list. But that list exists in memory, and won't persist after your program is done executing. For that, you would need require *data persistence*. But perhaps you don't, and you only mean during a single execution of your program. But *again*, there are *many different ways todo this*. But this question has **nothing** to do with creating a language in Python. – juanpa.arrivillaga Feb 21 '18 at 20:27
  • It was a *side question*. Anyway, so you are saying that there is no way my list can be [2,3,5,7] and after looking for more and going back to the editor it being [2,3,5,7,11] – yolo Feb 21 '18 at 20:29
  • You absolutely can do that, but it is a really, truly terrible idea. Look up the `pickle` library and use that to save and load your array from an external file. – Personman Feb 21 '18 at 20:30
  • I've looked it up and it is crazily confusing, but why would it be a bad idea? Because all the new data is in the array to be used next time you execute o_0 – yolo Feb 21 '18 at 20:31
  • You want to keep code and data separated. Period. – Matthias Feb 21 '18 at 21:11
  • Why though. And isn't a variable a peice of data??? – yolo Feb 21 '18 at 21:31
  • It's a bad practice in general because it violates a number of useful properties (like determinism, generality, reusability) for no gain. Building good habits even with simple programs will pay off for you in the long run. But a more satisfying answer right now might be: it'll be WAY easier to do it with pickle. – Personman Feb 21 '18 at 21:42
  • it's just: import pickle; arr = [1,2,3]; pickle.dump(arr, open('filename', 'w')); loaded_arr = pickle.load(open('filename', 'r')); print loaded_arr # [1,2,3] – Personman Feb 21 '18 at 21:44
  • Or if you REALLY wanted to change the code, despite the warnings above, you can try the answer below – whackamadoodle3000 Feb 22 '18 at 19:44
  • I've tried to do it in Scratch before, and failed miserably. This is because of the lank of functionality in Scratch, causing major problems, where in programming languages like C++ and Python, I wouldn't even have to _touch_ on the problem. – Xetrov Feb 23 '18 at 16:38

1 Answers1

0
def Write(linenumber,file,content):
        ile=open(file,'r')
        filer=ile.readlines()
        ile.close
        file1=open(file,'w')
        linenumber=linenumber-1
        loop=-1
        for elem in filer:
            loop=loop+1
            if loop!=linenumber:
                file1.write(elem)
            else:
                file1.write(content)
def Read(linenumber,file):
          ile=open(file,'r')
          lines=ile.readlines()
          ile.close()
          return lines[linenumber-1]

For your second question, you could do something like this to write to the same file, if you want the change to be permanent. Just use the functions above to change the variable permanently. You could also write to a separate file which would be more ideal with the functions.

whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44
  • I ran the code and it went straight to this >>>. – yolo Feb 21 '18 at 20:15
  • You need the use the functions to write and read. Also don't use the shell for this – whackamadoodle3000 Feb 21 '18 at 20:16
  • What do you mean by that? – yolo Feb 21 '18 at 20:16
  • Save the code to a file. Then at the end of the code, call the function by using `Write(linenumber,file,content)`. Replace linenumber with the linenumber (probably the end of the file), file with the filename, and content with the line of code that you want to write (your variable) – whackamadoodle3000 Feb 21 '18 at 20:18
  • Oh, so I'm saving text to a file!!! Brilliant, but not in my case because I'm sending it to another computer, meaning that I'd have to send the file too – yolo Feb 21 '18 at 20:19
  • 2
    @yolo if your program needs some sort of data to function correctly, you don't have a choice but to send that data. I'm not sure exactly what sort of solution you are expecting, but this answer has little to do with the question you've asked. – juanpa.arrivillaga Feb 21 '18 at 20:23
  • Yeh, but the question I've asked hasn't been answered in an answer area so I can't click on the tick yet, can I? – yolo Feb 21 '18 at 20:26