0

我正在学习 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 *)

请帮我修复错误!:D 提前谢谢!!

4

1 回答 1

3

缩进在 F# 中很重要——因此您需要为您声明的每个联合类型缩进 case 声明。此外,您不需要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 *)
于 2013-01-02T13:52:34.740 回答