Example #1
0
func (codegen *_CodeGen) typeName(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)

		return builtin[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.typeName(typeRef.Ref)

	case *ast.Enum, *ast.Table:
		_, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if gslang.IsException(typeDecl) {
			return exception(name)
		}

		return name

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		return fmt.Sprintf("%s[]", codegen.typeName(seq.Component))
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Example #2
0
func (codegen *_CodeGen) defaultVal(typeDecl ast.Type) string {

	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return defaultval[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.defaultVal(typeRef.Ref)

	case *ast.Enum:

		enum := typeDecl.(*ast.Enum)

		_, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())
		//
		// if prefix != "" {
		// 	return prefix + "." + name + "." + enum.Constants[0].Name()
		// }

		return name + "." + enum.Constants[0].Name()

	case *ast.Table:

		return "new " + codegen.typeName(typeDecl) + "()"

	case *ast.Seq:
		return fmt.Sprintf("new %s", codegen.arrayDefaultVal(typeDecl))
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Example #3
0
func (codegen *_CodeGen) objTypeName(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)

		return builtinObj[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.typeName(typeRef.Ref)

	case *ast.Enum, *ast.Table:
		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + "." + name
		}

		return name

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		return fmt.Sprintf("%s[]", codegen.typeName(seq.Component))
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Example #4
0
func (codegen *_CodeGen) defaultVal(typeDecl ast.Type) string {

	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return defaultval[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.defaultVal(typeRef.Ref)

	case *ast.Enum:

		enum := typeDecl.(*ast.Enum)

		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + "." + name + "" + enum.Constants[0].Name()
		}

		return name + "" + enum.Constants[0].Name()

	case *ast.Table:

		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + ".New" + name + "()"
		}

		return "New" + name + "()"

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		if seq.Size != -1 {

			var buff bytes.Buffer

			if err := codegen.tpl.ExecuteTemplate(&buff, "create_array", seq); err != nil {
				gserrors.Panicf(err, "exec template(create_array) for %s errir", seq)
			}

			return buff.String()
		}

		return "nil"
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}
Example #5
0
func (codegen *_CodeGen) readType(typeDecl ast.Type) string {
	switch typeDecl.(type) {
	case *ast.BuiltinType:
		builtinType := typeDecl.(*ast.BuiltinType)
		return readMapping[builtinType.Type]
	case *ast.TypeRef:
		typeRef := typeDecl.(*ast.TypeRef)

		return codegen.readType(typeRef.Ref)

	case *ast.Enum:
		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return prefix + ".Read" + name
		}

		return "Read" + name

	case *ast.Table:

		prefix, name := codegen.typeRef(typeDecl.Package(), typeDecl.FullName())

		if prefix != "" {
			return "" + prefix + ".Read" + name
		}

		return "Read" + name

	case *ast.Seq:
		seq := typeDecl.(*ast.Seq)

		var buff bytes.Buffer

		isbytes := false

		builtinType, ok := seq.Component.(*ast.BuiltinType)

		if ok && builtinType.Type == lexer.KeyByte {
			isbytes = true
		}

		if seq.Size != -1 {

			if isbytes {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readByteArray", seq); err != nil {
					gserrors.Panicf(err, "exec template(readByteArray) for %s errir", seq)
				}
			} else {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readArray", seq); err != nil {
					gserrors.Panicf(err, "exec template(readArray) for %s errir", seq)
				}
			}

		} else {

			if isbytes {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readByteList", seq); err != nil {
					gserrors.Panicf(err, "exec template(readByteList) for %s errir", seq)
				}
			} else {
				if err := codegen.tpl.ExecuteTemplate(&buff, "readList", seq); err != nil {
					gserrors.Panicf(err, "exec template(readList) for %s errir", seq)
				}
			}

		}

		return buff.String()
	}

	gserrors.Panicf(nil, "typeName  error: unsupport type(%s)", typeDecl)

	return "unknown"
}