« first day (773 days earlier)      last day (120 days later) » 

8:25 PM
@райтфолд Apparently, yes:
package rightfold;

public class rightfold {

}
 
user1804599
Nice!
 
user1804599
@FredOverflow TIL enum E { X { @Override public void f() { … } } public abstract void f(); }. :P
 
user1804599
But enums cannot be generic, so you can't use them for your list type. :(
 
@райтфолд Why would you want to use enums for list types?
 
user1804599
@FredOverflow No idea.
 
user1804599
8:28 PM
Nevermind.
 
user1804599
@FredOverflow I wonder how to find names in a compiler with Java interop.
 
user1804599
javac first looks at class files, then at source files, to resolve names.
 
@райтфолд Well, the names are in the class files, are they not?
 
user1804599
@FredOverflow Yeah, I want to load the class file and use reflection to find the names and types.
 
user1804599
Shouldn't be too difficult.
 
user1804599
8:31 PM
But I've never written a multiphase compiler.
 
user1804599
But I need to do it now since I want to support circular dependencies.
 
What would phase 1 and 2 be, for example?
 
user1804599
So I first have to find all declarations in one step, and then compile method bodies.
 
@райтфолд Ah yes, that's what the C# compilers does, IIRC.
 
user1804599
There's a problem with this:
 
user1804599
8:33 PM
class A { def f: B do … end }
class B extends A { def g: B do f end }
 
@райтфолд What's the problem?
 
user1804599
No idea how the compiler should deal with that.
 
user1804599
Or even something simple like this:
 
user1804599
class A extends B { }
class B { }
 
Would it be less problematic if you swapped both lines?
 
user1804599
8:36 PM
Basically, there would have to be many steps:
1) Find all classes.
2) Resolve extends clauses.
3) Find all methods.
4) Compile method bodies.
 
In 2) you can also detect circular dependencies.
 
user1804599
Circular inheritance, yes.
 
user1804599
Although I'd rather make that a separate phase.
 
user1804599
Phase 2.5.
 
user1804599
Well, it shouldn't be too difficult.
 
8:38 PM
76
A: How does C# compilation get around needing header files?

Eric LippertUPDATE: This question was the subject of my blog for February 4th 2010. Thanks for the great question! Let me lay it out for you. In the most basic sense the compiler is a "two pass compiler" because the phases that the compiler goes through are: 1) Generation of metadata. 2) Generation of IL. ...

 
user1804599
Nice, thanks.
 
Fortunately, I won't have to deal with these problems thanks to C's ancient compilation model :-D
 
user1804599
And the function that finds a symbol looks in both source and class files.
 
user1804599
How about this?
 
user1804599
package p {
    class C { }
}

class D extends p.C { }
 
user1804599
8:42 PM
Hmm, pretty easy.
 
user1804599
p.C is found in phase 1.
 
user1804599
So phase 2 knows about it.
 
user1804599
You can shorten package p { class C { } } to class p.C { } btw. :P
 
user1804599
I just keep a Map[Name, Symbol] where type Name = Vector[String]. :P
 
user1804599
So the above would have name Vector("p", "C").
 
user1804599
8:49 PM
There's a major problem.
 
user1804599
package C {
    class A { }
}

class C {
    class A { }
}
 
user1804599
What would C.A mean?
 
user1804599
@FredOverflow How does Java handle this?
 
Good question. I would have to try that out.
Eclipse seems to prefer the package A over the class A.
 
user1804599
@FredOverflow I want no mutability, and I want no duplication, so I handle phases and information availability like this. :) gist.github.com/rightfold/2bb4d854d5b030d9ee75
 
user1804599
8:59 PM
This is so awesome.
 
user1804599
Now I can specify per phase at the type level what information is available after that phase.
 
That's a problem I've been thinking about a lot lately. I guess I'll cross that bridge when I get there.
 
user1804599
It's easy to solve, as you can see!
 
user1804599
You just need an advanced type system like Scala's. :P
 
I am writing the damn thing in Scala.
 
user1804599
9:04 PM
This is similar to typedefs in C++.
 
user1804599
You can do it the same way in C++.
 
user1804599
And you can require a phase to have passed like this:
 
user1804599
def findMethods[P <: ResolveBasesPhase](…) = …
 
user1804599
and now the bases are available to the body of findMethods
 
I'll wait until you have tested this idea in practice before I open the Champagne.
 
user1804599
9:07 PM
Screw practice, this is functional programming!
 
fuck implementation #yolo
 
user1804599
I'll implement this soon.
 
I don't even have an interpreter yet.
 
user1804599
Compile to Java bytecode.
 
Do you have experience with that?
 
user1804599
9:10 PM
A little.
 
user1804599
It's quite easy.
 
user1804599
There's a library with a nice API that does it.
 
This is what I got so far:
void main(int a, int b)
{
    puts("hello world!");
}

void main(int a, int b)
{
    puts("hello world!");
}

Exception in thread "main" language.Diagnostic: foobar.c:7:10: cannot redefine main in the same scope
void main(int a, int b)
         ^
Yes I know, void main is bad :) But I wanted to test the void type somewhere.
 
user1804599
Looks like this:
 
user1804599
mv.visitCode();
mv.visitVarInsn(ILOAD, 1);
Label label = new Label();
mv.visitJumpInsn(IFLT, label);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ILOAD, 1);
mv.visitFieldInsn(PUTFIELD, "pkg/Bean", "f", "I");
Label end = new Label();
mv.visitJumpInsn(GOTO, end);
mv.visitLabel(label);
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "()V");
mv.visitInsn(ATHROW);
 
9:13 PM
looks simple
 
user1804599
scalac and Clojure use it.
 
user1804599
And one JRE implementation uses it for generating inner classes for lambdas.
 
So, is it part of the JDK, or where can I download it?
 
user1804599
 
user1804599
 
9:16 PM
You know what? This may save me a ton of work.
 
user1804599
:P
 
user1804599
What do you need an interpreter for anyway?
 
Running the code? :)
 
user1804599
Can't you just pass the code to clang and have it compile it and then invoke the generated executable?
 
No, I want stepping and sanitizers and visualisation and travel-back-in-time and stuff.
 
user1804599
9:17 PM
Oh, I see.
 
I don't care if it's 1000x slower.
 
user1804599
clang can insert UB checks for you.
 
But interpreting e.g. a switch statement could be very painful.
 
user1804599
Why?
 
Have you ever written an interpreter?
 
user1804599
9:18 PM
It's just an if-else if-else chain where the branches are gotos.
 
user1804599
And breaks are also gotos.
 
How do you interpret a break? throw a BreakException?
 
user1804599
I have written a bytecode interpreter.
 
user1804599
Only fools interpret ASTs; it's extremely difficult.
 
That's trivial, because you can just goto a different place in the bytecode.
 
user1804599
9:19 PM
Yeah, it's just pc += offset;. :D
 
@райтфолд That's exactly my point :)
 
user1804599
Compiling an AST to byte code is trivial.
 
user1804599
Interpreting byte code is trivial.
 
user1804599
Conclusion can be inferred.
 
Are you telling me problems become simpler if we split them up into smaller problems?
 
user1804599
9:20 PM
Yes.
 
user1804599
Note the hack on 110 to allow the nice : after OPCODE(…)
 
user1804599
TailCall is nice in particular.
 
I haven't looked at the code in months. It's probably terrible.
 
user1804599
Nice.
 
user1804599
 
user1804599
Hardcoded bytecode. :D
 
@райтфолд I can compile to byte arrays and then load them with a class loader, directly from memory? No temp files?
 
user1804599
@FredOverflow Yes.
 
awesome
 
user1804599
Using a custom class loader.
 
9:28 PM
...which is also provided by ASM, or not?
 
user1804599
No, but trivial to implement.
 
1
Q: Load a Byte Array into a Memory Class Loader

CrunkleI am wondering how I can load a byte array into a memory URLClassLoader? The byte array is the decrypted bytes of a jar file (as seen below)! Most of the memory class loaders are using ClassLoader and not URLClassLoader! I need it to be using URLClassLoader. byte[] fileB = Util.crypt.getFil...

 
user1804599
def loadClass(bytes: Array[Byte]): Class[_] = new ClassLoader {
  override def defineClass(name: String, bytes: Array[Byte], offset: Int, length: Int): Class[_] =
    super.defineClass(name, bytes, offset, length)
}.defineClass(name, bytes, 0, bytes.length)
 
user1804599
For some idiotic reason defineClass is protected so you have to make it public.
 
user1804599
This should work.
 
9:31 PM
Do these generated classes have access to the normal classes in the project?
Or do I have to somehow load them again?
 
user1804599
@FredOverflow Linkage is dynamic in Java, so yes.
 
user1804599
If the referenced classes aren't loaded yet the JVM will try to find them through a class loader.
 
user1804599
If it can't find them you'll get an exception.
 
user1804599
What you cannot do, however, is tell javac about classes you generate at runtime.
 
user1804599
You first have to write them to a class file.
 
user1804599
9:33 PM
But you probably do not need that anyway.
 
Probably all I need to generate is a class that implements a known interface with a single method.
I'll probably pick Runnable.
 
user1804599
Runnable. :P
 
yes, Runnable
 
user1804599
You could generate a class with all the C functions as methods.
 
Oh, of course.
 
user1804599
9:35 PM
Which implements an interface with void main(String[] args) in it. :P
 
not a crazy idea :)
 
user1804599
trait Main {
  def main(args: Array[String]): Unit
}

def compile(code: String): Class[_ <: Main] = …
 
How do class files store debugging information? Like to which source line a bytecode belongs?
 
user1804599
@FredOverflow I don't know, but you can do that.
 
user1804599
See page 77 in the PDF I linked.
 
user1804599
9:37 PM
Basically you insert labels and associate labels with file names and line numbers.
 
user1804599
I'll have to do this in the Mill compiler as well.
 
user1804599
Label label = …;
visitLabel(label);
visitLineNumber(42, label);
 
user1804599
Finding tail calls is incredibly easy.
 
user1804599
def optimizeTailCalls(instructions: Seq[Instruction]) =
  (instructions :+ null zip instructions.tail :+ null) map {
    case (CallInstruction(callee), ReturnInstruction) =>
      Seq(TailCallInstruction(callee))
    case (null, _) => Seq.empty
    case (c, _) =>
      Seq(c)
  }
 
user1804599
9:42 PM
:P
 
How about (instructions + dummyInstruction).tail instead? ;)
 
user1804599
And flatMap instead of map.
 
user1804599
Actually, since Seq[T] can build from Option[T], you can use flatMap and return Option[Instruction] instead of Seq[Instruction]. :)
 
user1804599
JVM has no tail call optimisation, though, so you can only optimise tail recursion for non-virtual methods.
 
user1804599
room topic changed to Java Sucks: Now even worse than C! [boilerplate] [checked-exceptions] [covariant-arrays] [design-patterns] [erased-generics] [inheritance-hierarchies] [java] [mutability] [singleton] [use-site-variance] [wildcard]
 
user4330208
9:49 PM
so, I'm trying to make a function that searches an array and turns all non repeating values to a certain string. So [ 0, 1, 2, 2, 3, ] would change into [ 'string', 'string', 2, 2, 'string' ].
 
user1804599
In what language?
 
user4330208
javascript...
 
user4330208
i know this room is titled java sucks but
 
user4330208
its all good, right
 
user1804599
for (var i = 0; i < array.length; ++i) {
    if (array[i] != array[i + 1] && array[i] != array[i - 1]) {
        array[i] = 'string';
    }
}
 
9:51 PM
@райтфолд Are array out of bounds accesses well-defined in JS?
 
user1804599
@FredOverflow Yes, they return undefined.
 
user4330208
I'ma try that out. I've attempted this alot.
 
user4330208
Looks very similar to my code
 
Can the 'string' already appear in the source array? :)
 
user1804599
So this code does assume you don't have undefined anywhere near the beginning or end of your array. :P
 
user1804599
9:52 PM
@FredOverflow Perhaps, then you should construct a new array instead of mutating the original.
 
user1804599
Also should be !==, not !=.
 
user1804599
!= does implicit conversions.
 
user1804599
@FredOverflow should I add extern "C" to Mill which generates JNA or JNI code and uses libclang to find declarations in header files? :D
 
Not until everything else is implemented and works ;)
 
user1804599
:(
 
user1804599
9:56 PM
OK, so I'll first write a lexer.
 
user1804599
Which is easy with parser combinators!
 
user1804599
And then a parser.
 
user1804599
Which is also easy with parser combinators!
 
user4330208
Wow, so looks like its working
 
user4330208
Thanks. I was using this:
 
user4330208
9:57 PM
for( var i = 1; i < activePool.length; i++ ){
		alert( activePool[ i ] + ' != ' + activePool[ i - 1 ] );
		if( activePool[ i ] != activePool[ i - 1 ] ){
			activePool[ i + 2 ] = 'none';
		}
	}
 
user4330208
lol me brain no worky
 
user1804599
And then I'll implement the compilation phases.
 
user1804599
Which are also easy with abstract types!
 
user1804599
@FredOverflow What does strictfp do? Should I support it?
 
user1804599
> Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.
 
user4330208
9:59 PM
Thanks @райтфолд. So did you make this chat room? Is it going to be around for a while?
 
user1804599
Ah, seems reasonable.
 
user1804599
@carb0nshel1 It's been in existence for a long time.
 
user1804599
I believe it was Fred who created it.
 
user4330208
intriguing name
 
user1804599
This room exists for 773 days now.
 
user1804599
10:00 PM
Thats 2.1164 years!
 
user4330208
So, how many languages do you know?
 
user1804599
Too many.
 
user1804599
AWK, C, C#, C++, Clojure, CoffeeScript, Crystal, D, Eiffel, Elixir, Erlang, F#, Go, Haskell, Java, JavaScript, LiveScript, Lua, Perl, Perl 6, Python, Ruby, Rust, Scala, TypeScript.
 
user1804599
Maybe more, maybe fewer.
 
user1804599
I can at least write fizzbuzz in all of these.
 
user1804599
10:05 PM
And compilers in most of these.
 
user4330208
Wow man
 
user4330208
Good grief, you still use all of those?
 
@райтфолд Unfortunately, strictfp does not mean "strict functional programming" ;)
 
user1804599
:v
 
Without strictfp, float and double calculations are probably done with 80 bits of precision.
 
user4330208
10:07 PM
Anyway, thanks for the help man, you're a life saver. I'm gonna get back to work
 
@райтфолд We should celebrate.
Strange, I would have thought the first message in this room would have been by you or by me. But it is by "user142019".
Or does chat.SO not store the entire history?
 
user1804599
@carb0nshel1 Here is hello world in all languages I know: gist.github.com/rightfold/244708a5f3b07e225e3b
 
user1804599
PHP is the easiest. :)
 
user1804599
Everything before <?php is just printed.
 
user1804599
Befunge one is lovely:
 
user1804599
10:21 PM
>               v
v"Hello, world!"<
> ............. @
 

« first day (773 days earlier)      last day (120 days later) »