Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining macros results in compiler errors #13343

Open
hozuki opened this issue Apr 20, 2018 · 7 comments
Open

Defining macros results in compiler errors #13343

hozuki opened this issue Apr 20, 2018 · 7 comments

Comments

@hozuki
Copy link

hozuki commented Apr 20, 2018

I'm following Macros tutorial. When I try to create a macro for the first exercise, IntelliSense throws an error: error : unbound type name 'IMacro'.

Macro code:

using Nemerle;
using Nemerle.Collections;
using Nemerle.Compiler;
using Nemerle.Compiler.Parsetree;
using Nemerle.Compiler.Typedtree;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

namespace XXX {
    macro SlowDownCompilationBy5Seconds() {
        System.Threading.Thread.Sleep(System.TimeSpan.FromSeconds(5));
    }
}

The compiler throws more errors, all of them are unbound names: GrammarElement, PExpr, SyntaxElement, etc.

I'm using Nemerle for Visual Studio version 1.2.547.0. I assume the compiler is of the same version.

This error also shows up on online environments, for example this one. They are using ncc 1.2.0.

@NN---
Copy link
Member

NN--- commented Apr 20, 2018

How you do you compile?
The easiest way is to install Nemerle integration in Visual Studio and use macro project wizard.

You have to compile with reference of Nemerle.Macro.dll.

@hozuki
Copy link
Author

hozuki commented Apr 20, 2018

I created a Nemerle project and added a macro using macro wizard. However the generated code contains too much redundancy ("generated code contains code that obviously doesn't appear in the example" + "the example macro in the tutorial can work" => "most of the code is unneeded for a simple macro test") so I deleted those.

Here's what I got using macro wizard (expression level, does not define a syntax extension):

using Nemerle;
using Nemerle.Collections;
using Nemerle.Compiler;
using Nemerle.Compiler.Parsetree;
using Nemerle.Compiler.Typedtree;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;


namespace NemerleExercises.Macros.Tutorial
{
  macro Macro1()
  {
    Macro1Impl.DoTransform(Macros.ImplicitCTX(), )
  }
  
  module Macro1Impl
  {
    public DoTransform(typer : Typer, ) : PExpr
    {
      Macros.DefineCTX(typer);
      // TODO: Add implementation here.
      ;
    }
  }
}

The piece of code raises errors described above; also, you can see that formal parameter list of DoTransform is incomplete.

For compiling, I use the default method provided by Nemerle VS extension. That is, Nemerle.dll assembly is automatically referenced from GAC and copied to local directory; using bundled ncc; ncc invocation arguments are automatically configured by the extension based on Nemerle project settings.

@hozuki
Copy link
Author

hozuki commented Apr 20, 2018

Well I got it.

Since the tutorial didn't mention it, and Nemerle is known as a meta-programming language, I thought macros feature is included in the Nemerle runtime (i.e. Nemerle.dll) and turned on by default. It turns out you have to reference Nemerle.Macros.dll and using Nemerle.Macros; to use the macro macro and associated types.

Thank you for the tip.

@hozuki hozuki closed this as completed Apr 20, 2018
@hozuki
Copy link
Author

hozuki commented Apr 20, 2018

Sorry it still doesn't get fixed. (using Nemerle.Macros; is not necessary)

The IntelliSense does not prompt anything in the macro definition now. (There used to be "unbound names" errors.) But when I try the macro "unbound name" appears at the usage.

On the other hand, when compiling the project, the compiler still throws "unbound names" error. I'm sure that Nemerle.Compiler, Nemerle.Compiler.Parsetree and Nemerle.Compiler.Typedtree are using-ed (IntelliSense recognizes the types) but the compiler seems to know nothing about it.

image

@hozuki hozuki reopened this Apr 20, 2018
@NN---
Copy link
Member

NN--- commented Apr 20, 2018

There is no intellisense inside macro.
You have to create a module (static class) and method inside, then pass macro arguments to this method.
Inside method there is intellisense.

If you create using VS wizard, it does this automatically.

@hozuki
Copy link
Author

hozuki commented Apr 20, 2018

Thanks for the reply. My point is not about IntelliSense, but a inconsistency between IntelliSense and the compiler, on the same piece of code.

Reading the tutorial again, the assembly that has to be referenced is Nemerle.Compiler, not Nemerle.Macros. But, oddly, when I reference Nemerle.Macros, the macro can still be compiled by ncc.

Code:

using Nemerle.Compiler;
using Nemerle.Compiler.Parsetree;
using Nemerle.Compiler.Typedtree;

// Taken directly from the tutorial
macro m () {
	Nemerle.IO.printf ("compile-time\n");
	<[ Nemerle.IO.printf ("run-time\n") ]>;
}

ncc referencing Nemerle.Macros: compilation successful (false positive)

ncc -out out.dll -t library -r Nemerle.Macros.dll Macro1.n

ncc referencing Nemerle.Compiler: compilation successful (true positive)

ncc -out out.dll -t library -r Nemerle.Compiler.dll Macro1.n

VS Nemerle project, adding Nemerle.Macros to "References": no IntelliSense errors (false positive); compilation failed (true negative)

VS Nemerle project, adding Nemerle.Compiler to "References": no IntelliSense errors (true positive); compilation successful (true positive)


However, under none of these case can I use the compiled macro. Consider the code:

using Nemerle;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using Nemerle.Compiler;
using Nemerle.Compiler.Parsetree;
using Nemerle.Compiler.Typedtree;

// Again, taken directly from the tutorial
macro m () {
	Nemerle.IO.printf ("compile-time\n");
	<[ Nemerle.IO.printf ("run-time\n") ]>;
}

namespace NemerleExercises.Macros.Tutorial {
	module Program {

		Main(): void {
			// Here if you input "mMacro." and wait for the completion list you can see that the macro class exists,
			// so the macro itself can be compiled.
			// But this usage raises "unbound name 'm'" error.
			m();
			_ = System.Console.ReadKey();
		}

	}
}

@hozuki
Copy link
Author

hozuki commented Apr 20, 2018

If you didn't reference Nemerle.Compiler or Nemerle.Macros (not suggested), IntelliSense will at least throw an error at the macro keyword ("unbound name 'IMacro'"). That's why I think whether IntelliSense prompts worths a mention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants