| 17 | ) |
| 18 | |
| 19 | func validate(oracle *pb.Oracle) (call string, args []string, err error) { |
| 20 | var prototype *ast.FunctionDeclaration |
| 21 | // first try to parse the oracle and validate that |
| 22 | // it starts with a function declaration |
| 23 | program, err := parser.ParseFile(nil, "", oracle.Code, 0) |
| 24 | ok := true |
| 25 | if err != nil { |
| 26 | return "", nil, err |
| 27 | } else if program.DeclarationList == nil || len(program.DeclarationList) < 1 { |
| 28 | return "", nil, errNoDeclarations |
| 29 | } else if prototype, ok = program.DeclarationList[0].(*ast.FunctionDeclaration); !ok { |
| 30 | return "", nil, fmt.Errorf("expected function declaration, found %T", program.DeclarationList[0]) |
| 31 | } |
| 32 | |
| 33 | // use the function declaration in order to build the function call |
| 34 | args = []string{} |
| 35 | if prototype.Function.ParameterList != nil && prototype.Function.ParameterList.List != nil { |
| 36 | args = make([]string, len(prototype.Function.ParameterList.List)) |
| 37 | for i, param := range prototype.Function.ParameterList.List { |
| 38 | args[i] = param.Name |
| 39 | } |
| 40 | } |
| 41 | call = fmt.Sprintf("%s(%s)", |
| 42 | prototype.Function.Name.Name, |
| 43 | strings.Join(args, ",")) |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | // Compiles a raw oracle. |
| 48 | func compile(oracle *pb.Oracle) (*compiled, error) { |