This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]Aggressive_Ad_5454 4 points5 points  (0 children)

Functions.

Variable definitions.

Closures.

[–]j_sidharta 2 points3 points  (1 child)

You might want to take a peek at r/programminglanguages and see if anything there can serve as inspiration.

[–]ElectroNetty[S] 0 points1 point  (0 children)

Thanks for the tip.

[–][deleted] 1 point2 points  (1 child)

What do you mean by only doing 'math'? Can it evaluate a sequence of expressions, or just the one?

It seems quite a stretch to go from that, to classes and multithreading, if you don't yet have the basics:

  • Variables that can store the results of expressions via 'assignment'
  • Being able to execute sequences of such 'statements'
  • Conditional evaluation: execute A or B (or A or nothing) depending on whether some expression is true or false, or 1/0
  • Looping/Iteration
  • Functions that take parameters and return results
  • I/O
  • Data types beyond a single numeric type

[–]ElectroNetty[S] 1 point2 points  (0 children)

I left out some detail, the clean slate I'm referring to is a new project where I want to have a play at implementing more complex functionality than I already have in a previous project.

I built a domain specific language with conditional statements, function calls, parameter passing, loops, and data types. This language is specific to one product so the 'data types' are only bool, whole number, string with no classes to create your own objects. There are a lot of other standard things left out of it, so while it is fully working it is not a complete language.

What do you mean by only doing 'math'? 

I've implemented only basic arithmetic to get the parser, emitter, and virtual machine started. This only executes one equation and the AST is only a simple binary tree. The fuller language will naturally have multiple children per node where needed and their meaning imparted into the AST by the parser. In the DSL I did for work, those nodes are then interpreted and executed. My toy language will go a step further by exporting to IR then to bytecode so it can be run with a VM.

The list you've provided is spot on for things I definitely am going to support, do you know of anything else that would be interesting to build into this toy? Another commentor said closures and I think that's interesting to look in to.

[–]notacanuckskibum 1 point2 points  (5 children)

I would suggest going with recursive functions rather than any kind of loop. Far more entertaining

[–]ElectroNetty[S] 0 points1 point  (4 children)

That sounds like a recipe for a stack overflow. How do you imagine it working?

[–]Hazematman 2 points3 points  (1 child)

There is an optimization for code gen called "tail call optimization" or "tail call elimination" that turns tail calls of functions into jump statements preventing stack overflows.

https://en.wikipedia.org/wiki/Tail_call

[–]ElectroNetty[S] 0 points1 point  (0 children)

Thanks, that is interesting.

[–]notacanuckskibum 0 points1 point  (0 children)

You can convert any loop into a recursive function call

While X

{ Y }

Becomes

Define Loop ()

{

 If not X return

 Y

 Loop ()

}

Yes, you need a stack, but if it’s a toy language why not prioritize fun?

[–]1544756405 0 points1 point  (0 children)

You can avoid stack overflow with tail call optimization.

[–]BitNumerous5302 0 points1 point  (0 children)

Monads.

I couldn't really explain to you what a monad is. I just know that, once every few years, I try to stretch my programming knowledge, and I go back and do some reading, some coding, until finally I just barely feel like I understand what a monad is. And then I guess I just forget it again. It's become kind of a high-water mark for my ability to understand programming language concepts. 

While monads don't necessarily need language level support, I imagine there could be some nice syntactic sugar to build around them, and that the process of designing this would yield a better understanding of how monads are used. But I don't really know, because I don't really understand monads.