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

Method type

chapter21/src/main/java/com/seaofnodes/simple/Parser.java:803–848  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

801
802 // t = int|i8|i16|i32|i64|u8|u16|u32|u64|byte|bool | flt|f32|f64 | val | var | struct[?]
803 private Type type() {
804 int old1 = pos();
805 // Only type with a leading `{` is a function pointer...
806 if( peek('{') ) return typeFunPtr();
807
808 // Otherwise you get a type name
809 String tname = _lexer.matchId();
810 if( tname==null ) return null;
811
812 // Convert the type name to a type.
813 Type t0 = TYPES.get(tname);
814 // No new types as keywords
815 if( t0 == null && KEYWORDS.contains(tname) )
816 return posT(old1);
817 if( t0 == Type.BOTTOM || t0 == Type.TOP ) return t0; // var/val type inference
818 Type t1 = t0 == null ? TypeMemPtr.make(TypeStruct.makeFRef(tname)) :t0; // Null: assume a forward ref type
819 // Nest arrays and '?' as needed
820 Type t2 = t1;
821 while( true ) {
822 if( match("?") ) {
823 if( !(t2 instanceof TypeMemPtr tmp) )
824 throw error("Type "+t0+" cannot be null");
825 if( tmp.nullable() ) throw error("Type "+t2+" already allows null");
826 t2 = tmp.makeNullable();
827 } else if( match("[]") ) {
828 t2 = typeAry(t2);
829 } else
830 break;
831 }
832
833 // Check no forward ref
834 if( t0 != null ) return t2;
835 // Check valid forward ref, after parsing all the type extra bits.
836 // Cannot check earlier, because cannot find required 'id' until after "[]?" syntax
837 int old2 = pos();
838 match("!");
839 String id = _lexer.matchId();
840 if( !(peek(',') || peek(';') || match("->")) )
841 return posT(old1);
842 pos(old2); // Reset lexer to reparse
843 if( id==null )
844 return posT(old1); // Reset lexer to reparse
845 // Yes a forward ref, so declare it
846 TYPES.put(tname,t1);
847 return t2;
848 }
849
850 // Make an array type of t
851 private TypeMemPtr typeAry( Type t ) {

Callers 4

typeFunPtrMethod · 0.95
allocMethod · 0.95
funcMethod · 0.95

Calls 15

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

Tested by

no test coverage detected