MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / type

Method type

chapter23/src/main/java/com/seaofnodes/simple/Parser.java:898–958  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

896
897 // t = int|i8|i16|i32|i64|u8|u16|u32|u64|byte|bool | flt|f32|f64 | val | var | struct[?]
898 private Type type() {
899 int old1 = pos();
900 // Only type with a leading `{` is a function pointer...
901 if( peek('{') ) return typeFunPtr();
902
903 // Otherwise you get a type name
904 String tname = _lexer.matchId();
905 if( tname==null ) return null;
906
907 // Convert the type name to a type.
908 Type t0 = TYPES.get(tname);
909 // No new types as keywords
910 if( t0 == null && KEYWORDS.contains(tname) )
911 return posT(old1);
912 if( t0 == Type.BOTTOM || t0 == Type.TOP ) return t0; // var/val type inference
913
914 // Check for subtype.
915 while( true ) {
916 int old2 = pos();
917 if( !match(".") ) break;
918 String sname = _lexer.matchId();
919 if( sname==null ) { pos(old2); break; }
920 String tsname = tname+"."+sname;
921 t0 = TYPES.get(tsname);
922 if( t0==null ) { pos(old2); t0 = TYPES.get(tname); break; }
923 tname = tsname;
924 }
925
926 // Still no type found? Assume forward reference
927 Type t1 = t0 == null ? TypeMemPtr.make(TypeStruct.open(tname)) :t0; // Null: assume a forward ref type
928 // Nest arrays and '?' as needed
929 Type t2 = t1;
930 while( true ) {
931 if( match("?") ) {
932 if( !(t2 instanceof TypeMemPtr tmp) )
933 throw error("Type "+t0+" cannot be null");
934 if( tmp.nullable() ) throw error("Type "+t2+" already allows null");
935 t2 = tmp.makeNullable();
936 } else if( match("[~]") ) {
937 t2 = typeAry(t2,true);
938 } else if( match("[]") ) {
939 t2 = typeAry(t2,false);
940 } else
941 break;
942 }
943
944 // Check no forward ref
945 if( t0 != null ) return t2;
946 // Check valid forward ref, after parsing all the type extra bits.
947 // Cannot check earlier, because cannot find required 'id' until after "[]?" syntax
948 int old2 = pos();
949 match("!");
950 String id = _lexer.matchId();
951 if( !(peek(',') || peek(';') || match("->")) || id==null )
952 return posT(old1); // Reset lexer to reparse
953 pos(old2); // Reset lexer to reparse
954 // Yes a forward ref, so declare it
955 TYPES.put(tname,t1);

Callers 5

typeFunPtrMethod · 0.95
parsePrimaryMethod · 0.95
allocMethod · 0.95
funcMethod · 0.95

Calls 15

posMethod · 0.95
peekMethod · 0.95
typeFunPtrMethod · 0.95
posTMethod · 0.95
matchMethod · 0.95
makeMethod · 0.95
openMethod · 0.95
errorMethod · 0.95
typeAryMethod · 0.95
matchIdMethod · 0.45
getMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected