Lobbotomized the codebase

This commit is contained in:
Sasha Koshka 2022-09-13 16:31:08 -04:00
parent 21ea27791d
commit 767bf81b85
11 changed files with 286 additions and 413 deletions

View File

@ -35,39 +35,16 @@ func (parser *ParsingOperation) parseDataSection () (
return
}
if parser.token.Is(lexer.TokenKindNewline) {
err = parser.nextToken()
if err != nil { return }
// check if data is external
if parser.token.Is(lexer.TokenKindName) &&
parser.token.Value().(string) == "external" {
// check if external
if !parser.token.Is(lexer.TokenKindIndent) { return }
if parser.token.Value().(int) != 1 { return }
err = parser.nextToken()
if err != nil { return }
if parser.token.Is(lexer.TokenKindName) &&
parser.token.Value().(string) == "external" {
section.external = true
err = parser.nextToken(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
return
}
// otherwise, parse initialization values
parser.previousToken()
section.value, err = parser.parseDefaultValues(0)
if err != nil { return }
} else {
section.value, err = parser.parseArgument()
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)
section.external = true
err = parser.nextToken(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
return
}
return
}

180
parser/default-values.go Normal file
View File

@ -0,0 +1,180 @@
package parser
//
// import "git.tebibyte.media/arf/arf/lexer"
// import "git.tebibyte.media/arf/arf/infoerr"
//
// // TODO:
// // (parser *ParsingOperation) parseDefaultValues
//
// // (parser *ParsingOperation) parseDefaultMemberValues (return tree of new members and a tree of member values)
// // (parser *ParsingOperation) parseDefaultArrayValues
//
// // (parser *ParsingOperation) parseDefaultMemberValue
// // (parser *ParsingOperation) parseMemberDeclaration
//
// // parsedefaultValues starts on the line after a = phrase, a data section, a
// // type section, or anywhere else where there can be a default value. It returns
// // a default value in the form of an argument, as well as any defined members
// // that it finds.
// func (parser *ParsingOperation) parseDefaultValues (
// baseIndent int,
// ) (
// argument Argument,
// members []TypeMember,
// err error,
// ) {
// // check if line is indented one more than baseIndent
// if !parser.token.Is(lexer.TokenKindIndent) { return }
// if parser.token.Value().(int) != baseIndent + 1 { return }
//
// argument.location = parser.token.Location()
//
// err = parser.nextToken()
// if err != nil { return }
//
// if parser.token.Is(lexer.TokenKindDot) {
//
// // object initialization
// parser.previousToken()
// var values ObjectDefaultValues
// values, err = parser.parseObjectdefaultValues()
// argument.kind = ArgumentKindObjectDefaultValues
// argument.value = values
//
// } else {
//
// // array initialization
// parser.previousToken()
// var values ArrayDefaultValues
// values, err = parser.parseArrayDefaultValues()
// argument.kind = ArgumentKindArrayDefaultValues
// argument.value = values
// }
//
// return
// }
//
// // parseObjectdefaultValues parses a list of object initialization
// // values until the indentation level drops.
// func (parser *ParsingOperation) parseObjectdefaultValues () (
// defaultValues ObjectDefaultValues,
// err error,
// ) {
// defaultValues.attributes = make(map[string] Argument)
//
// baseIndent := 0
// begin := true
//
// for {
// // if there is no indent we can just stop parsing
// if !parser.token.Is(lexer.TokenKindIndent) { break}
// indent := parser.token.Value().(int)
//
// if begin == true {
// defaultValues.location = parser.token.Location()
// baseIndent = indent
// begin = false
// }
//
// // do not parse any further if the indent has changed
// if indent != baseIndent { break }
//
// // move on to the beginning of the line, which must contain
// // a member initialization value
// err = parser.nextToken(lexer.TokenKindDot)
// if err != nil { return }
// err = parser.nextToken(lexer.TokenKindName)
// if err != nil { return }
// name := parser.token.Value().(string)
//
// // if the member has already been listed, throw an error
// _, exists := defaultValues.attributes[name]
// if exists {
// err = parser.token.NewError (
// "duplicate member \"" + name + "\" in object " +
// "member initialization",
// infoerr.ErrorKindError)
// return
// }
//
// // parse the argument determining the member initialization
// // value
// err = parser.nextToken()
// if err != nil { return }
// var value Argument
// if parser.token.Is(lexer.TokenKindNewline) {
//
// // recurse
// err = parser.nextToken(lexer.TokenKindIndent)
// if err != nil { return }
//
// value, err = parser.parseDefaultValues(baseIndent)
// defaultValues.attributes[name] = value
// if err != nil { return }
//
// } else {
//
// // parse as normal argument
// value, err = parser.parseArgument()
// defaultValues.attributes[name] = value
// if err != nil { return }
//
// err = parser.expect(lexer.TokenKindNewline)
// if err != nil { return }
// err = parser.nextToken()
// if err != nil { return }
// }
// }
//
// return
// }
//
// // parseArrayDefaultValues parses a list of array initialization values until
// // the indentation lexel drops.
// func (parser *ParsingOperation) parseArrayDefaultValues () (
// defaultValues ArrayDefaultValues,
// err error,
// ) {
// baseIndent := 0
// begin := true
//
// for {
// // if there is no indent we can just stop parsing
// if !parser.token.Is(lexer.TokenKindIndent) { break}
// indent := parser.token.Value().(int)
//
// if begin == true {
// defaultValues.location = parser.token.Location()
// baseIndent = indent
// begin = false
// }
//
// // do not parse any further if the indent has changed
// if indent != baseIndent { break }
//
// // move on to the beginning of the line, which must contain
// // arguments
// err = parser.nextToken(validArgumentStartTokens...)
// if err != nil { return }
//
// for {
// // stop parsing this line and go on to the next if a
// // newline token is encountered
// if parser.token.Is(lexer.TokenKindNewline) {
// err = parser.nextToken()
// if err != nil { return }
// break
// }
//
// // otherwise, parse the argument
// var argument Argument
// argument, err = parser.parseArgument()
// if err != nil { return }
// defaultValues.values = append (
// defaultValues.values,
// argument)
// }
// }
//
// return
// }

View File

@ -73,22 +73,13 @@ func (parser *ParsingOperation) parseEnumMembers (
if err != nil { return }
// parse default value
if parser.token.Is(lexer.TokenKindNewline) {
err = parser.nextToken()
if err != nil { return }
member.value, err = parser.parseArgument()
into.members = append(into.members, member)
if err != nil { return }
member.value, err = parser.parseDefaultValues(1)
into.members = append(into.members, member)
if err != nil { return }
} else {
member.value, err = parser.parseArgument()
into.members = append(into.members, member)
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
}
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
}
}

View File

@ -187,33 +187,10 @@ func (parser *ParsingOperation) parseFuncArguments (
output.what, err = parser.parseType()
if err != nil { return }
// skip the default value if we are skimming
if parser.skimming {
err = parser.skipIndentLevel(2)
into.outputs = append(into.outputs, output)
return
}
// parse default value
if parser.token.Is(lexer.TokenKindNewline) {
err = parser.nextToken()
if err != nil { return }
output.value, err =
parser.parseDefaultValues(1)
into.outputs = append(into.outputs, output)
if err != nil { return }
} else {
output.value, err =
parser.parseArgument()
into.outputs = append(into.outputs, output)
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
}
parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
}
}
}

View File

@ -1,179 +0,0 @@
package parser
import "git.tebibyte.media/arf/arf/lexer"
import "git.tebibyte.media/arf/arf/infoerr"
// TODO:
// (parser *ParsingOperation) parseDefaultValues
// (parser *ParsingOperation) parseDefaultMemberValues (return tree of new members and a tree of member values)
// (parser *ParsingOperation) parseDefaultArrayValues
// (parser *ParsingOperation) parseDefaultMemberValue
// (parser *ParsingOperation) parseMemberDeclaration
// parsedefaultValues starts on the line after a data section, or a set
// phrase. It checks for an indent greater than the indent of the aforementioned
// data section or set phrase (passed through baseIndent), and if there is,
// it parses initialization values.
func (parser *ParsingOperation) parseDefaultValues (
baseIndent int,
) (
argument Argument,
err error,
) {
// check if line is indented one more than baseIndent
if !parser.token.Is(lexer.TokenKindIndent) { return }
if parser.token.Value().(int) != baseIndent + 1 { return }
argument.location = parser.token.Location()
err = parser.nextToken()
if err != nil { return }
if parser.token.Is(lexer.TokenKindDot) {
// object initialization
parser.previousToken()
var values ObjectDefaultValues
values, err = parser.parseObjectdefaultValues()
argument.kind = ArgumentKindObjectDefaultValues
argument.value = values
} else {
// array initialization
parser.previousToken()
var values ArrayDefaultValues
values, err = parser.parseArrayDefaultValues()
argument.kind = ArgumentKindArrayDefaultValues
argument.value = values
}
return
}
// parseObjectdefaultValues parses a list of object initialization
// values until the indentation level drops.
func (parser *ParsingOperation) parseObjectdefaultValues () (
defaultValues ObjectDefaultValues,
err error,
) {
defaultValues.attributes = make(map[string] Argument)
baseIndent := 0
begin := true
for {
// if there is no indent we can just stop parsing
if !parser.token.Is(lexer.TokenKindIndent) { break}
indent := parser.token.Value().(int)
if begin == true {
defaultValues.location = parser.token.Location()
baseIndent = indent
begin = false
}
// do not parse any further if the indent has changed
if indent != baseIndent { break }
// move on to the beginning of the line, which must contain
// a member initialization value
err = parser.nextToken(lexer.TokenKindDot)
if err != nil { return }
err = parser.nextToken(lexer.TokenKindName)
if err != nil { return }
name := parser.token.Value().(string)
// if the member has already been listed, throw an error
_, exists := defaultValues.attributes[name]
if exists {
err = parser.token.NewError (
"duplicate member \"" + name + "\" in object " +
"member initialization",
infoerr.ErrorKindError)
return
}
// parse the argument determining the member initialization
// value
err = parser.nextToken()
if err != nil { return }
var value Argument
if parser.token.Is(lexer.TokenKindNewline) {
// recurse
err = parser.nextToken(lexer.TokenKindIndent)
if err != nil { return }
value, err = parser.parseDefaultValues(baseIndent)
defaultValues.attributes[name] = value
if err != nil { return }
} else {
// parse as normal argument
value, err = parser.parseArgument()
defaultValues.attributes[name] = value
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
}
}
return
}
// parseArrayDefaultValues parses a list of array initialization values until
// the indentation lexel drops.
func (parser *ParsingOperation) parseArrayDefaultValues () (
defaultValues ArrayDefaultValues,
err error,
) {
baseIndent := 0
begin := true
for {
// if there is no indent we can just stop parsing
if !parser.token.Is(lexer.TokenKindIndent) { break}
indent := parser.token.Value().(int)
if begin == true {
defaultValues.location = parser.token.Location()
baseIndent = indent
begin = false
}
// do not parse any further if the indent has changed
if indent != baseIndent { break }
// move on to the beginning of the line, which must contain
// arguments
err = parser.nextToken(validArgumentStartTokens...)
if err != nil { return }
for {
// stop parsing this line and go on to the next if a
// newline token is encountered
if parser.token.Is(lexer.TokenKindNewline) {
err = parser.nextToken()
if err != nil { return }
break
}
// otherwise, parse the argument
var argument Argument
argument, err = parser.parseArgument()
if err != nil { return }
defaultValues.values = append (
defaultValues.values,
argument)
}
}
return
}

View File

@ -1,74 +1,6 @@
package parser
import "git.tebibyte.media/arf/arf/lexer"
import "git.tebibyte.media/arf/arf/infoerr"
// parseType parses a type notation of the form Name, {Name}, etc.
func (parser *ParsingOperation) parseType () (what Type, err error) {
err = parser.expect(lexer.TokenKindName, lexer.TokenKindLBrace)
if err != nil { return }
what.location = parser.token.Location()
if parser.token.Is(lexer.TokenKindLBrace) {
what.kind = TypeKindPointer
err = parser.nextToken()
if err != nil { return }
var points Type
points, err = parser.parseType()
if err != nil { return }
what.points = &points
err = parser.expect (
lexer.TokenKindRBrace,
lexer.TokenKindElipsis)
if err != nil { return }
if parser.token.Is(lexer.TokenKindElipsis) {
what.kind = TypeKindVariableArray
err = parser.nextToken(lexer.TokenKindRBrace)
if err != nil { return }
}
err = parser.nextToken()
if err != nil { return }
} else {
what.name, err = parser.parseIdentifier()
if err != nil { return }
}
for {
if !parser.token.Is(lexer.TokenKindColon) { break }
err = parser.nextToken(lexer.TokenKindName, lexer.TokenKindUInt)
if err != nil { return }
if parser.token.Is(lexer.TokenKindName) {
// parse type qualifier
qualifier := parser.token.Value().(string)
switch qualifier {
case "mut":
what.mutable = true
default:
err = parser.token.NewError (
"unknown type qualifier \"" +
qualifier + "\"",
infoerr.ErrorKindError)
return
}
} else {
// parse fixed array length
what.length = parser.token.Value().(uint64)
}
err = parser.nextToken()
if err != nil { return }
}
return
}
// parseIdentifier parses an identifier made out of dot separated names.
func (parser *ParsingOperation) parseIdentifier () (

View File

@ -213,18 +213,6 @@ func (parser *ParsingOperation) parseBlockLevelPhrase (
err = parser.nextToken()
if err != nil { return }
// if this is a set phrase, parse initialization values under it
if phrase.kind == PhraseKindAssign {
var values Argument
values, err = parser.parseDefaultValues(indent)
if values.kind != ArgumentKindNil {
phrase.arguments = append(phrase.arguments, values)
}
return
}
// if this is a control flow phrase, parse block under it
isControlFlow := false
for _, kind := range controlFlowKinds {

View File

@ -88,6 +88,7 @@ func (what Type) ToString () (output string) {
output += ":mut"
}
// TODO: print out default value
return
}
@ -260,29 +261,7 @@ func (section DataSection) ToString (indent int) (output string) {
"type ",
section.permission.ToString(), " ",
section.name, ":",
section.what.ToString())
isComplexInitialization :=
section.value.kind == ArgumentKindObjectDefaultValues ||
section.value.kind == ArgumentKindArrayDefaultValues
if !isComplexInitialization && section.value.value != nil {
output += " " + section.value.ToString(0, false)
}
output += "\n"
for _, member := range section.what.members {
output += member.ToString(indent + 1)
}
if isComplexInitialization {
output += section.value.ToString(indent + 1, true)
}
if section.external {
output += "\n"
output += doIndent(indent + 1, "external\n")
}
section.what.ToString(), "\n")
return
}
@ -308,24 +287,7 @@ func (section TypeSection) ToString (indent int) (output string) {
"type ",
section.permission.ToString(), " ",
section.name, ":",
section.what.ToString())
isComplexInitialization :=
section.value.kind == ArgumentKindObjectDefaultValues ||
section.value.kind == ArgumentKindArrayDefaultValues
if !isComplexInitialization && section.value.value != nil {
output += " " + section.value.ToString(0, false)
}
output += "\n"
for _, member := range section.what.members {
output += member.ToString(indent + 1)
}
if isComplexInitialization {
output += section.value.ToString(indent + 1, true)
}
section.what.ToString(), "\n")
return
}
@ -443,24 +405,9 @@ func (block Block) ToString (indent int) (output string) {
}
func (funcOutput FuncOutput) ToString (indent int) (output string) {
output += doIndent(indent + 1, "< ", funcOutput.Declaration.ToString())
isComplexInitialization :=
funcOutput.value.kind == ArgumentKindObjectDefaultValues ||
funcOutput.value.kind == ArgumentKindArrayDefaultValues
if !isComplexInitialization && funcOutput.value.value != nil {
output += " " + funcOutput.value.ToString(0, false)
}
output += "\n"
for _, member := range funcOutput.what.members {
output += member.ToString(indent + 1)
}
if isComplexInitialization {
output += funcOutput.value.ToString(indent + 1, true)
}
output += doIndent (
indent + 1,
"< ", funcOutput.Declaration.ToString(), "\n")
return
}

View File

@ -64,7 +64,6 @@ type TypeMember struct {
nameable
typeable
permissionable
valuable
bitWidth uint64
}
@ -84,7 +83,10 @@ type Type struct {
points *Type
// if non-nil, this type defines new members.
members []TypeMember
members []TypeMember
// the default value of the type.
defaultValue Argument
}
// Declaration represents a variable declaration.
@ -180,7 +182,6 @@ type DataSection struct {
nameable
typeable
permissionable
valuable
external bool
}
@ -191,7 +192,6 @@ type TypeSection struct {
nameable
typeable
permissionable
valuable
}
// EnumMember represents a member of an enum section.
@ -270,7 +270,6 @@ type Block []Phrase
// that it can have a default value.
type FuncOutput struct {
Declaration
valuable
}
// FuncSection represents a function section.

72
parser/type-notation.go Normal file
View File

@ -0,0 +1,72 @@
package parser
import "git.tebibyte.media/arf/arf/lexer"
import "git.tebibyte.media/arf/arf/infoerr"
// parseType parses a type notation of the form Name, {Name}, etc.
func (parser *ParsingOperation) parseType () (what Type, err error) {
err = parser.expect(lexer.TokenKindName, lexer.TokenKindLBrace)
if err != nil { return }
what.location = parser.token.Location()
if parser.token.Is(lexer.TokenKindLBrace) {
what.kind = TypeKindPointer
err = parser.nextToken()
if err != nil { return }
var points Type
points, err = parser.parseType()
if err != nil { return }
what.points = &points
err = parser.expect (
lexer.TokenKindRBrace,
lexer.TokenKindElipsis)
if err != nil { return }
if parser.token.Is(lexer.TokenKindElipsis) {
what.kind = TypeKindVariableArray
err = parser.nextToken(lexer.TokenKindRBrace)
if err != nil { return }
}
err = parser.nextToken()
if err != nil { return }
} else {
what.name, err = parser.parseIdentifier()
if err != nil { return }
}
for {
if !parser.token.Is(lexer.TokenKindColon) { break }
err = parser.nextToken(lexer.TokenKindName, lexer.TokenKindUInt)
if err != nil { return }
if parser.token.Is(lexer.TokenKindName) {
// parse type qualifier
qualifier := parser.token.Value().(string)
switch qualifier {
case "mut":
what.mutable = true
default:
err = parser.token.NewError (
"unknown type qualifier \"" +
qualifier + "\"",
infoerr.ErrorKindError)
return
}
} else {
// parse fixed array length
what.length = parser.token.Value().(uint64)
}
err = parser.nextToken()
if err != nil { return }
}
return
}

View File

@ -32,21 +32,10 @@ func (parser *ParsingOperation) parseTypeSection () (
section.what, err = parser.parseType()
if err != nil { return }
// parse default values
if parser.token.Is(lexer.TokenKindNewline) {
err = parser.nextToken()
if err != nil { return }
parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
section.value, err = parser.parseDefaultValues(0)
if err != nil { return }
} else {
section.value, err = parser.parseArgument()
if err != nil { return }
err = parser.expect(lexer.TokenKindNewline)
if err != nil { return }
err = parser.nextToken()
if err != nil { return }
}
return
}