r/Forth • • Oct 20 '17

A javascript forth I'm working on

I play a competitive hacking video called hackmud where you try to outhack each other with javascript. There is a character limit and you have to get a more powerful character with more money to increase the character limit. You start out with around 500 characters so I try to fit more and more into 500 characters as I can.

https://hastebin.com/ekizilacaf.php

This is my current version of a forth runtime. I have not tested this one yet but it should be able to do just about everything that I need it to be able to do.

It's important to be able to run your own version of forth here because it lets you break the character limit by running code from strings, but because you can't trust that another person's forth is not logging what you do in it in their in game database.

The secret to this particular forth is you have to implement a mockup version of the colon compiler without the colon compiler. Also it will have to be handed functions from other programs in order to do certain tasks.

This is not meant to be a particularly fast forth. Just one with as few characters as possible.

edit: most current version https://hastebin.com/utoxecoxen.pl

1 Upvotes

16 comments sorted by

5

u/_crc Oct 20 '17

Can you provide an example of how to call this?

3

u/asrpo Oct 20 '17

I have not tested this one yet

I don't think this thing is close to running. I'm not sure if it was ever run. Here's an attempted deobfuscation.

function(next, input_str, _input_list, _dict, _pop, fetch, _push, _word){
    input_list = input_str.str.split(/\s/)
    dict = []
    pop = x=>x.pop()
    fetch=>pop(dict) //Does nothing. Defines an unused lambda and don't bind it?
    push = x=>dict.push(x)
    dict.a = x=>push(pop(input_list))
    dict.b = x=>fetch()[fetch()]=fetch()
    dict.c = x=>fetch()[fetch()]
    dict.d = x=>push([next, input_str, input_list, dict])

    while(dict.r!=0){
      word = dict.a();
      if (is_func(word)) {
        word()
      } else if (is_num(parseInt(word))) {
        push(parseInt(word))
      } else {
        input_list = input_list.concat(dict[word])}
    }
    return dict.s
}

Function parameters starting with an underscore are unused by the function and overwritten by a local variable in the original post!

1

u/[deleted] Oct 21 '17

function(context, args, terms, dictionary, pop, stack_pop, stack_push,){

terms = args.script.split(/\s/)

dictionary = []

pop = x=>x.pop()

stack_pop=x=>pop(dictionary) 

stack_push= x=>dictionary.push(x)

dictionary["next-word"] = x=>dictionary.push(pop(terms))

dictionary["!"] = x=>stack_pop()()[stack_pop()]=stack_pop()

dictionary["@"] = x=>stack_pop()[stack_pop()]

dictionary["state-pointers"] = x=>stack_push([context, args, terms, dictoinary])

while(dictionary.r!=0){
  word = dictionary["next-word"]();
  if (is_func(word)) {
    word()
  } else if (is_num(parseInt(word))) {
    push(parseInt(word))
  } else {
    input_list = input_list.concat(dictionary[word])}
}
return dictionary.s

}

3

u/dlyund Oct 20 '17

I guess the take away from this is that you can write a Forth with as few characters as you like if you don't require it to, you know, work ;-)

1

u/[deleted] Oct 20 '17

You have to pass the function an object with a key of s as the second argument. And s has to contain a string of some forth script.

The reason I do it that way is so that it works with the video game.

2

u/_crc Oct 20 '17

I gave this a shot. A few observations:

  • I have to bind the function() to a name; I used z.

  • I tried:

    z(0,{'s': '1 2 +'}, 0, 0, 0, 0, 0);

And get an error:

Uncaught ReferenceError: x is not defined
    at z (<anonymous>:16:16)
    at <anonymous>:1:1
  • At this point I looked over the source a bit; I don't see anywhere that x is defined, so I'm giving up.

1

u/[deleted] Oct 21 '17

I haven't run it yet I'm still building it. I've only run each of the pieces in testing. Also check my latest version which I will post shortly after this reply.

4

u/_crc Oct 21 '17

Given that it's not complete and requires a non-standard environment as part of a closed source game I have no interest in I'll disregard it for now.

1

u/[deleted] Oct 21 '17

also keep in mind my code uses the function is_func which is specific to the game hackmud.

6

u/dlyund Oct 20 '17

It would be really great if you could at least try to run the code before you declare this as anything. Judging by the state of your code it looks like you spent all of 5 minutes thinking about this.

2

u/[deleted] Oct 21 '17

Actually a few months I'm afraid.

1

u/dlyund Oct 21 '17

Ah well, you have to start somewhere, and I have to admire your persistence :-)

1

u/petrus4 Oct 20 '17

I'm not sure why this has received so many downvotes. I upvoted, if it means anything.

1

u/[deleted] Oct 20 '17

You working on any programming projects these days Petrus?

2

u/petrus4 Oct 20 '17

Nothing specific, but I'm currently reading Chuck Moore's book, Programming a Problem Oriented Language. I'd started reading it before, but didn't go that far into it. I'm surprised how much I can understand, but in most languages even doing relatively basic things like checking if each character is a space is beyond me. I'd know how to do that in shell script, yes; but not in something like C or Java.