Exemplo n.º 1
0
func (codegen *_CodeGen) BeginScript(compiler *gslang.Compiler, script *ast.Script) bool {

	codegen.compiler = compiler

	scriptPath := filepath.ToSlash(filepath.Clean(script.Name()))

	for _, skip := range codegen.skips {

		if skip.MatchString(scriptPath) {
			return false
		}
	}

	if strings.HasPrefix(script.Package, "gslang") {
		return false
	}

	codegen.header.Reset()
	codegen.source.Reset()
	codegen.predecl.Reset()

	codegen.script = script

	codegen.imports = make(map[string]string)

	for k, v := range imports {
		codegen.imports[k] = v
	}

	return true
}
Exemplo n.º 2
0
func (linker *_Linker) linkTypeRef(script *ast.Script, typeRef *ast.TypeRef) {

	linkedType, ok := script.Type(typeRef.Name())

	if ok {
		typeRef.Ref = linkedType

		linker.D("found type %s", linkedType)

		return
	}

	linkedType, ok = linker.importTypes[typeRef.Name()]

	if ok {
		typeRef.Ref = linkedType

		linker.D("found import types %s", linkedType)

		return
	}

	linkedType, ok = linker.types[typeRef.Name()]

	if ok {
		typeRef.Ref = linkedType

		linker.D("found import types %s", linkedType)

		return
	}

	linker.errorf(ErrTypeNotFound, typeRef, "unknown type reference :%s", typeRef)
}
Exemplo n.º 3
0
func (linker *_Linker) linkTypes(script *ast.Script) {
	linker.D("create using symbol table for script : %s", script)

	linker.importTypes = make(map[string]ast.Type)

	script.UsingForeach(func(using *ast.Using) {

		name := path.Base(strings.Replace(using.Name(), ".", "/", -1))

		linker.D("link using(%s) : %s", using, name)

		if gslangType, ok := linker.types[using.Name()]; ok {
			linker.importTypes[name] = gslangType
			using.Ref = gslangType
			linker.D("link using(%s:%p) : %s -- success", using, using, name)
			return
		}

		linker.errorf(ErrTypeNotFound, using, "using statment reference unknown type(%s)", using.Name())
	})

	linker.D("create using symbol table for script : %s -- success", script)

	script.TypeForeach(func(gslangType ast.Type) {

		linker.linkType(script, gslangType)
	})
}
Exemplo n.º 4
0
func (codegen *_CodeGen) BeginScript(compiler *gslang.Compiler, script *ast.Script) bool {

	scriptPath := filepath.ToSlash(filepath.Clean(script.Name()))

	for _, skip := range codegen.skips {

		if skip.MatchString(scriptPath) {

			return false
		}
	}

	if strings.HasPrefix(script.Package, "gslang.") {
		return false
	}

	codegen.header.Reset()
	codegen.content.Reset()

	codegen.script = script

	codegen.packageName = script.Package
	codegen.scriptPath = strings.Replace(codegen.packageName, ".", "/", -1)

	langs := gslang.FindAnnotations(script.Module, "gslang.Package")

	for _, lang := range langs {

		langName, ok := lang.Args.NamedArg("Lang")

		if ok && compiler.Eval().EvalString(langName) == "golang" {

			packageName, ok := lang.Args.NamedArg("Name")

			if ok && compiler.Eval().EvalString(packageName) == script.Package {

				redirect, ok := lang.Args.NamedArg("Redirect")

				if ok {
					codegen.packageName = compiler.Eval().EvalString(redirect)
					codegen.scriptPath = codegen.packageName
				}
			}

		}
	}

	path := strings.Replace(codegen.packageName, ".", "/", -1)
	codegen.header.WriteString(fmt.Sprintf("package %s\n\n", filepath.Base(path)))

	codegen.imports = make(map[string]string)

	for k, v := range imports {
		codegen.imports[k] = v
	}

	return true
}
Exemplo n.º 5
0
func (linker *_Linker) createSymbolTable(script *ast.Script) {

	linker.D("create global symoble table , search script defined types: %s", script)

	script.TypeForeach(func(gslangType ast.Type) {
		fullname := _fullName(script.Package, gslangType)

		if previous, ok := linker.types[fullname]; ok {
			linker.duplicateTypeDef(gslangType, previous)
			return
		}

		linker.types[fullname] = gslangType
	})

	linker.D("create global symoble table , search script defined types: %s -- success", script)
}