コード例 #1
0
ファイル: linker.go プロジェクト: gsrpc/gslang
func (linker *_Linker) linkConstantRef(script *ast.Script, constantRef *ast.ConstantRef) {

	nodes := strings.Split(constantRef.Name(), ".")

	if len(nodes) < 2 {
		linker.errorf(ErrTypeNotFound, constantRef, "unknown constant val (%s)", constantRef.Name())
		return
	}

	name := nodes[len(nodes)-1]

	typeRef := ast.NewTypeRef(strings.Join(nodes[0:len(nodes)-1], "."))

	start, end := Pos(constantRef)

	_setNodePos(typeRef, start, end)

	linker.linkTypeRef(script, typeRef)

	if typeRef.Ref != nil {
		enum := typeRef.Ref.(*ast.Enum)

		for _, constant := range enum.Constants {

			if constant.Name() == name {
				constantRef.Value = constant
				return
			}
		}

		linker.errorf(ErrVariableName, constantRef, "unknown enum(%s) constant filed :%s", enum, name)
	}

}
コード例 #2
0
ファイル: parser.go プロジェクト: gsrpc/gslang
func (parser *Parser) expectTypeDecl(fmtstring string, args ...interface{}) (typeDecl ast.Type) {

	msg := fmt.Sprintf(fmtstring, args...)

	for {
		token := parser.peek()

		switch token.Type {
		case lexer.KeyByte, lexer.KeySByte, lexer.KeyInt16, lexer.KeyUInt16,
			lexer.KeyInt32, lexer.KeyUInt32, lexer.KeyInt64, lexer.KeyUInt64,
			lexer.KeyFloat32, lexer.KeyFloat64, lexer.KeyString, lexer.KeyBool, lexer.KeyVoid:

			typeDecl = ast.NewBuiltinType(token.Type)

			_setNodePos(typeDecl, token.Start, token.End)

			parser.next()

		case lexer.TokenID:
			name, star, end := parser.expectFullName("expect type declare")

			typeDecl = ast.NewTypeRef(name)

			_setNodePos(typeDecl, star, end)

		default:
			parser.errorf(token.Start, "%s\n\tunexpect token %s", msg, token)
			continue
		}

		for {

			if seqType, ok := parser.parseSeq(typeDecl); ok {
				typeDecl = seqType
				continue
			}

			break
		}

		return
	}

}