1#import <Foundation/Foundation.h>
2#import <ANTLR/ANTLR.h>
3#import "PolyLexer.h"
4#import "PolyParser.h"
5// #import "PolyDifferentiator.h"
6// #import "PolyPrinter.h"
7// #import "Simplifier.h"
8
9
10int main(int argc, const char *argv[])
11{
12    NSError *error;
13    NSLog(@"starting polydiff\n");
14	NSString *input = [NSString stringWithContentsOfFile:@"../../examples/polydiff/input"  encoding:NSASCIIStringEncoding error:&error];
15	ANTLRStringStream *stream = [ANTLRStringStream newANTLRStringStream:input];
16	NSLog(@"%@", input);
17
18// BUILD AST
19    PolyLexer *lex = [PolyLexer newPolyLexerWithCharStream:stream];
20    CommonTokenStream *tokens = [CommonTokenStream newCommonTokenStreamWithTokenSource:lex];
21    PolyParser *parser = [PolyParser newPolyParser:tokens];
22    PolyParser_poly_return *r = [parser poly];
23    NSLog(@"tree=%@", [r.tree toStringTree]);
24
25#ifdef DONTUSENOMO
26// DIFFERENTIATE
27    CommonTreeNodeStream *nodes = [CommonTreeNodeStream newCommonTreeNodeStream:r.tree];
28    [nodes setTokenStream:tokens];
29    PolyDifferentiator *differ = [PolyDifferentiator newPolyDifferentiator:nodes];
30    PolyDifferentiator_poly_return *r2 = [differ poly];
31    NSLog("d/dx=%@", [r2.tree toStringTree]);
32
33// SIMPLIFY / NORMALIZE
34    nodes = [CommonTreeNodeStream newCommonTreeNodeStream:r2.tree];
35    [nodes setTokenStream:tokens];
36    Simplifier *reducer = [Simplifier newSimplifier:nodes];
37    Simplifier_poly_return *r3 = [reducer poly];
38    NSLog("simplified=%@", [r3.tree toStringTree]);
39
40// CONVERT BACK TO POLYNOMIAL
41    nodes = [CommonTreeNodeStream newCommonTreeNodeStream:r3.tree];
42    [nodes setTokenStream:tokens];
43    PolyPrinter *printer = [PolyPrinter newPolyPrinter:nodes];
44    PolyPrinter_poly_return *r4 = [printer poly];
45    NSLog( [r4.st toString]);
46#endif
47
48    NSLog(@"exiting PolyDiff\n");
49    return 0;
50}
51