using GLib; using Gee; // valac --pkg gee-1.0 -o blah blah.vala public class Foo: Object { public static int main() { Gee.List? terms = split_in_terms ("(2+a)*d+(a*b-3)-4-q/2"); if(null == terms) { stdout.printf("Error parsing string!\n"); return 1; } foreach (string term in terms) { stdout.printf("Got term \"%s\"\n", term); } return 0; } private static Gee.List? split_in_terms(string strmath) { int x = 0; int parentesis=0; ArrayList list = new ArrayList(); string str = strmath; StringBuilder term = new StringBuilder(""); int p = 0; stdout.printf("str = %s\n", str); for(weak string s = str; s.get_char()!=0 ; s = s.next_char()) { unichar c = s.get_char(); if ( c == '(') { parentesis++; term.append_unichar(c); } else if ( c == ')') { parentesis--; term.append_unichar(c); } else if ( c == '+') { if ( parentesis == 0) { list.add( term.str ); print("%s\n",term.str); term = new StringBuilder(""); p = x+1; if ( str.len() >= p ) list.add( "+" ); } } else { term.append_unichar(c); } x++; } list.add( term.str ); if (parentesis != 0) { stdout.printf("SintaxERROR: Los parentesis no cierran adecuadamente: %s\n", str); return null; } return list; } }// Foo