toctave

t(iny)octave
git clone https://0xff.ir/g/toctave.git
Log | Files | Refs | README

ast.cpp (2100B)


      1 // AST (Abstract Syntax Tree) types and functions
      2 
      3 // Copyright (C) 2022, Mohammad-Reza Nabipoor
      4 // SPDX-License-Identifier: GPL-3.0-or-later
      5 
      6 #include "ast.hpp"
      7 
      8 namespace ast {
      9 
     10 std::ostream&
     11 operator<<(std::ostream& os, const Num& n)
     12 {
     13   os << "Num{ " << n.num << " }";
     14   return os;
     15 }
     16 
     17 std::ostream&
     18 operator<<(std::ostream& os, const Op& o)
     19 {
     20   os << "Op{ " << o.op << " }";
     21   return os;
     22 }
     23 
     24 std::ostream&
     25 operator<<(std::ostream& os, const Id& i)
     26 {
     27   os << "Id{ " << i.id << " }";
     28   return os;
     29 }
     30 
     31 std::ostream&
     32 operator<<(std::ostream& os, const Str& s)
     33 {
     34   os << "Str{ \"" << s.str << "\" }";
     35   return os;
     36 }
     37 
     38 std::ostream&
     39 operator<<(std::ostream& os, const Ass& a)
     40 {
     41   os << "Ass{ " << a.id << " }";
     42   return os;
     43 }
     44 
     45 std::ostream&
     46 operator<<(std::ostream& os, const Call& c)
     47 {
     48   os << "Call{ " << c.id << " }";
     49   return os;
     50 }
     51 
     52 std::ostream&
     53 operator<<(std::ostream& os, const Node& n)
     54 {
     55   if (std::holds_alternative<Num>(n))
     56     os << std::get<Num>(n);
     57   else if (std::holds_alternative<Op>(n))
     58     os << std::get<Op>(n);
     59   else if (std::holds_alternative<Id>(n))
     60     os << std::get<Id>(n);
     61   else if (std::holds_alternative<Str>(n))
     62     os << std::get<Str>(n);
     63   else if (std::holds_alternative<Ass>(n))
     64     os << std::get<Ass>(n);
     65   else if (std::holds_alternative<Call>(n))
     66     os << std::get<Call>(n);
     67   return os;
     68 }
     69 
     70 Nodes
     71 mknum(double n)
     72 {
     73   Nodes f;
     74 
     75   f.insert(f.end(), Num{ n });
     76   return f;
     77 }
     78 
     79 Nodes
     80 mkop(Op op, Nodes lhs, Nodes rhs)
     81 {
     82   Nodes f;
     83   auto it{ stlab::trailing_of(f.insert(f.end(), std::move(op))) };
     84 
     85   f.splice(it, lhs);
     86   f.splice(it, rhs);
     87   return f;
     88 }
     89 
     90 Nodes
     91 mkid(Id id)
     92 {
     93   Nodes f;
     94 
     95   f.insert(f.end(), std::move(id));
     96   return f;
     97 }
     98 
     99 Nodes
    100 mkstr(Str str)
    101 {
    102   Nodes f;
    103 
    104   f.insert(f.end(), std::move(str));
    105   return f;
    106 }
    107 
    108 Nodes
    109 mkass(Id id, Nodes expr)
    110 {
    111   Nodes f;
    112   auto it{ stlab::trailing_of(f.insert(f.end(), ast::Ass{ std::move(id) })) };
    113 
    114   f.splice(it, expr);
    115   return f;
    116 }
    117 
    118 Nodes
    119 mkcall(Id id, Nodes args)
    120 {
    121   Nodes f;
    122   auto it{ stlab::trailing_of(f.insert(f.end(), ast::Call{ std::move(id) })) };
    123 
    124   f.splice(it, args);
    125   return f;
    126 }
    127 
    128 } // namespace ast