mediawiki-extensions-AbuseF.../parser_native/expr.cpp
River Tarnell bbf1737cc2 - convert parser to AST. while this is slower than the previous implementation, it's clearer, and more correct (lazy evaluation is supported)
- to compensate for the slower parser, optimised it a little
- change basic_string to fray, a refcounted immutable string
- add 'xml' tool, prints the parser tree in XML
2008-08-10 03:52:17 +00:00

61 lines
1.6 KiB
C++

/*
* Copyright (c) 2008 Andrew Garrett.
* Copyright (c) 2008 River Tarnell <river@wikimedia.org>
* Derived from public domain code contributed by Victor Vasiliev.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely. This software is provided 'as-is', without any express or
* implied warranty.
*/
#include <iostream>
#include "parser.h"
#include "afstring.h"
template<typename charT>
afp::basic_datum<charT>
f_add(std::vector<afp::basic_datum<charT> > const &args)
{
return args[0] + args[1];
}
template<typename charT>
afp::basic_datum<charT>
f_norm(std::vector<afp::basic_datum<charT> > const &args)
{
return args[0];
}
template<typename charT>
afp::basic_datum<charT>
f_length(std::vector<afp::basic_datum<charT> > const &args)
{
return afp::basic_datum<charT>::from_int(args[0].toString().size());
}
int
main(int argc, char **argv)
{
if (argc != 2) {
std::cerr << boost::format("usage: %s <expr>\n")
% argv[0];
return 1;
}
afp::u32expressor e;
e.add_variable(make_u32fray("ONE"), afp::u32datum::from_int(1));
e.add_variable(make_u32fray("TWO"), afp::u32datum::from_int(2));
e.add_variable(make_u32fray("THREE"), afp::u32datum::from_int(3));
e.add_function(make_u32fray("add"), f_add<UChar32>);
e.add_function(make_u32fray("norm"), f_norm<UChar32>);
e.add_function(make_u32fray("length"), f_length<UChar32>);
try {
std::cout << make_u8fray(e.evaluate(make_u32fray(argv[1])).toString()) << '\n';
} catch (std::exception &e) {
std::cout << "parsing failed: " << e.what() << '\n';
}
}