help-bison
[Top][All Lists]
Advanced

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

Re: Weird Rule Matching


From: Hans Åberg
Subject: Re: Weird Rule Matching
Date: Thu, 7 Apr 2022 15:18:05 +0200

> 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]