错误消息“定义一个类型,一个或多个......”

时间:2013-01-02 13:47:01

标签: f#

我正在学习F#,我从另一个人那里得到了一些代码。 但是当我尝试编译代码时,出现了错误。 完整代码如下:

module touchdev_ast
open FParsec
type Name = string
type Operator =
| Plus | Minus | Slash | Asterisk
| LessOrEqual | Less | Equal | Unequal | GreaterOrEqual | Greater
| Assignment // :=
| And | Or | Not
| Arrow // −>
| Concat // ||

type Parameter = Name (*param name*) * Name (*type qualifier*)

这里(下面一行)发生错误!!

type Expression =
public
| Nothing
| Float of float
| Boolean of bool
| Variable of string
| String of string
| BinaryOperation of Operator * Expression * Expression
| FunctionCall of Name (*fun name*) * Expression list (*arguments*)

and Statement =
public
| Block of Statement list
| Expression of Expression
| Assignment of Expression list (*multiple assignment*) * Expression
| If of Expression * Statement (*then*) * Statement option (*else*)
| ForI of Name (*loop variant*) * Expression (*upper bound*) * Statement
| ForEach of Name (*iterator*) * Expression (*collection*) * Expression option (*condition*) * Statement
| While of Expression * Statement
| MetaStatement of Name

and public FunctionType =
Name (*fun name*) * Parameter list option (*input values*)* Parameter list option (*return values*) * Statement

and TopLevel =
public
| MetaDeclaration of Name (*type*) * string (*value*)
| Action of FunctionType
| Event of FunctionType
| Global of Parameter * Statement (*options*)

type CodeEntry =
public
| Namespace of string (* name *) * bool (* singleton *) * CodeEntry list option (* properties *) * string option (* help *)
| Property of string (* name *) * bool (* public *) * Parameter list option (* input *) * Parameter list option (* return values *) * string option (* help *)
| Variable of string (* name *) * bool (* assignable *) * bool (* initialized *) * string (* type *) * string option (* help *)
| BinaryOperator of string (* name *) * string (* type left *) * string (* type right *) * string (* type return *) * string option (* help *)
| Meta of string (* name *) * string (* value *)

请帮我修复bug! :d 在此先感谢!!

1 个答案:

答案 0 :(得分:3)

缩进在F#中很重要 - 因此您需要缩进您声明的每种联合类型的大小写声明。此外,您不需要public修饰符,因为默认情况下公开(这与您的错误无关)。

module touchdev_ast
open FParsec
type Name = string
type Operator =
    | Plus | Minus | Slash | Asterisk
    | LessOrEqual | Less | Equal | Unequal | GreaterOrEqual | Greater
    | Assignment // :=
    | And | Or | Not
    | Arrow // −>
    | Concat // ||

type Parameter = Name (*param name*) * Name (*type qualifier*)

type Expression =
    | Nothing
    | Float of float
    | Boolean of bool
    | Variable of string
    | String of string
    | BinaryOperation of Operator * Expression * Expression
    | FunctionCall of Name (*fun name*) * Expression list (*arguments*)

and Statement =
    | Block of Statement list
    | Expression of Expression
    | Assignment of Expression list (*multiple assignment*) * Expression
    | If of Expression * Statement (*then*) * Statement option (*else*)
    | ForI of Name (*loop variant*) * Expression (*upper bound*) * Statement
    | ForEach of Name (*iterator*) * Expression (*collection*) * Expression option (*condition*) * Statement
    | While of Expression * Statement
    | MetaStatement of Name

and public FunctionType =
    Name (*fun name*) * Parameter list option (*input values*)* Parameter list option (*return values*) * Statement

and TopLevel =
    | MetaDeclaration of Name (*type*) * string (*value*)
    | Action of FunctionType
    | Event of FunctionType
    | Global of Parameter * Statement (*options*)

type CodeEntry =
    | Namespace of string (* name *) * bool (* singleton *) * CodeEntry list option (* properties *) * string option (* help *)
    | Property of string (* name *) * bool (* public *) * Parameter list option (* input *) * Parameter list option (* return values *) * string option (* help *)
    | Variable of string (* name *) * bool (* assignable *) * bool (* initialized *) * string (* type *) * string option (* help *)
    | BinaryOperator of string (* name *) * string (* type left *) * string (* type right *) * string (* type return *) * string option (* help *)
    | Meta of string (* name *) * string (* value *)