help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Weird Rule Matching


From: Tom
Subject: Re: Weird Rule Matching
Date: Sat, 9 Apr 2022 10:11:08 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0

Thanks a lot for the example, that's pretty close to how I modeled my 
instructions, I think I'm am just storing them slightly weirdly.

Think I am finally getting the hang of it...

On 07/04/2022 14:18, Hans Åberg wrote:
On 7 Apr 2022, at 12:00, tom2 <tom@tomflux.xyz> wrote:

I have heard of an AST, and, against my better judgement, thought they were to 
complex for my needs and decided to represent the instructions as one long 
list, that gets edited by loops/conditionals.

I see the error of my ways now, but I am too close to the deadline of this 
project for me to go back and change it now...
Here is a functional style outline in terms of C++:

First a base class with a virtual function evaluate(), say:
class unit {
   ref<unit> evaluate(ref<unit>) { return {}; }
};
where ref<X> is say the C++ reference counting class std::shared_ptr<X>.

Then
class function_application : public virtual unit {
     object f_, a_;   // Function and arguments.
ref<unit> evaluate(ref<unit> x) { return f_(a_(x)); }
};

In addition, classes for functions and tuples.

When constructed, this will delay any action until applies 'evaluate' to the 
object.

For an if-then-else conditional, it might have an boolean argument b, and an 
argument to evaluate if true t and false f_
class if_then_else {
    object b_, t_, f_;

    ref<unit> evaluate(ref<unit> x) { if (b_(x)) return t_(x) else return 
f_(x); }
};

For loops, it is possible in C++ to implement break and continue objects that 
throw exceptions to achieve the appropriate effect. This is not particularly 
efficient, but easy to write.

There are more expanded and efficient ways to do this, but the underlying idea 
would be the same.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]