Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track the buffer and slot name each variable refers to #64

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions actr/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func (model Model) LookupChunk(chunkName string) *Chunk {
return nil
}

// SlotName returns the name of the slot given the index.
func (c Chunk) SlotName(index int) (str string) {
return c.SlotNames[index]
}

func (c Chunk) IsInternal() bool {
return c.Name[0] == '_'
}
Expand Down
9 changes: 9 additions & 0 deletions actr/production.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ type Production struct {
Name string
Description *string // optional description to output as a comment in the generated code

VarIndexMap map[string]VarIndex // track the buffer and slot name each variable refers to

Matches []*Match
DoStatements []*Statement

AMODLineNumber int // line number in the amod file of the this production
}

// VarIndex is used to track which buffer slot a variable refers to
type VarIndex struct {
Var string
Buffer BufferInterface
SlotName string
}

type Match struct {
Buffer BufferInterface
Pattern *Pattern
Expand Down
26 changes: 23 additions & 3 deletions amod/amod.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ func addProductions(model *actr.Model, log *amodlog.Log, productions *production
prod := actr.Production{
Name: production.Name,
Description: production.Description,
VarIndexMap: map[string]actr.VarIndex{},
AMODLineNumber: production.Pos.Line,
}

Expand All @@ -384,12 +385,31 @@ func addProductions(model *actr.Model, log *amodlog.Log, productions *production
}

name := item.Name

prod.Matches = append(prod.Matches, &actr.Match{
match := actr.Match{
Buffer: model.LookupBuffer(name),
Pattern: pattern,
})
}

prod.Matches = append(prod.Matches, &match)

for index, slot := range pattern.Slots {
item := slot.Items[0]

if item.Var == nil {
continue
}

// Track the buffer and slot name the variable refers to
varItem := *item.Var
if _, ok := prod.VarIndexMap[varItem]; !ok {
varIndex := actr.VarIndex{
Var: varItem,
Buffer: match.Buffer,
SlotName: pattern.Chunk.SlotName(index),
}
prod.VarIndexMap[varItem] = varIndex
}
}
}

if production.Do.Statements != nil {
Expand Down
5 changes: 2 additions & 3 deletions amod/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func validateSetStatement(set *setStatement, model *actr.Model, log *amodlog.Log

for slotIndex, slot := range set.Pattern.Slots {
if len(slot.Items) > 1 {
log.Error(set.Pattern.Pos.Line, "cannot set '%s.%v' to compound var in production '%s'", bufferName, chunk.SlotNames[slotIndex], production.Name)
log.Error(set.Pattern.Pos.Line, "cannot set '%s.%v' to compound var in production '%s'", bufferName, chunk.SlotName(slotIndex), production.Name)
err = CompileError{}

continue
Expand All @@ -193,8 +193,7 @@ func validateSetStatement(set *setStatement, model *actr.Model, log *amodlog.Log
match := production.LookupMatchByVariable(varItem)
if match == nil {
if varItem == "?" {

log.Error(set.Pattern.Pos.Line, "cannot set '%s.%v' to anonymous var ('?') in production '%s'", bufferName, chunk.SlotNames[slotIndex], production.Name)
log.Error(set.Pattern.Pos.Line, "cannot set '%s.%v' to anonymous var ('?') in production '%s'", bufferName, chunk.SlotName(slotIndex), production.Name)
} else {
log.Error(set.Pattern.Pos.Line, "set statement variable '%s' not found in matches for production '%s'", varItem, production.Name)
}
Expand Down