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

Method type

chapter20/src/main/java/com/seaofnodes/simple/Parser.java:785–830  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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