Hello,

I am trying to write a simple assembler file parser. I just started developing 
in Go so I have the wrong mindset. I am keen to understand the best way to 
write something like this in Go.

An assembler file at first glance is a list of instructions, directives and 
labels. I know there are some other things but for the purposes of the example, 
that's enough.

So I have

type AsmFile list.List

The elements of the list are the problematic part. It's well... a union.
I have read on this and I have seen suggested an interface approach, where you 
can then cast to the right type.

The entries of the list above are AsmEntry

type AsmEntry interface {
    IsInstruction() bool
    IsLabel() bool
    IsDirective() bool
}

The problem is that I start then with:

type Label string

func (Label) IsLabel() bool {
    return true
}

func (Label) IsInstruction() bool {
    return false
}

-- same for IsDirective

but then an instruction should be an interface since there could be 
ArmInstruction, IntelInstruction, etc

type Instruction interface {
    GetMnemonic() string
    GetArgsLen() int
    GetArg(int) string
}

func (Instruction) IsInstruction() bool {
    return true
}

but this doesn't actually work because the receiver cannot be an instruction. 
This already looks like I am going on the wrong path.
Any suggestions on how to design something like this with Go?

Kind regards,


-- 
Paulo Matos

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to