Among impostor

This commit is contained in:
Sasha Koshka 2022-09-14 15:16:56 -04:00
parent a9f8881eb4
commit 558542127d
3 changed files with 156 additions and 55 deletions

View File

@ -83,7 +83,7 @@ type Type struct {
points *Type points *Type
// if non-nil, this type defines new members. // if non-nil, this type defines new members.
members []TypeMember members []TypeMember
// the default value of the type. // the default value of the type.
defaultValue Argument defaultValue Argument
@ -98,16 +98,10 @@ type Declaration struct {
// ObjectDefaultValues represents a list of object member initialization // ObjectDefaultValues represents a list of object member initialization
// attributes. // attributes.
type ObjectDefaultValues struct { type ObjectDefaultValues map[string] Argument
locatable
attributes map[string] Argument
}
// ArrayDefaultValues represents a list of elements initializing an array. // ArrayDefaultValues represents a list of elements initializing an array.
type ArrayDefaultValues struct { type ArrayDefaultValues []Argument
locatable
values []Argument
}
// ArgumentKind specifies the type of thing the value of an argument should be // ArgumentKind specifies the type of thing the value of an argument should be
// cast to. // cast to.

View File

@ -42,7 +42,11 @@ func (parser *ParsingOperation) parseType () (what Type, err error) {
for { for {
if !parser.token.Is(lexer.TokenKindColon) { break } if !parser.token.Is(lexer.TokenKindColon) { break }
err = parser.nextToken(lexer.TokenKindName, lexer.TokenKindUInt) err = parser.nextToken(
lexer.TokenKindName,
lexer.TokenKindUInt,
lexer.TokenKindLParen,
lexer.TokenKindLessThan)
if err != nil { return } if err != nil { return }
if parser.token.Is(lexer.TokenKindName) { if parser.token.Is(lexer.TokenKindName) {
@ -58,28 +62,27 @@ func (parser *ParsingOperation) parseType () (what Type, err error) {
infoerr.ErrorKindError) infoerr.ErrorKindError)
return return
} }
} else { } else if parser.token.Is(lexer.TokenKindUInt) {
// parse fixed array length // parse fixed array length
what.length = parser.token.Value().(uint64) what.length = parser.token.Value().(uint64)
} else if parser.token.Is(lexer.TokenKindLessThan) {
// parse default value
what.defaultValue, err = parser.parseBasicDefaultValue()
if err != nil { return }
} else if parser.token.Is(lexer.TokenKindLParen) {
// parse members and member default values
what.defaultValue,
what.members,
err = parser.parseObjectDefaultValueAndMembers()
if err != nil { return }
} }
err = parser.nextToken() err = parser.nextToken()
if err != nil { return } if err != nil { return }
} }
// TODO: consider offloading array default values to argument parsing, and
// then just grabbing the next argument after this if it exists. that way
// we can have array litreals anywhere.
// get default value
if parser.token.Is(lexer.TokenKindLessThan) {
what.defaultValue, err = parser.parseBasicDefaultValue()
if err != nil { return }
} else if parser.token.Is(lexer.TokenKindLParen) {
// TODO: parse members and member default values
}
return return
} }
@ -88,25 +91,21 @@ func (parser *ParsingOperation) parseBasicDefaultValue () (
value Argument, value Argument,
err error, err error,
) { ) {
value.location = parser.token.Location()
err = parser.expect(lexer.TokenKindLessThan) err = parser.expect(lexer.TokenKindLessThan)
if err != nil { return } if err != nil { return }
err = parser.nextToken() err = parser.nextToken()
if err != nil { return } if err != nil { return }
var arguments []Argument var attributes []Argument
defer func () { defer func () {
// if we have multiple values, we need to return the full array // if we have multiple values, we need to return the full array
// instead. // instead.
if len(arguments) > 1 { if len(attributes) > 1 {
// FIXME: i think this is a sign that the tree needs
// cleaning up.
value.kind = ArgumentKindArrayDefaultValues value.kind = ArgumentKindArrayDefaultValues
location := value.location value.value = ArrayDefaultValues(attributes)
value.value = ArrayDefaultValues {
values: arguments,
}
value.location = location
} }
} () } ()
@ -117,31 +116,139 @@ func (parser *ParsingOperation) parseBasicDefaultValue () (
value, err = parser.parseArgument() value, err = parser.parseArgument()
if err != nil { return } if err != nil { return }
arguments = append(arguments, value) attributes = append(attributes, value)
} }
return return
} }
// parseObjectDefaultValue parses default values and new members of an object // parseObjectDefaultValueAndMembers parses default values and new members of an
// type. // object type.
func (parser *ParsingOperation) parseObjectDefaultValue () ( func (parser *ParsingOperation) parseObjectDefaultValueAndMembers () (
value Argument, value Argument,
members []TypeMember, members []TypeMember,
err error, err error,
) { ) {
value.location = parser.token.Location()
err = parser.expect(lexer.TokenKindLParen) err = parser.expect(lexer.TokenKindLParen)
if err != nil { return } if err != nil { return }
parser.nextToken() parser.nextToken()
if err != nil { return } if err != nil { return }
var attributes ObjectDefaultValues
for { for {
if parser.token.Is(lexer.TokenKindIndent) { continue }
if parser.token.Is(lexer.TokenKindNewline) { continue }
if parser.token.Is(lexer.TokenKindRParen) { break }
err = parser.expect(lexer.TokenKindDot) err = parser.expect(lexer.TokenKindDot)
if err != nil { return } if err != nil { return }
parser.nextToken(lexer.TokenKindName, lexer.TokenKindPermission) parser.nextToken(lexer.TokenKindName, lexer.TokenKindPermission)
if err != nil { return } if err != nil { return }
// TODO: if name, parse parent default. if permission, if parser.token.Is(lexer.TokenKindName) {
// parsing a defalut value for an inherited member
var memberName string
var memberValue Argument
memberName,
memberValue, err = parser.parseObjectMemberDefinition()
if err != nil { return }
if value.kind == ArgumentKindNil {
// create default value map if it doesn't
// already exist
value.kind = ArgumentKindObjectDefaultValues
attributes = make(ObjectDefaultValues)
value.value = attributes
}
// TODO: error on duplicate
if memberValue.kind != ArgumentKindNil {
attributes[memberName] = memberValue
}
} else if parser.token.Is(lexer.TokenKindPermission) {
// parsing a member declaration
var member TypeMember
member,
err = parser.parseObjectMemberDeclaration()
members = append(members, member)
}
} }
return return
} }
// parseObjectDefaultValue parses member default values only, and will throw an
// error when it encounteres a new member definition.
func (parser *ParsingOperation) parseObjectDefaultValue () (
value Argument,
err error,
) {
value.location = parser.token.Location()
err = parser.expect(lexer.TokenKindLParen)
if err != nil { return }
parser.nextToken()
if err != nil { return }
var attributes ObjectDefaultValues
for {
if parser.token.Is(lexer.TokenKindIndent) { continue }
if parser.token.Is(lexer.TokenKindNewline) { continue }
if parser.token.Is(lexer.TokenKindRParen) { break }
err = parser.expect(lexer.TokenKindDot)
if err != nil { return }
parser.nextToken(lexer.TokenKindName)
if err != nil { return }
if value.kind == ArgumentKindNil {
value.kind = ArgumentKindObjectDefaultValues
attributes = make(ObjectDefaultValues)
value.value = attributes
}
var memberName string
var memberValue Argument
memberName,
memberValue, err = parser.parseObjectMemberDefinition()
attributes[memberName] = memberValue
}
return
}
// .ro name:Type:qualifier:<value>
// parseObjectMemberDeclaration parses a new default value for an inherited
// member.
func (parser *ParsingOperation) parseObjectMemberDefinition () (
name string,
value Argument,
err error,
) {
err = parser.expect(lexer.TokenKindName)
if err != nil { return }
return
}
// .name:<value>
// parseObjectMemberDeclaration parses an object member declaration, and its
// default value if it exists.
func (parser *ParsingOperation) parseObjectMemberDeclaration () (
member TypeMember,
err error,
) {
err = parser.expect(lexer.TokenKindPermission)
if err != nil { return }
return
}

View File

@ -1,9 +1,9 @@
:arf :arf
--- ---
data ro aInteger:Int 3202 data ro aInteger:Int:<3202>
data ro bMutInteger:Int:mut 3202 data ro bMutInteger:Int:mut:<3202>
data ro cIntegerPointer:{Int} data ro cIntegerPointer:{Int}
@ -13,35 +13,35 @@ data ro eIntegerArray16:Int:16
data ro fIntegerArrayVariable:{Int ..} data ro fIntegerArrayVariable:{Int ..}
data ro gIntegerArrayInitialized:Int:16 data ro gIntegerArrayInitialized:Int:16:<
3948 293 293049 948 912 3948 293 293049 948 912
340 0 2304 0 4785 92 340 0 2304 0 4785 92
>
# TODO: reinstate these two after phrase parsing is implemented # TODO: reinstate these two after phrase parsing is implemented
# data wr hIntegerPointerInit:{Int} [& integer] # data wr hIntegerPointerInit:{Int} [& integer]
# data wr iMutIntegerPointerInit:{Int}:mut [& integer] # data wr iMutIntegerPointerInit:{Int}:mut [& integer]
# TODO: maybe test identifiers somewhere else? data ro jObject:Obj:(
data ro jObject:thing.Thing. .this:<324>
thing.thing .that:<324>)
-- this 324
-- that 2139
data ro kNestedObject:Obj data ro kNestedObject:Obj:(
-- this .this:(
-- bird0 324 .bird0:<324>
-- bird1 "hello world" .bird1:<"hello world">)
ro newMember:Int 9023 .ro newMember:Int:<9023>
-- that .that:(
-- bird2 123.8439 .bird2:<123.8439>
-- bird3 9328.21348239 .bird3:<9328.21348239>))
data ro lMutIntegerArray16:Int:16:mut data ro lMutIntegerArray16:Int:16:mut
data ro mExternalData:Int:8 data ro mExternalData:Int:8
external external
data ro nIntegerArrayInitialized:Int:16:mut data ro nIntegerArrayInitialized:Int:16:mut:<
3948 293 293049 948 912 3948 293 293049 948 912
340 0 2304 0 4785 92 340 0 2304 0 4785 92
>